danielebarchiesi@0
|
1 <?php
|
danielebarchiesi@0
|
2
|
danielebarchiesi@0
|
3 /**
|
danielebarchiesi@0
|
4 * @file
|
danielebarchiesi@0
|
5 * Common functions that many Drupal modules will need to reference.
|
danielebarchiesi@0
|
6 *
|
danielebarchiesi@0
|
7 * The functions that are critical and need to be available even when serving
|
danielebarchiesi@0
|
8 * a cached page are instead located in bootstrap.inc.
|
danielebarchiesi@0
|
9 */
|
danielebarchiesi@0
|
10
|
danielebarchiesi@0
|
11 /**
|
danielebarchiesi@0
|
12 * @defgroup php_wrappers PHP wrapper functions
|
danielebarchiesi@0
|
13 * @{
|
danielebarchiesi@0
|
14 * Functions that are wrappers or custom implementations of PHP functions.
|
danielebarchiesi@0
|
15 *
|
danielebarchiesi@0
|
16 * Certain PHP functions should not be used in Drupal. Instead, Drupal's
|
danielebarchiesi@0
|
17 * replacement functions should be used.
|
danielebarchiesi@0
|
18 *
|
danielebarchiesi@0
|
19 * For example, for improved or more secure UTF8-handling, or RFC-compliant
|
danielebarchiesi@0
|
20 * handling of URLs in Drupal.
|
danielebarchiesi@0
|
21 *
|
danielebarchiesi@0
|
22 * For ease of use and memorizing, all these wrapper functions use the same name
|
danielebarchiesi@0
|
23 * as the original PHP function, but prefixed with "drupal_". Beware, however,
|
danielebarchiesi@0
|
24 * that not all wrapper functions support the same arguments as the original
|
danielebarchiesi@0
|
25 * functions.
|
danielebarchiesi@0
|
26 *
|
danielebarchiesi@0
|
27 * You should always use these wrapper functions in your code.
|
danielebarchiesi@0
|
28 *
|
danielebarchiesi@0
|
29 * Wrong:
|
danielebarchiesi@0
|
30 * @code
|
danielebarchiesi@0
|
31 * $my_substring = substr($original_string, 0, 5);
|
danielebarchiesi@0
|
32 * @endcode
|
danielebarchiesi@0
|
33 *
|
danielebarchiesi@0
|
34 * Correct:
|
danielebarchiesi@0
|
35 * @code
|
danielebarchiesi@0
|
36 * $my_substring = drupal_substr($original_string, 0, 5);
|
danielebarchiesi@0
|
37 * @endcode
|
danielebarchiesi@0
|
38 *
|
danielebarchiesi@0
|
39 * @}
|
danielebarchiesi@0
|
40 */
|
danielebarchiesi@0
|
41
|
danielebarchiesi@0
|
42 /**
|
danielebarchiesi@0
|
43 * Return status for saving which involved creating a new item.
|
danielebarchiesi@0
|
44 */
|
danielebarchiesi@0
|
45 define('SAVED_NEW', 1);
|
danielebarchiesi@0
|
46
|
danielebarchiesi@0
|
47 /**
|
danielebarchiesi@0
|
48 * Return status for saving which involved an update to an existing item.
|
danielebarchiesi@0
|
49 */
|
danielebarchiesi@0
|
50 define('SAVED_UPDATED', 2);
|
danielebarchiesi@0
|
51
|
danielebarchiesi@0
|
52 /**
|
danielebarchiesi@0
|
53 * Return status for saving which deleted an existing item.
|
danielebarchiesi@0
|
54 */
|
danielebarchiesi@0
|
55 define('SAVED_DELETED', 3);
|
danielebarchiesi@0
|
56
|
danielebarchiesi@0
|
57 /**
|
danielebarchiesi@0
|
58 * The default group for system CSS files added to the page.
|
danielebarchiesi@0
|
59 */
|
danielebarchiesi@0
|
60 define('CSS_SYSTEM', -100);
|
danielebarchiesi@0
|
61
|
danielebarchiesi@0
|
62 /**
|
danielebarchiesi@0
|
63 * The default group for module CSS files added to the page.
|
danielebarchiesi@0
|
64 */
|
danielebarchiesi@0
|
65 define('CSS_DEFAULT', 0);
|
danielebarchiesi@0
|
66
|
danielebarchiesi@0
|
67 /**
|
danielebarchiesi@0
|
68 * The default group for theme CSS files added to the page.
|
danielebarchiesi@0
|
69 */
|
danielebarchiesi@0
|
70 define('CSS_THEME', 100);
|
danielebarchiesi@0
|
71
|
danielebarchiesi@0
|
72 /**
|
danielebarchiesi@0
|
73 * The default group for JavaScript and jQuery libraries added to the page.
|
danielebarchiesi@0
|
74 */
|
danielebarchiesi@0
|
75 define('JS_LIBRARY', -100);
|
danielebarchiesi@0
|
76
|
danielebarchiesi@0
|
77 /**
|
danielebarchiesi@0
|
78 * The default group for module JavaScript code added to the page.
|
danielebarchiesi@0
|
79 */
|
danielebarchiesi@0
|
80 define('JS_DEFAULT', 0);
|
danielebarchiesi@0
|
81
|
danielebarchiesi@0
|
82 /**
|
danielebarchiesi@0
|
83 * The default group for theme JavaScript code added to the page.
|
danielebarchiesi@0
|
84 */
|
danielebarchiesi@0
|
85 define('JS_THEME', 100);
|
danielebarchiesi@0
|
86
|
danielebarchiesi@0
|
87 /**
|
danielebarchiesi@0
|
88 * Error code indicating that the request exceeded the specified timeout.
|
danielebarchiesi@0
|
89 *
|
danielebarchiesi@0
|
90 * @see drupal_http_request()
|
danielebarchiesi@0
|
91 */
|
danielebarchiesi@0
|
92 define('HTTP_REQUEST_TIMEOUT', -1);
|
danielebarchiesi@0
|
93
|
danielebarchiesi@0
|
94 /**
|
danielebarchiesi@0
|
95 * @defgroup block_caching Block Caching
|
danielebarchiesi@0
|
96 * @{
|
danielebarchiesi@0
|
97 * Constants that define each block's caching state.
|
danielebarchiesi@0
|
98 *
|
danielebarchiesi@0
|
99 * Modules specify how their blocks can be cached in their hook_block_info()
|
danielebarchiesi@0
|
100 * implementations. Caching can be turned off (DRUPAL_NO_CACHE), managed by the
|
danielebarchiesi@0
|
101 * module declaring the block (DRUPAL_CACHE_CUSTOM), or managed by the core
|
danielebarchiesi@0
|
102 * Block module. If the Block module is managing the cache, you can specify that
|
danielebarchiesi@0
|
103 * the block is the same for every page and user (DRUPAL_CACHE_GLOBAL), or that
|
danielebarchiesi@0
|
104 * it can change depending on the page (DRUPAL_CACHE_PER_PAGE) or by user
|
danielebarchiesi@0
|
105 * (DRUPAL_CACHE_PER_ROLE or DRUPAL_CACHE_PER_USER). Page and user settings can
|
danielebarchiesi@0
|
106 * be combined with a bitwise-binary or operator; for example,
|
danielebarchiesi@0
|
107 * DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE means that the block can change
|
danielebarchiesi@0
|
108 * depending on the user role or page it is on.
|
danielebarchiesi@0
|
109 *
|
danielebarchiesi@0
|
110 * The block cache is cleared in cache_clear_all(), and uses the same clearing
|
danielebarchiesi@0
|
111 * policy than page cache (node, comment, user, taxonomy added or updated...).
|
danielebarchiesi@0
|
112 * Blocks requiring more fine-grained clearing might consider disabling the
|
danielebarchiesi@0
|
113 * built-in block cache (DRUPAL_NO_CACHE) and roll their own.
|
danielebarchiesi@0
|
114 *
|
danielebarchiesi@0
|
115 * Note that user 1 is excluded from block caching.
|
danielebarchiesi@0
|
116 */
|
danielebarchiesi@0
|
117
|
danielebarchiesi@0
|
118 /**
|
danielebarchiesi@0
|
119 * The block should not get cached.
|
danielebarchiesi@0
|
120 *
|
danielebarchiesi@0
|
121 * This setting should be used:
|
danielebarchiesi@0
|
122 * - For simple blocks (notably those that do not perform any db query), where
|
danielebarchiesi@0
|
123 * querying the db cache would be more expensive than directly generating the
|
danielebarchiesi@0
|
124 * content.
|
danielebarchiesi@0
|
125 * - For blocks that change too frequently.
|
danielebarchiesi@0
|
126 */
|
danielebarchiesi@0
|
127 define('DRUPAL_NO_CACHE', -1);
|
danielebarchiesi@0
|
128
|
danielebarchiesi@0
|
129 /**
|
danielebarchiesi@0
|
130 * The block is handling its own caching in its hook_block_view().
|
danielebarchiesi@0
|
131 *
|
danielebarchiesi@0
|
132 * This setting is useful when time based expiration is needed or a site uses a
|
danielebarchiesi@0
|
133 * node access which invalidates standard block cache.
|
danielebarchiesi@0
|
134 */
|
danielebarchiesi@0
|
135 define('DRUPAL_CACHE_CUSTOM', -2);
|
danielebarchiesi@0
|
136
|
danielebarchiesi@0
|
137 /**
|
danielebarchiesi@0
|
138 * The block or element can change depending on the user's roles.
|
danielebarchiesi@0
|
139 *
|
danielebarchiesi@0
|
140 * This is the default setting for blocks, used when the block does not specify
|
danielebarchiesi@0
|
141 * anything.
|
danielebarchiesi@0
|
142 */
|
danielebarchiesi@0
|
143 define('DRUPAL_CACHE_PER_ROLE', 0x0001);
|
danielebarchiesi@0
|
144
|
danielebarchiesi@0
|
145 /**
|
danielebarchiesi@0
|
146 * The block or element can change depending on the user.
|
danielebarchiesi@0
|
147 *
|
danielebarchiesi@0
|
148 * This setting can be resource-consuming for sites with large number of users,
|
danielebarchiesi@0
|
149 * and thus should only be used when DRUPAL_CACHE_PER_ROLE is not sufficient.
|
danielebarchiesi@0
|
150 */
|
danielebarchiesi@0
|
151 define('DRUPAL_CACHE_PER_USER', 0x0002);
|
danielebarchiesi@0
|
152
|
danielebarchiesi@0
|
153 /**
|
danielebarchiesi@0
|
154 * The block or element can change depending on the page being viewed.
|
danielebarchiesi@0
|
155 */
|
danielebarchiesi@0
|
156 define('DRUPAL_CACHE_PER_PAGE', 0x0004);
|
danielebarchiesi@0
|
157
|
danielebarchiesi@0
|
158 /**
|
danielebarchiesi@0
|
159 * The block or element is the same for every user and page that it is visible.
|
danielebarchiesi@0
|
160 */
|
danielebarchiesi@0
|
161 define('DRUPAL_CACHE_GLOBAL', 0x0008);
|
danielebarchiesi@0
|
162
|
danielebarchiesi@0
|
163 /**
|
danielebarchiesi@0
|
164 * @} End of "defgroup block_caching".
|
danielebarchiesi@0
|
165 */
|
danielebarchiesi@0
|
166
|
danielebarchiesi@0
|
167 /**
|
danielebarchiesi@0
|
168 * Adds content to a specified region.
|
danielebarchiesi@0
|
169 *
|
danielebarchiesi@0
|
170 * @param $region
|
danielebarchiesi@0
|
171 * Page region the content is added to.
|
danielebarchiesi@0
|
172 * @param $data
|
danielebarchiesi@0
|
173 * Content to be added.
|
danielebarchiesi@0
|
174 */
|
danielebarchiesi@0
|
175 function drupal_add_region_content($region = NULL, $data = NULL) {
|
danielebarchiesi@0
|
176 static $content = array();
|
danielebarchiesi@0
|
177
|
danielebarchiesi@0
|
178 if (isset($region) && isset($data)) {
|
danielebarchiesi@0
|
179 $content[$region][] = $data;
|
danielebarchiesi@0
|
180 }
|
danielebarchiesi@0
|
181 return $content;
|
danielebarchiesi@0
|
182 }
|
danielebarchiesi@0
|
183
|
danielebarchiesi@0
|
184 /**
|
danielebarchiesi@0
|
185 * Gets assigned content for a given region.
|
danielebarchiesi@0
|
186 *
|
danielebarchiesi@0
|
187 * @param $region
|
danielebarchiesi@0
|
188 * A specified region to fetch content for. If NULL, all regions will be
|
danielebarchiesi@0
|
189 * returned.
|
danielebarchiesi@0
|
190 * @param $delimiter
|
danielebarchiesi@0
|
191 * Content to be inserted between imploded array elements.
|
danielebarchiesi@0
|
192 */
|
danielebarchiesi@0
|
193 function drupal_get_region_content($region = NULL, $delimiter = ' ') {
|
danielebarchiesi@0
|
194 $content = drupal_add_region_content();
|
danielebarchiesi@0
|
195 if (isset($region)) {
|
danielebarchiesi@0
|
196 if (isset($content[$region]) && is_array($content[$region])) {
|
danielebarchiesi@0
|
197 return implode($delimiter, $content[$region]);
|
danielebarchiesi@0
|
198 }
|
danielebarchiesi@0
|
199 }
|
danielebarchiesi@0
|
200 else {
|
danielebarchiesi@0
|
201 foreach (array_keys($content) as $region) {
|
danielebarchiesi@0
|
202 if (is_array($content[$region])) {
|
danielebarchiesi@0
|
203 $content[$region] = implode($delimiter, $content[$region]);
|
danielebarchiesi@0
|
204 }
|
danielebarchiesi@0
|
205 }
|
danielebarchiesi@0
|
206 return $content;
|
danielebarchiesi@0
|
207 }
|
danielebarchiesi@0
|
208 }
|
danielebarchiesi@0
|
209
|
danielebarchiesi@0
|
210 /**
|
danielebarchiesi@0
|
211 * Gets the name of the currently active installation profile.
|
danielebarchiesi@0
|
212 *
|
danielebarchiesi@0
|
213 * When this function is called during Drupal's initial installation process,
|
danielebarchiesi@0
|
214 * the name of the profile that's about to be installed is stored in the global
|
danielebarchiesi@0
|
215 * installation state. At all other times, the standard Drupal systems variable
|
danielebarchiesi@0
|
216 * table contains the name of the current profile, and we can call
|
danielebarchiesi@0
|
217 * variable_get() to determine what one is active.
|
danielebarchiesi@0
|
218 *
|
danielebarchiesi@0
|
219 * @return $profile
|
danielebarchiesi@0
|
220 * The name of the installation profile.
|
danielebarchiesi@0
|
221 */
|
danielebarchiesi@0
|
222 function drupal_get_profile() {
|
danielebarchiesi@0
|
223 global $install_state;
|
danielebarchiesi@0
|
224
|
danielebarchiesi@0
|
225 if (isset($install_state['parameters']['profile'])) {
|
danielebarchiesi@0
|
226 $profile = $install_state['parameters']['profile'];
|
danielebarchiesi@0
|
227 }
|
danielebarchiesi@0
|
228 else {
|
danielebarchiesi@0
|
229 $profile = variable_get('install_profile', 'standard');
|
danielebarchiesi@0
|
230 }
|
danielebarchiesi@0
|
231
|
danielebarchiesi@0
|
232 return $profile;
|
danielebarchiesi@0
|
233 }
|
danielebarchiesi@0
|
234
|
danielebarchiesi@0
|
235
|
danielebarchiesi@0
|
236 /**
|
danielebarchiesi@0
|
237 * Sets the breadcrumb trail for the current page.
|
danielebarchiesi@0
|
238 *
|
danielebarchiesi@0
|
239 * @param $breadcrumb
|
danielebarchiesi@0
|
240 * Array of links, starting with "home" and proceeding up to but not including
|
danielebarchiesi@0
|
241 * the current page.
|
danielebarchiesi@0
|
242 */
|
danielebarchiesi@0
|
243 function drupal_set_breadcrumb($breadcrumb = NULL) {
|
danielebarchiesi@0
|
244 $stored_breadcrumb = &drupal_static(__FUNCTION__);
|
danielebarchiesi@0
|
245
|
danielebarchiesi@0
|
246 if (isset($breadcrumb)) {
|
danielebarchiesi@0
|
247 $stored_breadcrumb = $breadcrumb;
|
danielebarchiesi@0
|
248 }
|
danielebarchiesi@0
|
249 return $stored_breadcrumb;
|
danielebarchiesi@0
|
250 }
|
danielebarchiesi@0
|
251
|
danielebarchiesi@0
|
252 /**
|
danielebarchiesi@0
|
253 * Gets the breadcrumb trail for the current page.
|
danielebarchiesi@0
|
254 */
|
danielebarchiesi@0
|
255 function drupal_get_breadcrumb() {
|
danielebarchiesi@0
|
256 $breadcrumb = drupal_set_breadcrumb();
|
danielebarchiesi@0
|
257
|
danielebarchiesi@0
|
258 if (!isset($breadcrumb)) {
|
danielebarchiesi@0
|
259 $breadcrumb = menu_get_active_breadcrumb();
|
danielebarchiesi@0
|
260 }
|
danielebarchiesi@0
|
261
|
danielebarchiesi@0
|
262 return $breadcrumb;
|
danielebarchiesi@0
|
263 }
|
danielebarchiesi@0
|
264
|
danielebarchiesi@0
|
265 /**
|
danielebarchiesi@0
|
266 * Returns a string containing RDF namespace declarations for use in XML and
|
danielebarchiesi@0
|
267 * XHTML output.
|
danielebarchiesi@0
|
268 */
|
danielebarchiesi@0
|
269 function drupal_get_rdf_namespaces() {
|
danielebarchiesi@0
|
270 $xml_rdf_namespaces = array();
|
danielebarchiesi@0
|
271
|
danielebarchiesi@0
|
272 // Serializes the RDF namespaces in XML namespace syntax.
|
danielebarchiesi@0
|
273 if (function_exists('rdf_get_namespaces')) {
|
danielebarchiesi@0
|
274 foreach (rdf_get_namespaces() as $prefix => $uri) {
|
danielebarchiesi@0
|
275 $xml_rdf_namespaces[] = 'xmlns:' . $prefix . '="' . $uri . '"';
|
danielebarchiesi@0
|
276 }
|
danielebarchiesi@0
|
277 }
|
danielebarchiesi@0
|
278 return count($xml_rdf_namespaces) ? "\n " . implode("\n ", $xml_rdf_namespaces) : '';
|
danielebarchiesi@0
|
279 }
|
danielebarchiesi@0
|
280
|
danielebarchiesi@0
|
281 /**
|
danielebarchiesi@0
|
282 * Adds output to the HEAD tag of the HTML page.
|
danielebarchiesi@0
|
283 *
|
danielebarchiesi@0
|
284 * This function can be called as long as the headers aren't sent. Pass no
|
danielebarchiesi@0
|
285 * arguments (or NULL for both) to retrieve the currently stored elements.
|
danielebarchiesi@0
|
286 *
|
danielebarchiesi@0
|
287 * @param $data
|
danielebarchiesi@0
|
288 * A renderable array. If the '#type' key is not set then 'html_tag' will be
|
danielebarchiesi@0
|
289 * added as the default '#type'.
|
danielebarchiesi@0
|
290 * @param $key
|
danielebarchiesi@0
|
291 * A unique string key to allow implementations of hook_html_head_alter() to
|
danielebarchiesi@0
|
292 * identify the element in $data. Required if $data is not NULL.
|
danielebarchiesi@0
|
293 *
|
danielebarchiesi@0
|
294 * @return
|
danielebarchiesi@0
|
295 * An array of all stored HEAD elements.
|
danielebarchiesi@0
|
296 *
|
danielebarchiesi@0
|
297 * @see theme_html_tag()
|
danielebarchiesi@0
|
298 */
|
danielebarchiesi@0
|
299 function drupal_add_html_head($data = NULL, $key = NULL) {
|
danielebarchiesi@0
|
300 $stored_head = &drupal_static(__FUNCTION__);
|
danielebarchiesi@0
|
301
|
danielebarchiesi@0
|
302 if (!isset($stored_head)) {
|
danielebarchiesi@0
|
303 // Make sure the defaults, including Content-Type, come first.
|
danielebarchiesi@0
|
304 $stored_head = _drupal_default_html_head();
|
danielebarchiesi@0
|
305 }
|
danielebarchiesi@0
|
306
|
danielebarchiesi@0
|
307 if (isset($data) && isset($key)) {
|
danielebarchiesi@0
|
308 if (!isset($data['#type'])) {
|
danielebarchiesi@0
|
309 $data['#type'] = 'html_tag';
|
danielebarchiesi@0
|
310 }
|
danielebarchiesi@0
|
311 $stored_head[$key] = $data;
|
danielebarchiesi@0
|
312 }
|
danielebarchiesi@0
|
313 return $stored_head;
|
danielebarchiesi@0
|
314 }
|
danielebarchiesi@0
|
315
|
danielebarchiesi@0
|
316 /**
|
danielebarchiesi@0
|
317 * Returns elements that are always displayed in the HEAD tag of the HTML page.
|
danielebarchiesi@0
|
318 */
|
danielebarchiesi@0
|
319 function _drupal_default_html_head() {
|
danielebarchiesi@0
|
320 // Add default elements. Make sure the Content-Type comes first because the
|
danielebarchiesi@0
|
321 // IE browser may be vulnerable to XSS via encoding attacks from any content
|
danielebarchiesi@0
|
322 // that comes before this META tag, such as a TITLE tag.
|
danielebarchiesi@0
|
323 $elements['system_meta_content_type'] = array(
|
danielebarchiesi@0
|
324 '#type' => 'html_tag',
|
danielebarchiesi@0
|
325 '#tag' => 'meta',
|
danielebarchiesi@0
|
326 '#attributes' => array(
|
danielebarchiesi@0
|
327 'http-equiv' => 'Content-Type',
|
danielebarchiesi@0
|
328 'content' => 'text/html; charset=utf-8',
|
danielebarchiesi@0
|
329 ),
|
danielebarchiesi@0
|
330 // Security: This always has to be output first.
|
danielebarchiesi@0
|
331 '#weight' => -1000,
|
danielebarchiesi@0
|
332 );
|
danielebarchiesi@0
|
333 // Show Drupal and the major version number in the META GENERATOR tag.
|
danielebarchiesi@0
|
334 // Get the major version.
|
danielebarchiesi@0
|
335 list($version, ) = explode('.', VERSION);
|
danielebarchiesi@0
|
336 $elements['system_meta_generator'] = array(
|
danielebarchiesi@0
|
337 '#type' => 'html_tag',
|
danielebarchiesi@0
|
338 '#tag' => 'meta',
|
danielebarchiesi@0
|
339 '#attributes' => array(
|
danielebarchiesi@0
|
340 'name' => 'Generator',
|
danielebarchiesi@0
|
341 'content' => 'Drupal ' . $version . ' (http://drupal.org)',
|
danielebarchiesi@0
|
342 ),
|
danielebarchiesi@0
|
343 );
|
danielebarchiesi@0
|
344 // Also send the generator in the HTTP header.
|
danielebarchiesi@0
|
345 $elements['system_meta_generator']['#attached']['drupal_add_http_header'][] = array('X-Generator', $elements['system_meta_generator']['#attributes']['content']);
|
danielebarchiesi@0
|
346 return $elements;
|
danielebarchiesi@0
|
347 }
|
danielebarchiesi@0
|
348
|
danielebarchiesi@0
|
349 /**
|
danielebarchiesi@0
|
350 * Retrieves output to be displayed in the HEAD tag of the HTML page.
|
danielebarchiesi@0
|
351 */
|
danielebarchiesi@0
|
352 function drupal_get_html_head() {
|
danielebarchiesi@0
|
353 $elements = drupal_add_html_head();
|
danielebarchiesi@0
|
354 drupal_alter('html_head', $elements);
|
danielebarchiesi@0
|
355 return drupal_render($elements);
|
danielebarchiesi@0
|
356 }
|
danielebarchiesi@0
|
357
|
danielebarchiesi@0
|
358 /**
|
danielebarchiesi@0
|
359 * Adds a feed URL for the current page.
|
danielebarchiesi@0
|
360 *
|
danielebarchiesi@0
|
361 * This function can be called as long the HTML header hasn't been sent.
|
danielebarchiesi@0
|
362 *
|
danielebarchiesi@0
|
363 * @param $url
|
danielebarchiesi@0
|
364 * An internal system path or a fully qualified external URL of the feed.
|
danielebarchiesi@0
|
365 * @param $title
|
danielebarchiesi@0
|
366 * The title of the feed.
|
danielebarchiesi@0
|
367 */
|
danielebarchiesi@0
|
368 function drupal_add_feed($url = NULL, $title = '') {
|
danielebarchiesi@0
|
369 $stored_feed_links = &drupal_static(__FUNCTION__, array());
|
danielebarchiesi@0
|
370
|
danielebarchiesi@0
|
371 if (isset($url)) {
|
danielebarchiesi@0
|
372 $stored_feed_links[$url] = theme('feed_icon', array('url' => $url, 'title' => $title));
|
danielebarchiesi@0
|
373
|
danielebarchiesi@0
|
374 drupal_add_html_head_link(array(
|
danielebarchiesi@0
|
375 'rel' => 'alternate',
|
danielebarchiesi@0
|
376 'type' => 'application/rss+xml',
|
danielebarchiesi@0
|
377 'title' => $title,
|
danielebarchiesi@0
|
378 // Force the URL to be absolute, for consistency with other <link> tags
|
danielebarchiesi@0
|
379 // output by Drupal.
|
danielebarchiesi@0
|
380 'href' => url($url, array('absolute' => TRUE)),
|
danielebarchiesi@0
|
381 ));
|
danielebarchiesi@0
|
382 }
|
danielebarchiesi@0
|
383 return $stored_feed_links;
|
danielebarchiesi@0
|
384 }
|
danielebarchiesi@0
|
385
|
danielebarchiesi@0
|
386 /**
|
danielebarchiesi@0
|
387 * Gets the feed URLs for the current page.
|
danielebarchiesi@0
|
388 *
|
danielebarchiesi@0
|
389 * @param $delimiter
|
danielebarchiesi@0
|
390 * A delimiter to split feeds by.
|
danielebarchiesi@0
|
391 */
|
danielebarchiesi@0
|
392 function drupal_get_feeds($delimiter = "\n") {
|
danielebarchiesi@0
|
393 $feeds = drupal_add_feed();
|
danielebarchiesi@0
|
394 return implode($feeds, $delimiter);
|
danielebarchiesi@0
|
395 }
|
danielebarchiesi@0
|
396
|
danielebarchiesi@0
|
397 /**
|
danielebarchiesi@0
|
398 * @defgroup http_handling HTTP handling
|
danielebarchiesi@0
|
399 * @{
|
danielebarchiesi@0
|
400 * Functions to properly handle HTTP responses.
|
danielebarchiesi@0
|
401 */
|
danielebarchiesi@0
|
402
|
danielebarchiesi@0
|
403 /**
|
danielebarchiesi@0
|
404 * Processes a URL query parameter array to remove unwanted elements.
|
danielebarchiesi@0
|
405 *
|
danielebarchiesi@0
|
406 * @param $query
|
danielebarchiesi@0
|
407 * (optional) An array to be processed. Defaults to $_GET.
|
danielebarchiesi@0
|
408 * @param $exclude
|
danielebarchiesi@0
|
409 * (optional) A list of $query array keys to remove. Use "parent[child]" to
|
danielebarchiesi@0
|
410 * exclude nested items. Defaults to array('q').
|
danielebarchiesi@0
|
411 * @param $parent
|
danielebarchiesi@0
|
412 * Internal use only. Used to build the $query array key for nested items.
|
danielebarchiesi@0
|
413 *
|
danielebarchiesi@0
|
414 * @return
|
danielebarchiesi@0
|
415 * An array containing query parameters, which can be used for url().
|
danielebarchiesi@0
|
416 */
|
danielebarchiesi@0
|
417 function drupal_get_query_parameters(array $query = NULL, array $exclude = array('q'), $parent = '') {
|
danielebarchiesi@0
|
418 // Set defaults, if none given.
|
danielebarchiesi@0
|
419 if (!isset($query)) {
|
danielebarchiesi@0
|
420 $query = $_GET;
|
danielebarchiesi@0
|
421 }
|
danielebarchiesi@0
|
422 // If $exclude is empty, there is nothing to filter.
|
danielebarchiesi@0
|
423 if (empty($exclude)) {
|
danielebarchiesi@0
|
424 return $query;
|
danielebarchiesi@0
|
425 }
|
danielebarchiesi@0
|
426 elseif (!$parent) {
|
danielebarchiesi@0
|
427 $exclude = array_flip($exclude);
|
danielebarchiesi@0
|
428 }
|
danielebarchiesi@0
|
429
|
danielebarchiesi@0
|
430 $params = array();
|
danielebarchiesi@0
|
431 foreach ($query as $key => $value) {
|
danielebarchiesi@0
|
432 $string_key = ($parent ? $parent . '[' . $key . ']' : $key);
|
danielebarchiesi@0
|
433 if (isset($exclude[$string_key])) {
|
danielebarchiesi@0
|
434 continue;
|
danielebarchiesi@0
|
435 }
|
danielebarchiesi@0
|
436
|
danielebarchiesi@0
|
437 if (is_array($value)) {
|
danielebarchiesi@0
|
438 $params[$key] = drupal_get_query_parameters($value, $exclude, $string_key);
|
danielebarchiesi@0
|
439 }
|
danielebarchiesi@0
|
440 else {
|
danielebarchiesi@0
|
441 $params[$key] = $value;
|
danielebarchiesi@0
|
442 }
|
danielebarchiesi@0
|
443 }
|
danielebarchiesi@0
|
444
|
danielebarchiesi@0
|
445 return $params;
|
danielebarchiesi@0
|
446 }
|
danielebarchiesi@0
|
447
|
danielebarchiesi@0
|
448 /**
|
danielebarchiesi@0
|
449 * Splits a URL-encoded query string into an array.
|
danielebarchiesi@0
|
450 *
|
danielebarchiesi@0
|
451 * @param $query
|
danielebarchiesi@0
|
452 * The query string to split.
|
danielebarchiesi@0
|
453 *
|
danielebarchiesi@0
|
454 * @return
|
danielebarchiesi@0
|
455 * An array of URL decoded couples $param_name => $value.
|
danielebarchiesi@0
|
456 */
|
danielebarchiesi@0
|
457 function drupal_get_query_array($query) {
|
danielebarchiesi@0
|
458 $result = array();
|
danielebarchiesi@0
|
459 if (!empty($query)) {
|
danielebarchiesi@0
|
460 foreach (explode('&', $query) as $param) {
|
danielebarchiesi@0
|
461 $param = explode('=', $param);
|
danielebarchiesi@0
|
462 $result[$param[0]] = isset($param[1]) ? rawurldecode($param[1]) : '';
|
danielebarchiesi@0
|
463 }
|
danielebarchiesi@0
|
464 }
|
danielebarchiesi@0
|
465 return $result;
|
danielebarchiesi@0
|
466 }
|
danielebarchiesi@0
|
467
|
danielebarchiesi@0
|
468 /**
|
danielebarchiesi@0
|
469 * Parses an array into a valid, rawurlencoded query string.
|
danielebarchiesi@0
|
470 *
|
danielebarchiesi@0
|
471 * This differs from http_build_query() as we need to rawurlencode() (instead of
|
danielebarchiesi@0
|
472 * urlencode()) all query parameters.
|
danielebarchiesi@0
|
473 *
|
danielebarchiesi@0
|
474 * @param $query
|
danielebarchiesi@0
|
475 * The query parameter array to be processed, e.g. $_GET.
|
danielebarchiesi@0
|
476 * @param $parent
|
danielebarchiesi@0
|
477 * Internal use only. Used to build the $query array key for nested items.
|
danielebarchiesi@0
|
478 *
|
danielebarchiesi@0
|
479 * @return
|
danielebarchiesi@0
|
480 * A rawurlencoded string which can be used as or appended to the URL query
|
danielebarchiesi@0
|
481 * string.
|
danielebarchiesi@0
|
482 *
|
danielebarchiesi@0
|
483 * @see drupal_get_query_parameters()
|
danielebarchiesi@0
|
484 * @ingroup php_wrappers
|
danielebarchiesi@0
|
485 */
|
danielebarchiesi@0
|
486 function drupal_http_build_query(array $query, $parent = '') {
|
danielebarchiesi@0
|
487 $params = array();
|
danielebarchiesi@0
|
488
|
danielebarchiesi@0
|
489 foreach ($query as $key => $value) {
|
danielebarchiesi@0
|
490 $key = ($parent ? $parent . '[' . rawurlencode($key) . ']' : rawurlencode($key));
|
danielebarchiesi@0
|
491
|
danielebarchiesi@0
|
492 // Recurse into children.
|
danielebarchiesi@0
|
493 if (is_array($value)) {
|
danielebarchiesi@0
|
494 $params[] = drupal_http_build_query($value, $key);
|
danielebarchiesi@0
|
495 }
|
danielebarchiesi@0
|
496 // If a query parameter value is NULL, only append its key.
|
danielebarchiesi@0
|
497 elseif (!isset($value)) {
|
danielebarchiesi@0
|
498 $params[] = $key;
|
danielebarchiesi@0
|
499 }
|
danielebarchiesi@0
|
500 else {
|
danielebarchiesi@0
|
501 // For better readability of paths in query strings, we decode slashes.
|
danielebarchiesi@0
|
502 $params[] = $key . '=' . str_replace('%2F', '/', rawurlencode($value));
|
danielebarchiesi@0
|
503 }
|
danielebarchiesi@0
|
504 }
|
danielebarchiesi@0
|
505
|
danielebarchiesi@0
|
506 return implode('&', $params);
|
danielebarchiesi@0
|
507 }
|
danielebarchiesi@0
|
508
|
danielebarchiesi@0
|
509 /**
|
danielebarchiesi@0
|
510 * Prepares a 'destination' URL query parameter for use with drupal_goto().
|
danielebarchiesi@0
|
511 *
|
danielebarchiesi@0
|
512 * Used to direct the user back to the referring page after completing a form.
|
danielebarchiesi@0
|
513 * By default the current URL is returned. If a destination exists in the
|
danielebarchiesi@0
|
514 * previous request, that destination is returned. As such, a destination can
|
danielebarchiesi@0
|
515 * persist across multiple pages.
|
danielebarchiesi@0
|
516 *
|
danielebarchiesi@0
|
517 * @return
|
danielebarchiesi@0
|
518 * An associative array containing the key:
|
danielebarchiesi@0
|
519 * - destination: The path provided via the destination query string or, if
|
danielebarchiesi@0
|
520 * not available, the current path.
|
danielebarchiesi@0
|
521 *
|
danielebarchiesi@0
|
522 * @see current_path()
|
danielebarchiesi@0
|
523 * @see drupal_goto()
|
danielebarchiesi@0
|
524 */
|
danielebarchiesi@0
|
525 function drupal_get_destination() {
|
danielebarchiesi@0
|
526 $destination = &drupal_static(__FUNCTION__);
|
danielebarchiesi@0
|
527
|
danielebarchiesi@0
|
528 if (isset($destination)) {
|
danielebarchiesi@0
|
529 return $destination;
|
danielebarchiesi@0
|
530 }
|
danielebarchiesi@0
|
531
|
danielebarchiesi@0
|
532 if (isset($_GET['destination'])) {
|
danielebarchiesi@0
|
533 $destination = array('destination' => $_GET['destination']);
|
danielebarchiesi@0
|
534 }
|
danielebarchiesi@0
|
535 else {
|
danielebarchiesi@0
|
536 $path = $_GET['q'];
|
danielebarchiesi@0
|
537 $query = drupal_http_build_query(drupal_get_query_parameters());
|
danielebarchiesi@0
|
538 if ($query != '') {
|
danielebarchiesi@0
|
539 $path .= '?' . $query;
|
danielebarchiesi@0
|
540 }
|
danielebarchiesi@0
|
541 $destination = array('destination' => $path);
|
danielebarchiesi@0
|
542 }
|
danielebarchiesi@0
|
543 return $destination;
|
danielebarchiesi@0
|
544 }
|
danielebarchiesi@0
|
545
|
danielebarchiesi@0
|
546 /**
|
danielebarchiesi@0
|
547 * Parses a system URL string into an associative array suitable for url().
|
danielebarchiesi@0
|
548 *
|
danielebarchiesi@0
|
549 * This function should only be used for URLs that have been generated by the
|
danielebarchiesi@0
|
550 * system, such as via url(). It should not be used for URLs that come from
|
danielebarchiesi@0
|
551 * external sources, or URLs that link to external resources.
|
danielebarchiesi@0
|
552 *
|
danielebarchiesi@0
|
553 * The returned array contains a 'path' that may be passed separately to url().
|
danielebarchiesi@0
|
554 * For example:
|
danielebarchiesi@0
|
555 * @code
|
danielebarchiesi@0
|
556 * $options = drupal_parse_url($_GET['destination']);
|
danielebarchiesi@0
|
557 * $my_url = url($options['path'], $options);
|
danielebarchiesi@0
|
558 * $my_link = l('Example link', $options['path'], $options);
|
danielebarchiesi@0
|
559 * @endcode
|
danielebarchiesi@0
|
560 *
|
danielebarchiesi@0
|
561 * This is required, because url() does not support relative URLs containing a
|
danielebarchiesi@0
|
562 * query string or fragment in its $path argument. Instead, any query string
|
danielebarchiesi@0
|
563 * needs to be parsed into an associative query parameter array in
|
danielebarchiesi@0
|
564 * $options['query'] and the fragment into $options['fragment'].
|
danielebarchiesi@0
|
565 *
|
danielebarchiesi@0
|
566 * @param $url
|
danielebarchiesi@0
|
567 * The URL string to parse, f.e. $_GET['destination'].
|
danielebarchiesi@0
|
568 *
|
danielebarchiesi@0
|
569 * @return
|
danielebarchiesi@0
|
570 * An associative array containing the keys:
|
danielebarchiesi@0
|
571 * - 'path': The path of the URL. If the given $url is external, this includes
|
danielebarchiesi@0
|
572 * the scheme and host.
|
danielebarchiesi@0
|
573 * - 'query': An array of query parameters of $url, if existent.
|
danielebarchiesi@0
|
574 * - 'fragment': The fragment of $url, if existent.
|
danielebarchiesi@0
|
575 *
|
danielebarchiesi@0
|
576 * @see url()
|
danielebarchiesi@0
|
577 * @see drupal_goto()
|
danielebarchiesi@0
|
578 * @ingroup php_wrappers
|
danielebarchiesi@0
|
579 */
|
danielebarchiesi@0
|
580 function drupal_parse_url($url) {
|
danielebarchiesi@0
|
581 $options = array(
|
danielebarchiesi@0
|
582 'path' => NULL,
|
danielebarchiesi@0
|
583 'query' => array(),
|
danielebarchiesi@0
|
584 'fragment' => '',
|
danielebarchiesi@0
|
585 );
|
danielebarchiesi@0
|
586
|
danielebarchiesi@0
|
587 // External URLs: not using parse_url() here, so we do not have to rebuild
|
danielebarchiesi@0
|
588 // the scheme, host, and path without having any use for it.
|
danielebarchiesi@0
|
589 if (strpos($url, '://') !== FALSE) {
|
danielebarchiesi@0
|
590 // Split off everything before the query string into 'path'.
|
danielebarchiesi@0
|
591 $parts = explode('?', $url);
|
danielebarchiesi@0
|
592 $options['path'] = $parts[0];
|
danielebarchiesi@0
|
593 // If there is a query string, transform it into keyed query parameters.
|
danielebarchiesi@0
|
594 if (isset($parts[1])) {
|
danielebarchiesi@0
|
595 $query_parts = explode('#', $parts[1]);
|
danielebarchiesi@0
|
596 parse_str($query_parts[0], $options['query']);
|
danielebarchiesi@0
|
597 // Take over the fragment, if there is any.
|
danielebarchiesi@0
|
598 if (isset($query_parts[1])) {
|
danielebarchiesi@0
|
599 $options['fragment'] = $query_parts[1];
|
danielebarchiesi@0
|
600 }
|
danielebarchiesi@0
|
601 }
|
danielebarchiesi@0
|
602 }
|
danielebarchiesi@0
|
603 // Internal URLs.
|
danielebarchiesi@0
|
604 else {
|
danielebarchiesi@0
|
605 // parse_url() does not support relative URLs, so make it absolute. E.g. the
|
danielebarchiesi@0
|
606 // relative URL "foo/bar:1" isn't properly parsed.
|
danielebarchiesi@0
|
607 $parts = parse_url('http://example.com/' . $url);
|
danielebarchiesi@0
|
608 // Strip the leading slash that was just added.
|
danielebarchiesi@0
|
609 $options['path'] = substr($parts['path'], 1);
|
danielebarchiesi@0
|
610 if (isset($parts['query'])) {
|
danielebarchiesi@0
|
611 parse_str($parts['query'], $options['query']);
|
danielebarchiesi@0
|
612 }
|
danielebarchiesi@0
|
613 if (isset($parts['fragment'])) {
|
danielebarchiesi@0
|
614 $options['fragment'] = $parts['fragment'];
|
danielebarchiesi@0
|
615 }
|
danielebarchiesi@0
|
616 }
|
danielebarchiesi@0
|
617 // The 'q' parameter contains the path of the current page if clean URLs are
|
danielebarchiesi@0
|
618 // disabled. It overrides the 'path' of the URL when present, even if clean
|
danielebarchiesi@0
|
619 // URLs are enabled, due to how Apache rewriting rules work.
|
danielebarchiesi@0
|
620 if (isset($options['query']['q'])) {
|
danielebarchiesi@0
|
621 $options['path'] = $options['query']['q'];
|
danielebarchiesi@0
|
622 unset($options['query']['q']);
|
danielebarchiesi@0
|
623 }
|
danielebarchiesi@0
|
624
|
danielebarchiesi@0
|
625 return $options;
|
danielebarchiesi@0
|
626 }
|
danielebarchiesi@0
|
627
|
danielebarchiesi@0
|
628 /**
|
danielebarchiesi@0
|
629 * Encodes a Drupal path for use in a URL.
|
danielebarchiesi@0
|
630 *
|
danielebarchiesi@0
|
631 * For aesthetic reasons slashes are not escaped.
|
danielebarchiesi@0
|
632 *
|
danielebarchiesi@0
|
633 * Note that url() takes care of calling this function, so a path passed to that
|
danielebarchiesi@0
|
634 * function should not be encoded in advance.
|
danielebarchiesi@0
|
635 *
|
danielebarchiesi@0
|
636 * @param $path
|
danielebarchiesi@0
|
637 * The Drupal path to encode.
|
danielebarchiesi@0
|
638 */
|
danielebarchiesi@0
|
639 function drupal_encode_path($path) {
|
danielebarchiesi@0
|
640 return str_replace('%2F', '/', rawurlencode($path));
|
danielebarchiesi@0
|
641 }
|
danielebarchiesi@0
|
642
|
danielebarchiesi@0
|
643 /**
|
danielebarchiesi@0
|
644 * Sends the user to a different page.
|
danielebarchiesi@0
|
645 *
|
danielebarchiesi@0
|
646 * This issues an on-site HTTP redirect. The function makes sure the redirected
|
danielebarchiesi@0
|
647 * URL is formatted correctly.
|
danielebarchiesi@0
|
648 *
|
danielebarchiesi@0
|
649 * Usually the redirected URL is constructed from this function's input
|
danielebarchiesi@0
|
650 * parameters. However you may override that behavior by setting a
|
danielebarchiesi@0
|
651 * destination in either the $_REQUEST-array (i.e. by using
|
danielebarchiesi@0
|
652 * the query string of an URI) This is used to direct the user back to
|
danielebarchiesi@0
|
653 * the proper page after completing a form. For example, after editing
|
danielebarchiesi@0
|
654 * a post on the 'admin/content'-page or after having logged on using the
|
danielebarchiesi@0
|
655 * 'user login'-block in a sidebar. The function drupal_get_destination()
|
danielebarchiesi@0
|
656 * can be used to help set the destination URL.
|
danielebarchiesi@0
|
657 *
|
danielebarchiesi@0
|
658 * Drupal will ensure that messages set by drupal_set_message() and other
|
danielebarchiesi@0
|
659 * session data are written to the database before the user is redirected.
|
danielebarchiesi@0
|
660 *
|
danielebarchiesi@0
|
661 * This function ends the request; use it instead of a return in your menu
|
danielebarchiesi@0
|
662 * callback.
|
danielebarchiesi@0
|
663 *
|
danielebarchiesi@0
|
664 * @param $path
|
danielebarchiesi@0
|
665 * (optional) A Drupal path or a full URL, which will be passed to url() to
|
danielebarchiesi@0
|
666 * compute the redirect for the URL.
|
danielebarchiesi@0
|
667 * @param $options
|
danielebarchiesi@0
|
668 * (optional) An associative array of additional URL options to pass to url().
|
danielebarchiesi@0
|
669 * @param $http_response_code
|
danielebarchiesi@0
|
670 * (optional) The HTTP status code to use for the redirection, defaults to
|
danielebarchiesi@0
|
671 * 302. The valid values for 3xx redirection status codes are defined in
|
danielebarchiesi@0
|
672 * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3 RFC 2616 @endlink
|
danielebarchiesi@0
|
673 * and the
|
danielebarchiesi@0
|
674 * @link http://tools.ietf.org/html/draft-reschke-http-status-308-07 draft for the new HTTP status codes: @endlink
|
danielebarchiesi@0
|
675 * - 301: Moved Permanently (the recommended value for most redirects).
|
danielebarchiesi@0
|
676 * - 302: Found (default in Drupal and PHP, sometimes used for spamming search
|
danielebarchiesi@0
|
677 * engines).
|
danielebarchiesi@0
|
678 * - 303: See Other.
|
danielebarchiesi@0
|
679 * - 304: Not Modified.
|
danielebarchiesi@0
|
680 * - 305: Use Proxy.
|
danielebarchiesi@0
|
681 * - 307: Temporary Redirect.
|
danielebarchiesi@0
|
682 *
|
danielebarchiesi@0
|
683 * @see drupal_get_destination()
|
danielebarchiesi@0
|
684 * @see url()
|
danielebarchiesi@0
|
685 */
|
danielebarchiesi@0
|
686 function drupal_goto($path = '', array $options = array(), $http_response_code = 302) {
|
danielebarchiesi@0
|
687 // A destination in $_GET always overrides the function arguments.
|
danielebarchiesi@0
|
688 // We do not allow absolute URLs to be passed via $_GET, as this can be an attack vector.
|
danielebarchiesi@0
|
689 if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
|
danielebarchiesi@0
|
690 $destination = drupal_parse_url($_GET['destination']);
|
danielebarchiesi@0
|
691 $path = $destination['path'];
|
danielebarchiesi@0
|
692 $options['query'] = $destination['query'];
|
danielebarchiesi@0
|
693 $options['fragment'] = $destination['fragment'];
|
danielebarchiesi@0
|
694 }
|
danielebarchiesi@0
|
695
|
danielebarchiesi@0
|
696 drupal_alter('drupal_goto', $path, $options, $http_response_code);
|
danielebarchiesi@0
|
697
|
danielebarchiesi@0
|
698 // The 'Location' HTTP header must be absolute.
|
danielebarchiesi@0
|
699 $options['absolute'] = TRUE;
|
danielebarchiesi@0
|
700
|
danielebarchiesi@0
|
701 $url = url($path, $options);
|
danielebarchiesi@0
|
702
|
danielebarchiesi@0
|
703 header('Location: ' . $url, TRUE, $http_response_code);
|
danielebarchiesi@0
|
704
|
danielebarchiesi@0
|
705 // The "Location" header sends a redirect status code to the HTTP daemon. In
|
danielebarchiesi@0
|
706 // some cases this can be wrong, so we make sure none of the code below the
|
danielebarchiesi@0
|
707 // drupal_goto() call gets executed upon redirection.
|
danielebarchiesi@0
|
708 drupal_exit($url);
|
danielebarchiesi@0
|
709 }
|
danielebarchiesi@0
|
710
|
danielebarchiesi@0
|
711 /**
|
danielebarchiesi@0
|
712 * Delivers a "site is under maintenance" message to the browser.
|
danielebarchiesi@0
|
713 *
|
danielebarchiesi@0
|
714 * Page callback functions wanting to report a "site offline" message should
|
danielebarchiesi@0
|
715 * return MENU_SITE_OFFLINE instead of calling drupal_site_offline(). However,
|
danielebarchiesi@0
|
716 * functions that are invoked in contexts where that return value might not
|
danielebarchiesi@0
|
717 * bubble up to menu_execute_active_handler() should call drupal_site_offline().
|
danielebarchiesi@0
|
718 */
|
danielebarchiesi@0
|
719 function drupal_site_offline() {
|
danielebarchiesi@0
|
720 drupal_deliver_page(MENU_SITE_OFFLINE);
|
danielebarchiesi@0
|
721 }
|
danielebarchiesi@0
|
722
|
danielebarchiesi@0
|
723 /**
|
danielebarchiesi@0
|
724 * Delivers a "page not found" error to the browser.
|
danielebarchiesi@0
|
725 *
|
danielebarchiesi@0
|
726 * Page callback functions wanting to report a "page not found" message should
|
danielebarchiesi@0
|
727 * return MENU_NOT_FOUND instead of calling drupal_not_found(). However,
|
danielebarchiesi@0
|
728 * functions that are invoked in contexts where that return value might not
|
danielebarchiesi@0
|
729 * bubble up to menu_execute_active_handler() should call drupal_not_found().
|
danielebarchiesi@0
|
730 */
|
danielebarchiesi@0
|
731 function drupal_not_found() {
|
danielebarchiesi@0
|
732 drupal_deliver_page(MENU_NOT_FOUND);
|
danielebarchiesi@0
|
733 }
|
danielebarchiesi@0
|
734
|
danielebarchiesi@0
|
735 /**
|
danielebarchiesi@0
|
736 * Delivers an "access denied" error to the browser.
|
danielebarchiesi@0
|
737 *
|
danielebarchiesi@0
|
738 * Page callback functions wanting to report an "access denied" message should
|
danielebarchiesi@0
|
739 * return MENU_ACCESS_DENIED instead of calling drupal_access_denied(). However,
|
danielebarchiesi@0
|
740 * functions that are invoked in contexts where that return value might not
|
danielebarchiesi@0
|
741 * bubble up to menu_execute_active_handler() should call
|
danielebarchiesi@0
|
742 * drupal_access_denied().
|
danielebarchiesi@0
|
743 */
|
danielebarchiesi@0
|
744 function drupal_access_denied() {
|
danielebarchiesi@0
|
745 drupal_deliver_page(MENU_ACCESS_DENIED);
|
danielebarchiesi@0
|
746 }
|
danielebarchiesi@0
|
747
|
danielebarchiesi@0
|
748 /**
|
danielebarchiesi@0
|
749 * Performs an HTTP request.
|
danielebarchiesi@0
|
750 *
|
danielebarchiesi@0
|
751 * This is a flexible and powerful HTTP client implementation. Correctly
|
danielebarchiesi@0
|
752 * handles GET, POST, PUT or any other HTTP requests. Handles redirects.
|
danielebarchiesi@0
|
753 *
|
danielebarchiesi@0
|
754 * @param $url
|
danielebarchiesi@0
|
755 * A string containing a fully qualified URI.
|
danielebarchiesi@0
|
756 * @param array $options
|
danielebarchiesi@0
|
757 * (optional) An array that can have one or more of the following elements:
|
danielebarchiesi@0
|
758 * - headers: An array containing request headers to send as name/value pairs.
|
danielebarchiesi@0
|
759 * - method: A string containing the request method. Defaults to 'GET'.
|
danielebarchiesi@0
|
760 * - data: A string containing the request body, formatted as
|
danielebarchiesi@0
|
761 * 'param=value¶m=value&...'. Defaults to NULL.
|
danielebarchiesi@0
|
762 * - max_redirects: An integer representing how many times a redirect
|
danielebarchiesi@0
|
763 * may be followed. Defaults to 3.
|
danielebarchiesi@0
|
764 * - timeout: A float representing the maximum number of seconds the function
|
danielebarchiesi@0
|
765 * call may take. The default is 30 seconds. If a timeout occurs, the error
|
danielebarchiesi@0
|
766 * code is set to the HTTP_REQUEST_TIMEOUT constant.
|
danielebarchiesi@0
|
767 * - context: A context resource created with stream_context_create().
|
danielebarchiesi@0
|
768 *
|
danielebarchiesi@0
|
769 * @return object
|
danielebarchiesi@0
|
770 * An object that can have one or more of the following components:
|
danielebarchiesi@0
|
771 * - request: A string containing the request body that was sent.
|
danielebarchiesi@0
|
772 * - code: An integer containing the response status code, or the error code
|
danielebarchiesi@0
|
773 * if an error occurred.
|
danielebarchiesi@0
|
774 * - protocol: The response protocol (e.g. HTTP/1.1 or HTTP/1.0).
|
danielebarchiesi@0
|
775 * - status_message: The status message from the response, if a response was
|
danielebarchiesi@0
|
776 * received.
|
danielebarchiesi@0
|
777 * - redirect_code: If redirected, an integer containing the initial response
|
danielebarchiesi@0
|
778 * status code.
|
danielebarchiesi@0
|
779 * - redirect_url: If redirected, a string containing the URL of the redirect
|
danielebarchiesi@0
|
780 * target.
|
danielebarchiesi@0
|
781 * - error: If an error occurred, the error message. Otherwise not set.
|
danielebarchiesi@0
|
782 * - headers: An array containing the response headers as name/value pairs.
|
danielebarchiesi@0
|
783 * HTTP header names are case-insensitive (RFC 2616, section 4.2), so for
|
danielebarchiesi@0
|
784 * easy access the array keys are returned in lower case.
|
danielebarchiesi@0
|
785 * - data: A string containing the response body that was received.
|
danielebarchiesi@0
|
786 */
|
danielebarchiesi@0
|
787 function drupal_http_request($url, array $options = array()) {
|
danielebarchiesi@0
|
788 // Allow an alternate HTTP client library to replace Drupal's default
|
danielebarchiesi@0
|
789 // implementation.
|
danielebarchiesi@0
|
790 $override_function = variable_get('drupal_http_request_function', FALSE);
|
danielebarchiesi@0
|
791 if (!empty($override_function) && function_exists($override_function)) {
|
danielebarchiesi@0
|
792 return $override_function($url, $options);
|
danielebarchiesi@0
|
793 }
|
danielebarchiesi@0
|
794
|
danielebarchiesi@0
|
795 $result = new stdClass();
|
danielebarchiesi@0
|
796
|
danielebarchiesi@0
|
797 // Parse the URL and make sure we can handle the schema.
|
danielebarchiesi@0
|
798 $uri = @parse_url($url);
|
danielebarchiesi@0
|
799
|
danielebarchiesi@0
|
800 if ($uri == FALSE) {
|
danielebarchiesi@0
|
801 $result->error = 'unable to parse URL';
|
danielebarchiesi@0
|
802 $result->code = -1001;
|
danielebarchiesi@0
|
803 return $result;
|
danielebarchiesi@0
|
804 }
|
danielebarchiesi@0
|
805
|
danielebarchiesi@0
|
806 if (!isset($uri['scheme'])) {
|
danielebarchiesi@0
|
807 $result->error = 'missing schema';
|
danielebarchiesi@0
|
808 $result->code = -1002;
|
danielebarchiesi@0
|
809 return $result;
|
danielebarchiesi@0
|
810 }
|
danielebarchiesi@0
|
811
|
danielebarchiesi@0
|
812 timer_start(__FUNCTION__);
|
danielebarchiesi@0
|
813
|
danielebarchiesi@0
|
814 // Merge the default options.
|
danielebarchiesi@0
|
815 $options += array(
|
danielebarchiesi@0
|
816 'headers' => array(),
|
danielebarchiesi@0
|
817 'method' => 'GET',
|
danielebarchiesi@0
|
818 'data' => NULL,
|
danielebarchiesi@0
|
819 'max_redirects' => 3,
|
danielebarchiesi@0
|
820 'timeout' => 30.0,
|
danielebarchiesi@0
|
821 'context' => NULL,
|
danielebarchiesi@0
|
822 );
|
danielebarchiesi@0
|
823
|
danielebarchiesi@0
|
824 // Merge the default headers.
|
danielebarchiesi@0
|
825 $options['headers'] += array(
|
danielebarchiesi@0
|
826 'User-Agent' => 'Drupal (+http://drupal.org/)',
|
danielebarchiesi@0
|
827 );
|
danielebarchiesi@0
|
828
|
danielebarchiesi@0
|
829 // stream_socket_client() requires timeout to be a float.
|
danielebarchiesi@0
|
830 $options['timeout'] = (float) $options['timeout'];
|
danielebarchiesi@0
|
831
|
danielebarchiesi@0
|
832 // Use a proxy if one is defined and the host is not on the excluded list.
|
danielebarchiesi@0
|
833 $proxy_server = variable_get('proxy_server', '');
|
danielebarchiesi@0
|
834 if ($proxy_server && _drupal_http_use_proxy($uri['host'])) {
|
danielebarchiesi@0
|
835 // Set the scheme so we open a socket to the proxy server.
|
danielebarchiesi@0
|
836 $uri['scheme'] = 'proxy';
|
danielebarchiesi@0
|
837 // Set the path to be the full URL.
|
danielebarchiesi@0
|
838 $uri['path'] = $url;
|
danielebarchiesi@0
|
839 // Since the URL is passed as the path, we won't use the parsed query.
|
danielebarchiesi@0
|
840 unset($uri['query']);
|
danielebarchiesi@0
|
841
|
danielebarchiesi@0
|
842 // Add in username and password to Proxy-Authorization header if needed.
|
danielebarchiesi@0
|
843 if ($proxy_username = variable_get('proxy_username', '')) {
|
danielebarchiesi@0
|
844 $proxy_password = variable_get('proxy_password', '');
|
danielebarchiesi@0
|
845 $options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : ''));
|
danielebarchiesi@0
|
846 }
|
danielebarchiesi@0
|
847 // Some proxies reject requests with any User-Agent headers, while others
|
danielebarchiesi@0
|
848 // require a specific one.
|
danielebarchiesi@0
|
849 $proxy_user_agent = variable_get('proxy_user_agent', '');
|
danielebarchiesi@0
|
850 // The default value matches neither condition.
|
danielebarchiesi@0
|
851 if ($proxy_user_agent === NULL) {
|
danielebarchiesi@0
|
852 unset($options['headers']['User-Agent']);
|
danielebarchiesi@0
|
853 }
|
danielebarchiesi@0
|
854 elseif ($proxy_user_agent) {
|
danielebarchiesi@0
|
855 $options['headers']['User-Agent'] = $proxy_user_agent;
|
danielebarchiesi@0
|
856 }
|
danielebarchiesi@0
|
857 }
|
danielebarchiesi@0
|
858
|
danielebarchiesi@0
|
859 switch ($uri['scheme']) {
|
danielebarchiesi@0
|
860 case 'proxy':
|
danielebarchiesi@0
|
861 // Make the socket connection to a proxy server.
|
danielebarchiesi@0
|
862 $socket = 'tcp://' . $proxy_server . ':' . variable_get('proxy_port', 8080);
|
danielebarchiesi@0
|
863 // The Host header still needs to match the real request.
|
danielebarchiesi@0
|
864 $options['headers']['Host'] = $uri['host'];
|
danielebarchiesi@0
|
865 $options['headers']['Host'] .= isset($uri['port']) && $uri['port'] != 80 ? ':' . $uri['port'] : '';
|
danielebarchiesi@0
|
866 break;
|
danielebarchiesi@0
|
867
|
danielebarchiesi@0
|
868 case 'http':
|
danielebarchiesi@0
|
869 case 'feed':
|
danielebarchiesi@0
|
870 $port = isset($uri['port']) ? $uri['port'] : 80;
|
danielebarchiesi@0
|
871 $socket = 'tcp://' . $uri['host'] . ':' . $port;
|
danielebarchiesi@0
|
872 // RFC 2616: "non-standard ports MUST, default ports MAY be included".
|
danielebarchiesi@0
|
873 // We don't add the standard port to prevent from breaking rewrite rules
|
danielebarchiesi@0
|
874 // checking the host that do not take into account the port number.
|
danielebarchiesi@0
|
875 $options['headers']['Host'] = $uri['host'] . ($port != 80 ? ':' . $port : '');
|
danielebarchiesi@0
|
876 break;
|
danielebarchiesi@0
|
877
|
danielebarchiesi@0
|
878 case 'https':
|
danielebarchiesi@0
|
879 // Note: Only works when PHP is compiled with OpenSSL support.
|
danielebarchiesi@0
|
880 $port = isset($uri['port']) ? $uri['port'] : 443;
|
danielebarchiesi@0
|
881 $socket = 'ssl://' . $uri['host'] . ':' . $port;
|
danielebarchiesi@0
|
882 $options['headers']['Host'] = $uri['host'] . ($port != 443 ? ':' . $port : '');
|
danielebarchiesi@0
|
883 break;
|
danielebarchiesi@0
|
884
|
danielebarchiesi@0
|
885 default:
|
danielebarchiesi@0
|
886 $result->error = 'invalid schema ' . $uri['scheme'];
|
danielebarchiesi@0
|
887 $result->code = -1003;
|
danielebarchiesi@0
|
888 return $result;
|
danielebarchiesi@0
|
889 }
|
danielebarchiesi@0
|
890
|
danielebarchiesi@0
|
891 if (empty($options['context'])) {
|
danielebarchiesi@0
|
892 $fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout']);
|
danielebarchiesi@0
|
893 }
|
danielebarchiesi@0
|
894 else {
|
danielebarchiesi@0
|
895 // Create a stream with context. Allows verification of a SSL certificate.
|
danielebarchiesi@0
|
896 $fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $options['context']);
|
danielebarchiesi@0
|
897 }
|
danielebarchiesi@0
|
898
|
danielebarchiesi@0
|
899 // Make sure the socket opened properly.
|
danielebarchiesi@0
|
900 if (!$fp) {
|
danielebarchiesi@0
|
901 // When a network error occurs, we use a negative number so it does not
|
danielebarchiesi@0
|
902 // clash with the HTTP status codes.
|
danielebarchiesi@0
|
903 $result->code = -$errno;
|
danielebarchiesi@0
|
904 $result->error = trim($errstr) ? trim($errstr) : t('Error opening socket @socket', array('@socket' => $socket));
|
danielebarchiesi@0
|
905
|
danielebarchiesi@0
|
906 // Mark that this request failed. This will trigger a check of the web
|
danielebarchiesi@0
|
907 // server's ability to make outgoing HTTP requests the next time that
|
danielebarchiesi@0
|
908 // requirements checking is performed.
|
danielebarchiesi@0
|
909 // See system_requirements().
|
danielebarchiesi@0
|
910 variable_set('drupal_http_request_fails', TRUE);
|
danielebarchiesi@0
|
911
|
danielebarchiesi@0
|
912 return $result;
|
danielebarchiesi@0
|
913 }
|
danielebarchiesi@0
|
914
|
danielebarchiesi@0
|
915 // Construct the path to act on.
|
danielebarchiesi@0
|
916 $path = isset($uri['path']) ? $uri['path'] : '/';
|
danielebarchiesi@0
|
917 if (isset($uri['query'])) {
|
danielebarchiesi@0
|
918 $path .= '?' . $uri['query'];
|
danielebarchiesi@0
|
919 }
|
danielebarchiesi@0
|
920
|
danielebarchiesi@0
|
921 // Only add Content-Length if we actually have any content or if it is a POST
|
danielebarchiesi@0
|
922 // or PUT request. Some non-standard servers get confused by Content-Length in
|
danielebarchiesi@0
|
923 // at least HEAD/GET requests, and Squid always requires Content-Length in
|
danielebarchiesi@0
|
924 // POST/PUT requests.
|
danielebarchiesi@0
|
925 $content_length = strlen($options['data']);
|
danielebarchiesi@0
|
926 if ($content_length > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') {
|
danielebarchiesi@0
|
927 $options['headers']['Content-Length'] = $content_length;
|
danielebarchiesi@0
|
928 }
|
danielebarchiesi@0
|
929
|
danielebarchiesi@0
|
930 // If the server URL has a user then attempt to use basic authentication.
|
danielebarchiesi@0
|
931 if (isset($uri['user'])) {
|
danielebarchiesi@0
|
932 $options['headers']['Authorization'] = 'Basic ' . base64_encode($uri['user'] . (isset($uri['pass']) ? ':' . $uri['pass'] : ''));
|
danielebarchiesi@0
|
933 }
|
danielebarchiesi@0
|
934
|
danielebarchiesi@0
|
935 // If the database prefix is being used by SimpleTest to run the tests in a copied
|
danielebarchiesi@0
|
936 // database then set the user-agent header to the database prefix so that any
|
danielebarchiesi@0
|
937 // calls to other Drupal pages will run the SimpleTest prefixed database. The
|
danielebarchiesi@0
|
938 // user-agent is used to ensure that multiple testing sessions running at the
|
danielebarchiesi@0
|
939 // same time won't interfere with each other as they would if the database
|
danielebarchiesi@0
|
940 // prefix were stored statically in a file or database variable.
|
danielebarchiesi@0
|
941 $test_info = &$GLOBALS['drupal_test_info'];
|
danielebarchiesi@0
|
942 if (!empty($test_info['test_run_id'])) {
|
danielebarchiesi@0
|
943 $options['headers']['User-Agent'] = drupal_generate_test_ua($test_info['test_run_id']);
|
danielebarchiesi@0
|
944 }
|
danielebarchiesi@0
|
945
|
danielebarchiesi@0
|
946 $request = $options['method'] . ' ' . $path . " HTTP/1.0\r\n";
|
danielebarchiesi@0
|
947 foreach ($options['headers'] as $name => $value) {
|
danielebarchiesi@0
|
948 $request .= $name . ': ' . trim($value) . "\r\n";
|
danielebarchiesi@0
|
949 }
|
danielebarchiesi@0
|
950 $request .= "\r\n" . $options['data'];
|
danielebarchiesi@0
|
951 $result->request = $request;
|
danielebarchiesi@0
|
952 // Calculate how much time is left of the original timeout value.
|
danielebarchiesi@0
|
953 $timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
|
danielebarchiesi@0
|
954 if ($timeout > 0) {
|
danielebarchiesi@0
|
955 stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
|
danielebarchiesi@0
|
956 fwrite($fp, $request);
|
danielebarchiesi@0
|
957 }
|
danielebarchiesi@0
|
958
|
danielebarchiesi@0
|
959 // Fetch response. Due to PHP bugs like http://bugs.php.net/bug.php?id=43782
|
danielebarchiesi@0
|
960 // and http://bugs.php.net/bug.php?id=46049 we can't rely on feof(), but
|
danielebarchiesi@0
|
961 // instead must invoke stream_get_meta_data() each iteration.
|
danielebarchiesi@0
|
962 $info = stream_get_meta_data($fp);
|
danielebarchiesi@0
|
963 $alive = !$info['eof'] && !$info['timed_out'];
|
danielebarchiesi@0
|
964 $response = '';
|
danielebarchiesi@0
|
965
|
danielebarchiesi@0
|
966 while ($alive) {
|
danielebarchiesi@0
|
967 // Calculate how much time is left of the original timeout value.
|
danielebarchiesi@0
|
968 $timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
|
danielebarchiesi@0
|
969 if ($timeout <= 0) {
|
danielebarchiesi@0
|
970 $info['timed_out'] = TRUE;
|
danielebarchiesi@0
|
971 break;
|
danielebarchiesi@0
|
972 }
|
danielebarchiesi@0
|
973 stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
|
danielebarchiesi@0
|
974 $chunk = fread($fp, 1024);
|
danielebarchiesi@0
|
975 $response .= $chunk;
|
danielebarchiesi@0
|
976 $info = stream_get_meta_data($fp);
|
danielebarchiesi@0
|
977 $alive = !$info['eof'] && !$info['timed_out'] && $chunk;
|
danielebarchiesi@0
|
978 }
|
danielebarchiesi@0
|
979 fclose($fp);
|
danielebarchiesi@0
|
980
|
danielebarchiesi@0
|
981 if ($info['timed_out']) {
|
danielebarchiesi@0
|
982 $result->code = HTTP_REQUEST_TIMEOUT;
|
danielebarchiesi@0
|
983 $result->error = 'request timed out';
|
danielebarchiesi@0
|
984 return $result;
|
danielebarchiesi@0
|
985 }
|
danielebarchiesi@0
|
986 // Parse response headers from the response body.
|
danielebarchiesi@0
|
987 // Be tolerant of malformed HTTP responses that separate header and body with
|
danielebarchiesi@0
|
988 // \n\n or \r\r instead of \r\n\r\n.
|
danielebarchiesi@0
|
989 list($response, $result->data) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
|
danielebarchiesi@0
|
990 $response = preg_split("/\r\n|\n|\r/", $response);
|
danielebarchiesi@0
|
991
|
danielebarchiesi@0
|
992 // Parse the response status line.
|
danielebarchiesi@0
|
993 list($protocol, $code, $status_message) = explode(' ', trim(array_shift($response)), 3);
|
danielebarchiesi@0
|
994 $result->protocol = $protocol;
|
danielebarchiesi@0
|
995 $result->status_message = $status_message;
|
danielebarchiesi@0
|
996
|
danielebarchiesi@0
|
997 $result->headers = array();
|
danielebarchiesi@0
|
998
|
danielebarchiesi@0
|
999 // Parse the response headers.
|
danielebarchiesi@0
|
1000 while ($line = trim(array_shift($response))) {
|
danielebarchiesi@0
|
1001 list($name, $value) = explode(':', $line, 2);
|
danielebarchiesi@0
|
1002 $name = strtolower($name);
|
danielebarchiesi@0
|
1003 if (isset($result->headers[$name]) && $name == 'set-cookie') {
|
danielebarchiesi@0
|
1004 // RFC 2109: the Set-Cookie response header comprises the token Set-
|
danielebarchiesi@0
|
1005 // Cookie:, followed by a comma-separated list of one or more cookies.
|
danielebarchiesi@0
|
1006 $result->headers[$name] .= ',' . trim($value);
|
danielebarchiesi@0
|
1007 }
|
danielebarchiesi@0
|
1008 else {
|
danielebarchiesi@0
|
1009 $result->headers[$name] = trim($value);
|
danielebarchiesi@0
|
1010 }
|
danielebarchiesi@0
|
1011 }
|
danielebarchiesi@0
|
1012
|
danielebarchiesi@0
|
1013 $responses = array(
|
danielebarchiesi@0
|
1014 100 => 'Continue',
|
danielebarchiesi@0
|
1015 101 => 'Switching Protocols',
|
danielebarchiesi@0
|
1016 200 => 'OK',
|
danielebarchiesi@0
|
1017 201 => 'Created',
|
danielebarchiesi@0
|
1018 202 => 'Accepted',
|
danielebarchiesi@0
|
1019 203 => 'Non-Authoritative Information',
|
danielebarchiesi@0
|
1020 204 => 'No Content',
|
danielebarchiesi@0
|
1021 205 => 'Reset Content',
|
danielebarchiesi@0
|
1022 206 => 'Partial Content',
|
danielebarchiesi@0
|
1023 300 => 'Multiple Choices',
|
danielebarchiesi@0
|
1024 301 => 'Moved Permanently',
|
danielebarchiesi@0
|
1025 302 => 'Found',
|
danielebarchiesi@0
|
1026 303 => 'See Other',
|
danielebarchiesi@0
|
1027 304 => 'Not Modified',
|
danielebarchiesi@0
|
1028 305 => 'Use Proxy',
|
danielebarchiesi@0
|
1029 307 => 'Temporary Redirect',
|
danielebarchiesi@0
|
1030 400 => 'Bad Request',
|
danielebarchiesi@0
|
1031 401 => 'Unauthorized',
|
danielebarchiesi@0
|
1032 402 => 'Payment Required',
|
danielebarchiesi@0
|
1033 403 => 'Forbidden',
|
danielebarchiesi@0
|
1034 404 => 'Not Found',
|
danielebarchiesi@0
|
1035 405 => 'Method Not Allowed',
|
danielebarchiesi@0
|
1036 406 => 'Not Acceptable',
|
danielebarchiesi@0
|
1037 407 => 'Proxy Authentication Required',
|
danielebarchiesi@0
|
1038 408 => 'Request Time-out',
|
danielebarchiesi@0
|
1039 409 => 'Conflict',
|
danielebarchiesi@0
|
1040 410 => 'Gone',
|
danielebarchiesi@0
|
1041 411 => 'Length Required',
|
danielebarchiesi@0
|
1042 412 => 'Precondition Failed',
|
danielebarchiesi@0
|
1043 413 => 'Request Entity Too Large',
|
danielebarchiesi@0
|
1044 414 => 'Request-URI Too Large',
|
danielebarchiesi@0
|
1045 415 => 'Unsupported Media Type',
|
danielebarchiesi@0
|
1046 416 => 'Requested range not satisfiable',
|
danielebarchiesi@0
|
1047 417 => 'Expectation Failed',
|
danielebarchiesi@0
|
1048 500 => 'Internal Server Error',
|
danielebarchiesi@0
|
1049 501 => 'Not Implemented',
|
danielebarchiesi@0
|
1050 502 => 'Bad Gateway',
|
danielebarchiesi@0
|
1051 503 => 'Service Unavailable',
|
danielebarchiesi@0
|
1052 504 => 'Gateway Time-out',
|
danielebarchiesi@0
|
1053 505 => 'HTTP Version not supported',
|
danielebarchiesi@0
|
1054 );
|
danielebarchiesi@0
|
1055 // RFC 2616 states that all unknown HTTP codes must be treated the same as the
|
danielebarchiesi@0
|
1056 // base code in their class.
|
danielebarchiesi@0
|
1057 if (!isset($responses[$code])) {
|
danielebarchiesi@0
|
1058 $code = floor($code / 100) * 100;
|
danielebarchiesi@0
|
1059 }
|
danielebarchiesi@0
|
1060 $result->code = $code;
|
danielebarchiesi@0
|
1061
|
danielebarchiesi@0
|
1062 switch ($code) {
|
danielebarchiesi@0
|
1063 case 200: // OK
|
danielebarchiesi@0
|
1064 case 304: // Not modified
|
danielebarchiesi@0
|
1065 break;
|
danielebarchiesi@0
|
1066 case 301: // Moved permanently
|
danielebarchiesi@0
|
1067 case 302: // Moved temporarily
|
danielebarchiesi@0
|
1068 case 307: // Moved temporarily
|
danielebarchiesi@0
|
1069 $location = $result->headers['location'];
|
danielebarchiesi@0
|
1070 $options['timeout'] -= timer_read(__FUNCTION__) / 1000;
|
danielebarchiesi@0
|
1071 if ($options['timeout'] <= 0) {
|
danielebarchiesi@0
|
1072 $result->code = HTTP_REQUEST_TIMEOUT;
|
danielebarchiesi@0
|
1073 $result->error = 'request timed out';
|
danielebarchiesi@0
|
1074 }
|
danielebarchiesi@0
|
1075 elseif ($options['max_redirects']) {
|
danielebarchiesi@0
|
1076 // Redirect to the new location.
|
danielebarchiesi@0
|
1077 $options['max_redirects']--;
|
danielebarchiesi@0
|
1078 $result = drupal_http_request($location, $options);
|
danielebarchiesi@0
|
1079 $result->redirect_code = $code;
|
danielebarchiesi@0
|
1080 }
|
danielebarchiesi@0
|
1081 if (!isset($result->redirect_url)) {
|
danielebarchiesi@0
|
1082 $result->redirect_url = $location;
|
danielebarchiesi@0
|
1083 }
|
danielebarchiesi@0
|
1084 break;
|
danielebarchiesi@0
|
1085 default:
|
danielebarchiesi@0
|
1086 $result->error = $status_message;
|
danielebarchiesi@0
|
1087 }
|
danielebarchiesi@0
|
1088
|
danielebarchiesi@0
|
1089 return $result;
|
danielebarchiesi@0
|
1090 }
|
danielebarchiesi@0
|
1091
|
danielebarchiesi@0
|
1092 /**
|
danielebarchiesi@0
|
1093 * Helper function for determining hosts excluded from needing a proxy.
|
danielebarchiesi@0
|
1094 *
|
danielebarchiesi@0
|
1095 * @return
|
danielebarchiesi@0
|
1096 * TRUE if a proxy should be used for this host.
|
danielebarchiesi@0
|
1097 */
|
danielebarchiesi@0
|
1098 function _drupal_http_use_proxy($host) {
|
danielebarchiesi@0
|
1099 $proxy_exceptions = variable_get('proxy_exceptions', array('localhost', '127.0.0.1'));
|
danielebarchiesi@0
|
1100 return !in_array(strtolower($host), $proxy_exceptions, TRUE);
|
danielebarchiesi@0
|
1101 }
|
danielebarchiesi@0
|
1102
|
danielebarchiesi@0
|
1103 /**
|
danielebarchiesi@0
|
1104 * @} End of "HTTP handling".
|
danielebarchiesi@0
|
1105 */
|
danielebarchiesi@0
|
1106
|
danielebarchiesi@0
|
1107 /**
|
danielebarchiesi@0
|
1108 * Strips slashes from a string or array of strings.
|
danielebarchiesi@0
|
1109 *
|
danielebarchiesi@0
|
1110 * Callback for array_walk() within fix_gpx_magic().
|
danielebarchiesi@0
|
1111 *
|
danielebarchiesi@0
|
1112 * @param $item
|
danielebarchiesi@0
|
1113 * An individual string or array of strings from superglobals.
|
danielebarchiesi@0
|
1114 */
|
danielebarchiesi@0
|
1115 function _fix_gpc_magic(&$item) {
|
danielebarchiesi@0
|
1116 if (is_array($item)) {
|
danielebarchiesi@0
|
1117 array_walk($item, '_fix_gpc_magic');
|
danielebarchiesi@0
|
1118 }
|
danielebarchiesi@0
|
1119 else {
|
danielebarchiesi@0
|
1120 $item = stripslashes($item);
|
danielebarchiesi@0
|
1121 }
|
danielebarchiesi@0
|
1122 }
|
danielebarchiesi@0
|
1123
|
danielebarchiesi@0
|
1124 /**
|
danielebarchiesi@0
|
1125 * Strips slashes from $_FILES items.
|
danielebarchiesi@0
|
1126 *
|
danielebarchiesi@0
|
1127 * Callback for array_walk() within fix_gpc_magic().
|
danielebarchiesi@0
|
1128 *
|
danielebarchiesi@0
|
1129 * The tmp_name key is skipped keys since PHP generates single backslashes for
|
danielebarchiesi@0
|
1130 * file paths on Windows systems.
|
danielebarchiesi@0
|
1131 *
|
danielebarchiesi@0
|
1132 * @param $item
|
danielebarchiesi@0
|
1133 * An item from $_FILES.
|
danielebarchiesi@0
|
1134 * @param $key
|
danielebarchiesi@0
|
1135 * The key for the item within $_FILES.
|
danielebarchiesi@0
|
1136 *
|
danielebarchiesi@0
|
1137 * @see http://php.net/manual/en/features.file-upload.php#42280
|
danielebarchiesi@0
|
1138 */
|
danielebarchiesi@0
|
1139 function _fix_gpc_magic_files(&$item, $key) {
|
danielebarchiesi@0
|
1140 if ($key != 'tmp_name') {
|
danielebarchiesi@0
|
1141 if (is_array($item)) {
|
danielebarchiesi@0
|
1142 array_walk($item, '_fix_gpc_magic_files');
|
danielebarchiesi@0
|
1143 }
|
danielebarchiesi@0
|
1144 else {
|
danielebarchiesi@0
|
1145 $item = stripslashes($item);
|
danielebarchiesi@0
|
1146 }
|
danielebarchiesi@0
|
1147 }
|
danielebarchiesi@0
|
1148 }
|
danielebarchiesi@0
|
1149
|
danielebarchiesi@0
|
1150 /**
|
danielebarchiesi@0
|
1151 * Fixes double-escaping caused by "magic quotes" in some PHP installations.
|
danielebarchiesi@0
|
1152 *
|
danielebarchiesi@0
|
1153 * @see _fix_gpc_magic()
|
danielebarchiesi@0
|
1154 * @see _fix_gpc_magic_files()
|
danielebarchiesi@0
|
1155 */
|
danielebarchiesi@0
|
1156 function fix_gpc_magic() {
|
danielebarchiesi@0
|
1157 static $fixed = FALSE;
|
danielebarchiesi@0
|
1158 if (!$fixed && ini_get('magic_quotes_gpc')) {
|
danielebarchiesi@0
|
1159 array_walk($_GET, '_fix_gpc_magic');
|
danielebarchiesi@0
|
1160 array_walk($_POST, '_fix_gpc_magic');
|
danielebarchiesi@0
|
1161 array_walk($_COOKIE, '_fix_gpc_magic');
|
danielebarchiesi@0
|
1162 array_walk($_REQUEST, '_fix_gpc_magic');
|
danielebarchiesi@0
|
1163 array_walk($_FILES, '_fix_gpc_magic_files');
|
danielebarchiesi@0
|
1164 }
|
danielebarchiesi@0
|
1165 $fixed = TRUE;
|
danielebarchiesi@0
|
1166 }
|
danielebarchiesi@0
|
1167
|
danielebarchiesi@0
|
1168 /**
|
danielebarchiesi@0
|
1169 * @defgroup validation Input validation
|
danielebarchiesi@0
|
1170 * @{
|
danielebarchiesi@0
|
1171 * Functions to validate user input.
|
danielebarchiesi@0
|
1172 */
|
danielebarchiesi@0
|
1173
|
danielebarchiesi@0
|
1174 /**
|
danielebarchiesi@0
|
1175 * Verifies the syntax of the given e-mail address.
|
danielebarchiesi@0
|
1176 *
|
danielebarchiesi@0
|
1177 * This uses the
|
danielebarchiesi@0
|
1178 * @link http://php.net/manual/filter.filters.validate.php PHP e-mail validation filter. @endlink
|
danielebarchiesi@0
|
1179 *
|
danielebarchiesi@0
|
1180 * @param $mail
|
danielebarchiesi@0
|
1181 * A string containing an e-mail address.
|
danielebarchiesi@0
|
1182 *
|
danielebarchiesi@0
|
1183 * @return
|
danielebarchiesi@0
|
1184 * TRUE if the address is in a valid format.
|
danielebarchiesi@0
|
1185 */
|
danielebarchiesi@0
|
1186 function valid_email_address($mail) {
|
danielebarchiesi@0
|
1187 return (bool)filter_var($mail, FILTER_VALIDATE_EMAIL);
|
danielebarchiesi@0
|
1188 }
|
danielebarchiesi@0
|
1189
|
danielebarchiesi@0
|
1190 /**
|
danielebarchiesi@0
|
1191 * Verifies the syntax of the given URL.
|
danielebarchiesi@0
|
1192 *
|
danielebarchiesi@0
|
1193 * This function should only be used on actual URLs. It should not be used for
|
danielebarchiesi@0
|
1194 * Drupal menu paths, which can contain arbitrary characters.
|
danielebarchiesi@0
|
1195 * Valid values per RFC 3986.
|
danielebarchiesi@0
|
1196 * @param $url
|
danielebarchiesi@0
|
1197 * The URL to verify.
|
danielebarchiesi@0
|
1198 * @param $absolute
|
danielebarchiesi@0
|
1199 * Whether the URL is absolute (beginning with a scheme such as "http:").
|
danielebarchiesi@0
|
1200 *
|
danielebarchiesi@0
|
1201 * @return
|
danielebarchiesi@0
|
1202 * TRUE if the URL is in a valid format.
|
danielebarchiesi@0
|
1203 */
|
danielebarchiesi@0
|
1204 function valid_url($url, $absolute = FALSE) {
|
danielebarchiesi@0
|
1205 if ($absolute) {
|
danielebarchiesi@0
|
1206 return (bool)preg_match("
|
danielebarchiesi@0
|
1207 /^ # Start at the beginning of the text
|
danielebarchiesi@0
|
1208 (?:ftp|https?|feed):\/\/ # Look for ftp, http, https or feed schemes
|
danielebarchiesi@0
|
1209 (?: # Userinfo (optional) which is typically
|
danielebarchiesi@0
|
1210 (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)* # a username or a username and password
|
danielebarchiesi@0
|
1211 (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@ # combination
|
danielebarchiesi@0
|
1212 )?
|
danielebarchiesi@0
|
1213 (?:
|
danielebarchiesi@0
|
1214 (?:[a-z0-9\-\.]|%[0-9a-f]{2})+ # A domain name or a IPv4 address
|
danielebarchiesi@0
|
1215 |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]) # or a well formed IPv6 address
|
danielebarchiesi@0
|
1216 )
|
danielebarchiesi@0
|
1217 (?::[0-9]+)? # Server port number (optional)
|
danielebarchiesi@0
|
1218 (?:[\/|\?]
|
danielebarchiesi@0
|
1219 (?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2}) # The path and query (optional)
|
danielebarchiesi@0
|
1220 *)?
|
danielebarchiesi@0
|
1221 $/xi", $url);
|
danielebarchiesi@0
|
1222 }
|
danielebarchiesi@0
|
1223 else {
|
danielebarchiesi@0
|
1224 return (bool)preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url);
|
danielebarchiesi@0
|
1225 }
|
danielebarchiesi@0
|
1226 }
|
danielebarchiesi@0
|
1227
|
danielebarchiesi@0
|
1228 /**
|
danielebarchiesi@0
|
1229 * @} End of "defgroup validation".
|
danielebarchiesi@0
|
1230 */
|
danielebarchiesi@0
|
1231
|
danielebarchiesi@0
|
1232 /**
|
danielebarchiesi@0
|
1233 * Registers an event for the current visitor to the flood control mechanism.
|
danielebarchiesi@0
|
1234 *
|
danielebarchiesi@0
|
1235 * @param $name
|
danielebarchiesi@0
|
1236 * The name of an event.
|
danielebarchiesi@0
|
1237 * @param $window
|
danielebarchiesi@0
|
1238 * Optional number of seconds before this event expires. Defaults to 3600 (1
|
danielebarchiesi@0
|
1239 * hour). Typically uses the same value as the flood_is_allowed() $window
|
danielebarchiesi@0
|
1240 * parameter. Expired events are purged on cron run to prevent the flood table
|
danielebarchiesi@0
|
1241 * from growing indefinitely.
|
danielebarchiesi@0
|
1242 * @param $identifier
|
danielebarchiesi@0
|
1243 * Optional identifier (defaults to the current user's IP address).
|
danielebarchiesi@0
|
1244 */
|
danielebarchiesi@0
|
1245 function flood_register_event($name, $window = 3600, $identifier = NULL) {
|
danielebarchiesi@0
|
1246 if (!isset($identifier)) {
|
danielebarchiesi@0
|
1247 $identifier = ip_address();
|
danielebarchiesi@0
|
1248 }
|
danielebarchiesi@0
|
1249 db_insert('flood')
|
danielebarchiesi@0
|
1250 ->fields(array(
|
danielebarchiesi@0
|
1251 'event' => $name,
|
danielebarchiesi@0
|
1252 'identifier' => $identifier,
|
danielebarchiesi@0
|
1253 'timestamp' => REQUEST_TIME,
|
danielebarchiesi@0
|
1254 'expiration' => REQUEST_TIME + $window,
|
danielebarchiesi@0
|
1255 ))
|
danielebarchiesi@0
|
1256 ->execute();
|
danielebarchiesi@0
|
1257 }
|
danielebarchiesi@0
|
1258
|
danielebarchiesi@0
|
1259 /**
|
danielebarchiesi@0
|
1260 * Makes the flood control mechanism forget an event for the current visitor.
|
danielebarchiesi@0
|
1261 *
|
danielebarchiesi@0
|
1262 * @param $name
|
danielebarchiesi@0
|
1263 * The name of an event.
|
danielebarchiesi@0
|
1264 * @param $identifier
|
danielebarchiesi@0
|
1265 * Optional identifier (defaults to the current user's IP address).
|
danielebarchiesi@0
|
1266 */
|
danielebarchiesi@0
|
1267 function flood_clear_event($name, $identifier = NULL) {
|
danielebarchiesi@0
|
1268 if (!isset($identifier)) {
|
danielebarchiesi@0
|
1269 $identifier = ip_address();
|
danielebarchiesi@0
|
1270 }
|
danielebarchiesi@0
|
1271 db_delete('flood')
|
danielebarchiesi@0
|
1272 ->condition('event', $name)
|
danielebarchiesi@0
|
1273 ->condition('identifier', $identifier)
|
danielebarchiesi@0
|
1274 ->execute();
|
danielebarchiesi@0
|
1275 }
|
danielebarchiesi@0
|
1276
|
danielebarchiesi@0
|
1277 /**
|
danielebarchiesi@0
|
1278 * Checks whether a user is allowed to proceed with the specified event.
|
danielebarchiesi@0
|
1279 *
|
danielebarchiesi@0
|
1280 * Events can have thresholds saying that each user can only do that event
|
danielebarchiesi@0
|
1281 * a certain number of times in a time window. This function verifies that the
|
danielebarchiesi@0
|
1282 * current user has not exceeded this threshold.
|
danielebarchiesi@0
|
1283 *
|
danielebarchiesi@0
|
1284 * @param $name
|
danielebarchiesi@0
|
1285 * The unique name of the event.
|
danielebarchiesi@0
|
1286 * @param $threshold
|
danielebarchiesi@0
|
1287 * The maximum number of times each user can do this event per time window.
|
danielebarchiesi@0
|
1288 * @param $window
|
danielebarchiesi@0
|
1289 * Number of seconds in the time window for this event (default is 3600
|
danielebarchiesi@0
|
1290 * seconds, or 1 hour).
|
danielebarchiesi@0
|
1291 * @param $identifier
|
danielebarchiesi@0
|
1292 * Unique identifier of the current user. Defaults to their IP address.
|
danielebarchiesi@0
|
1293 *
|
danielebarchiesi@0
|
1294 * @return
|
danielebarchiesi@0
|
1295 * TRUE if the user is allowed to proceed. FALSE if they have exceeded the
|
danielebarchiesi@0
|
1296 * threshold and should not be allowed to proceed.
|
danielebarchiesi@0
|
1297 */
|
danielebarchiesi@0
|
1298 function flood_is_allowed($name, $threshold, $window = 3600, $identifier = NULL) {
|
danielebarchiesi@0
|
1299 if (!isset($identifier)) {
|
danielebarchiesi@0
|
1300 $identifier = ip_address();
|
danielebarchiesi@0
|
1301 }
|
danielebarchiesi@0
|
1302 $number = db_query("SELECT COUNT(*) FROM {flood} WHERE event = :event AND identifier = :identifier AND timestamp > :timestamp", array(
|
danielebarchiesi@0
|
1303 ':event' => $name,
|
danielebarchiesi@0
|
1304 ':identifier' => $identifier,
|
danielebarchiesi@0
|
1305 ':timestamp' => REQUEST_TIME - $window))
|
danielebarchiesi@0
|
1306 ->fetchField();
|
danielebarchiesi@0
|
1307 return ($number < $threshold);
|
danielebarchiesi@0
|
1308 }
|
danielebarchiesi@0
|
1309
|
danielebarchiesi@0
|
1310 /**
|
danielebarchiesi@0
|
1311 * @defgroup sanitization Sanitization functions
|
danielebarchiesi@0
|
1312 * @{
|
danielebarchiesi@0
|
1313 * Functions to sanitize values.
|
danielebarchiesi@0
|
1314 *
|
danielebarchiesi@0
|
1315 * See http://drupal.org/writing-secure-code for information
|
danielebarchiesi@0
|
1316 * on writing secure code.
|
danielebarchiesi@0
|
1317 */
|
danielebarchiesi@0
|
1318
|
danielebarchiesi@0
|
1319 /**
|
danielebarchiesi@0
|
1320 * Strips dangerous protocols (e.g. 'javascript:') from a URI.
|
danielebarchiesi@0
|
1321 *
|
danielebarchiesi@0
|
1322 * This function must be called for all URIs within user-entered input prior
|
danielebarchiesi@0
|
1323 * to being output to an HTML attribute value. It is often called as part of
|
danielebarchiesi@0
|
1324 * check_url() or filter_xss(), but those functions return an HTML-encoded
|
danielebarchiesi@0
|
1325 * string, so this function can be called independently when the output needs to
|
danielebarchiesi@0
|
1326 * be a plain-text string for passing to t(), l(), drupal_attributes(), or
|
danielebarchiesi@0
|
1327 * another function that will call check_plain() separately.
|
danielebarchiesi@0
|
1328 *
|
danielebarchiesi@0
|
1329 * @param $uri
|
danielebarchiesi@0
|
1330 * A plain-text URI that might contain dangerous protocols.
|
danielebarchiesi@0
|
1331 *
|
danielebarchiesi@0
|
1332 * @return
|
danielebarchiesi@0
|
1333 * A plain-text URI stripped of dangerous protocols. As with all plain-text
|
danielebarchiesi@0
|
1334 * strings, this return value must not be output to an HTML page without
|
danielebarchiesi@0
|
1335 * check_plain() being called on it. However, it can be passed to functions
|
danielebarchiesi@0
|
1336 * expecting plain-text strings.
|
danielebarchiesi@0
|
1337 *
|
danielebarchiesi@0
|
1338 * @see check_url()
|
danielebarchiesi@0
|
1339 */
|
danielebarchiesi@0
|
1340 function drupal_strip_dangerous_protocols($uri) {
|
danielebarchiesi@0
|
1341 static $allowed_protocols;
|
danielebarchiesi@0
|
1342
|
danielebarchiesi@0
|
1343 if (!isset($allowed_protocols)) {
|
danielebarchiesi@0
|
1344 $allowed_protocols = array_flip(variable_get('filter_allowed_protocols', array('ftp', 'http', 'https', 'irc', 'mailto', 'news', 'nntp', 'rtsp', 'sftp', 'ssh', 'tel', 'telnet', 'webcal')));
|
danielebarchiesi@0
|
1345 }
|
danielebarchiesi@0
|
1346
|
danielebarchiesi@0
|
1347 // Iteratively remove any invalid protocol found.
|
danielebarchiesi@0
|
1348 do {
|
danielebarchiesi@0
|
1349 $before = $uri;
|
danielebarchiesi@0
|
1350 $colonpos = strpos($uri, ':');
|
danielebarchiesi@0
|
1351 if ($colonpos > 0) {
|
danielebarchiesi@0
|
1352 // We found a colon, possibly a protocol. Verify.
|
danielebarchiesi@0
|
1353 $protocol = substr($uri, 0, $colonpos);
|
danielebarchiesi@0
|
1354 // If a colon is preceded by a slash, question mark or hash, it cannot
|
danielebarchiesi@0
|
1355 // possibly be part of the URL scheme. This must be a relative URL, which
|
danielebarchiesi@0
|
1356 // inherits the (safe) protocol of the base document.
|
danielebarchiesi@0
|
1357 if (preg_match('![/?#]!', $protocol)) {
|
danielebarchiesi@0
|
1358 break;
|
danielebarchiesi@0
|
1359 }
|
danielebarchiesi@0
|
1360 // Check if this is a disallowed protocol. Per RFC2616, section 3.2.3
|
danielebarchiesi@0
|
1361 // (URI Comparison) scheme comparison must be case-insensitive.
|
danielebarchiesi@0
|
1362 if (!isset($allowed_protocols[strtolower($protocol)])) {
|
danielebarchiesi@0
|
1363 $uri = substr($uri, $colonpos + 1);
|
danielebarchiesi@0
|
1364 }
|
danielebarchiesi@0
|
1365 }
|
danielebarchiesi@0
|
1366 } while ($before != $uri);
|
danielebarchiesi@0
|
1367
|
danielebarchiesi@0
|
1368 return $uri;
|
danielebarchiesi@0
|
1369 }
|
danielebarchiesi@0
|
1370
|
danielebarchiesi@0
|
1371 /**
|
danielebarchiesi@0
|
1372 * Strips dangerous protocols from a URI and encodes it for output to HTML.
|
danielebarchiesi@0
|
1373 *
|
danielebarchiesi@0
|
1374 * @param $uri
|
danielebarchiesi@0
|
1375 * A plain-text URI that might contain dangerous protocols.
|
danielebarchiesi@0
|
1376 *
|
danielebarchiesi@0
|
1377 * @return
|
danielebarchiesi@0
|
1378 * A URI stripped of dangerous protocols and encoded for output to an HTML
|
danielebarchiesi@0
|
1379 * attribute value. Because it is already encoded, it should not be set as a
|
danielebarchiesi@0
|
1380 * value within a $attributes array passed to drupal_attributes(), because
|
danielebarchiesi@0
|
1381 * drupal_attributes() expects those values to be plain-text strings. To pass
|
danielebarchiesi@0
|
1382 * a filtered URI to drupal_attributes(), call
|
danielebarchiesi@0
|
1383 * drupal_strip_dangerous_protocols() instead.
|
danielebarchiesi@0
|
1384 *
|
danielebarchiesi@0
|
1385 * @see drupal_strip_dangerous_protocols()
|
danielebarchiesi@0
|
1386 */
|
danielebarchiesi@0
|
1387 function check_url($uri) {
|
danielebarchiesi@0
|
1388 return check_plain(drupal_strip_dangerous_protocols($uri));
|
danielebarchiesi@0
|
1389 }
|
danielebarchiesi@0
|
1390
|
danielebarchiesi@0
|
1391 /**
|
danielebarchiesi@0
|
1392 * Applies a very permissive XSS/HTML filter for admin-only use.
|
danielebarchiesi@0
|
1393 *
|
danielebarchiesi@0
|
1394 * Use only for fields where it is impractical to use the
|
danielebarchiesi@0
|
1395 * whole filter system, but where some (mainly inline) mark-up
|
danielebarchiesi@0
|
1396 * is desired (so check_plain() is not acceptable).
|
danielebarchiesi@0
|
1397 *
|
danielebarchiesi@0
|
1398 * Allows all tags that can be used inside an HTML body, save
|
danielebarchiesi@0
|
1399 * for scripts and styles.
|
danielebarchiesi@0
|
1400 */
|
danielebarchiesi@0
|
1401 function filter_xss_admin($string) {
|
danielebarchiesi@0
|
1402 return filter_xss($string, array('a', 'abbr', 'acronym', 'address', 'article', 'aside', 'b', 'bdi', 'bdo', 'big', 'blockquote', 'br', 'caption', 'cite', 'code', 'col', 'colgroup', 'command', 'dd', 'del', 'details', 'dfn', 'div', 'dl', 'dt', 'em', 'figcaption', 'figure', 'footer', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'i', 'img', 'ins', 'kbd', 'li', 'mark', 'menu', 'meter', 'nav', 'ol', 'output', 'p', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'section', 'small', 'span', 'strong', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'time', 'tr', 'tt', 'u', 'ul', 'var', 'wbr'));
|
danielebarchiesi@0
|
1403 }
|
danielebarchiesi@0
|
1404
|
danielebarchiesi@0
|
1405 /**
|
danielebarchiesi@0
|
1406 * Filters HTML to prevent cross-site-scripting (XSS) vulnerabilities.
|
danielebarchiesi@0
|
1407 *
|
danielebarchiesi@0
|
1408 * Based on kses by Ulf Harnhammar, see http://sourceforge.net/projects/kses.
|
danielebarchiesi@0
|
1409 * For examples of various XSS attacks, see: http://ha.ckers.org/xss.html.
|
danielebarchiesi@0
|
1410 *
|
danielebarchiesi@0
|
1411 * This code does four things:
|
danielebarchiesi@0
|
1412 * - Removes characters and constructs that can trick browsers.
|
danielebarchiesi@0
|
1413 * - Makes sure all HTML entities are well-formed.
|
danielebarchiesi@0
|
1414 * - Makes sure all HTML tags and attributes are well-formed.
|
danielebarchiesi@0
|
1415 * - Makes sure no HTML tags contain URLs with a disallowed protocol (e.g.
|
danielebarchiesi@0
|
1416 * javascript:).
|
danielebarchiesi@0
|
1417 *
|
danielebarchiesi@0
|
1418 * @param $string
|
danielebarchiesi@0
|
1419 * The string with raw HTML in it. It will be stripped of everything that can
|
danielebarchiesi@0
|
1420 * cause an XSS attack.
|
danielebarchiesi@0
|
1421 * @param $allowed_tags
|
danielebarchiesi@0
|
1422 * An array of allowed tags.
|
danielebarchiesi@0
|
1423 *
|
danielebarchiesi@0
|
1424 * @return
|
danielebarchiesi@0
|
1425 * An XSS safe version of $string, or an empty string if $string is not
|
danielebarchiesi@0
|
1426 * valid UTF-8.
|
danielebarchiesi@0
|
1427 *
|
danielebarchiesi@0
|
1428 * @see drupal_validate_utf8()
|
danielebarchiesi@0
|
1429 * @ingroup sanitization
|
danielebarchiesi@0
|
1430 */
|
danielebarchiesi@0
|
1431 function filter_xss($string, $allowed_tags = array('a', 'em', 'strong', 'cite', 'blockquote', 'code', 'ul', 'ol', 'li', 'dl', 'dt', 'dd')) {
|
danielebarchiesi@0
|
1432 // Only operate on valid UTF-8 strings. This is necessary to prevent cross
|
danielebarchiesi@0
|
1433 // site scripting issues on Internet Explorer 6.
|
danielebarchiesi@0
|
1434 if (!drupal_validate_utf8($string)) {
|
danielebarchiesi@0
|
1435 return '';
|
danielebarchiesi@0
|
1436 }
|
danielebarchiesi@0
|
1437 // Store the text format.
|
danielebarchiesi@0
|
1438 _filter_xss_split($allowed_tags, TRUE);
|
danielebarchiesi@0
|
1439 // Remove NULL characters (ignored by some browsers).
|
danielebarchiesi@0
|
1440 $string = str_replace(chr(0), '', $string);
|
danielebarchiesi@0
|
1441 // Remove Netscape 4 JS entities.
|
danielebarchiesi@0
|
1442 $string = preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string);
|
danielebarchiesi@0
|
1443
|
danielebarchiesi@0
|
1444 // Defuse all HTML entities.
|
danielebarchiesi@0
|
1445 $string = str_replace('&', '&', $string);
|
danielebarchiesi@0
|
1446 // Change back only well-formed entities in our whitelist:
|
danielebarchiesi@0
|
1447 // Decimal numeric entities.
|
danielebarchiesi@0
|
1448 $string = preg_replace('/&#([0-9]+;)/', '&#\1', $string);
|
danielebarchiesi@0
|
1449 // Hexadecimal numeric entities.
|
danielebarchiesi@0
|
1450 $string = preg_replace('/&#[Xx]0*((?:[0-9A-Fa-f]{2})+;)/', '&#x\1', $string);
|
danielebarchiesi@0
|
1451 // Named entities.
|
danielebarchiesi@0
|
1452 $string = preg_replace('/&([A-Za-z][A-Za-z0-9]*;)/', '&\1', $string);
|
danielebarchiesi@0
|
1453
|
danielebarchiesi@0
|
1454 return preg_replace_callback('%
|
danielebarchiesi@0
|
1455 (
|
danielebarchiesi@0
|
1456 <(?=[^a-zA-Z!/]) # a lone <
|
danielebarchiesi@0
|
1457 | # or
|
danielebarchiesi@0
|
1458 <!--.*?--> # a comment
|
danielebarchiesi@0
|
1459 | # or
|
danielebarchiesi@0
|
1460 <[^>]*(>|$) # a string that starts with a <, up until the > or the end of the string
|
danielebarchiesi@0
|
1461 | # or
|
danielebarchiesi@0
|
1462 > # just a >
|
danielebarchiesi@0
|
1463 )%x', '_filter_xss_split', $string);
|
danielebarchiesi@0
|
1464 }
|
danielebarchiesi@0
|
1465
|
danielebarchiesi@0
|
1466 /**
|
danielebarchiesi@0
|
1467 * Processes an HTML tag.
|
danielebarchiesi@0
|
1468 *
|
danielebarchiesi@0
|
1469 * @param $m
|
danielebarchiesi@0
|
1470 * An array with various meaning depending on the value of $store.
|
danielebarchiesi@0
|
1471 * If $store is TRUE then the array contains the allowed tags.
|
danielebarchiesi@0
|
1472 * If $store is FALSE then the array has one element, the HTML tag to process.
|
danielebarchiesi@0
|
1473 * @param $store
|
danielebarchiesi@0
|
1474 * Whether to store $m.
|
danielebarchiesi@0
|
1475 *
|
danielebarchiesi@0
|
1476 * @return
|
danielebarchiesi@0
|
1477 * If the element isn't allowed, an empty string. Otherwise, the cleaned up
|
danielebarchiesi@0
|
1478 * version of the HTML element.
|
danielebarchiesi@0
|
1479 */
|
danielebarchiesi@0
|
1480 function _filter_xss_split($m, $store = FALSE) {
|
danielebarchiesi@0
|
1481 static $allowed_html;
|
danielebarchiesi@0
|
1482
|
danielebarchiesi@0
|
1483 if ($store) {
|
danielebarchiesi@0
|
1484 $allowed_html = array_flip($m);
|
danielebarchiesi@0
|
1485 return;
|
danielebarchiesi@0
|
1486 }
|
danielebarchiesi@0
|
1487
|
danielebarchiesi@0
|
1488 $string = $m[1];
|
danielebarchiesi@0
|
1489
|
danielebarchiesi@0
|
1490 if (substr($string, 0, 1) != '<') {
|
danielebarchiesi@0
|
1491 // We matched a lone ">" character.
|
danielebarchiesi@0
|
1492 return '>';
|
danielebarchiesi@0
|
1493 }
|
danielebarchiesi@0
|
1494 elseif (strlen($string) == 1) {
|
danielebarchiesi@0
|
1495 // We matched a lone "<" character.
|
danielebarchiesi@0
|
1496 return '<';
|
danielebarchiesi@0
|
1497 }
|
danielebarchiesi@0
|
1498
|
danielebarchiesi@0
|
1499 if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?|(<!--.*?-->)$%', $string, $matches)) {
|
danielebarchiesi@0
|
1500 // Seriously malformed.
|
danielebarchiesi@0
|
1501 return '';
|
danielebarchiesi@0
|
1502 }
|
danielebarchiesi@0
|
1503
|
danielebarchiesi@0
|
1504 $slash = trim($matches[1]);
|
danielebarchiesi@0
|
1505 $elem = &$matches[2];
|
danielebarchiesi@0
|
1506 $attrlist = &$matches[3];
|
danielebarchiesi@0
|
1507 $comment = &$matches[4];
|
danielebarchiesi@0
|
1508
|
danielebarchiesi@0
|
1509 if ($comment) {
|
danielebarchiesi@0
|
1510 $elem = '!--';
|
danielebarchiesi@0
|
1511 }
|
danielebarchiesi@0
|
1512
|
danielebarchiesi@0
|
1513 if (!isset($allowed_html[strtolower($elem)])) {
|
danielebarchiesi@0
|
1514 // Disallowed HTML element.
|
danielebarchiesi@0
|
1515 return '';
|
danielebarchiesi@0
|
1516 }
|
danielebarchiesi@0
|
1517
|
danielebarchiesi@0
|
1518 if ($comment) {
|
danielebarchiesi@0
|
1519 return $comment;
|
danielebarchiesi@0
|
1520 }
|
danielebarchiesi@0
|
1521
|
danielebarchiesi@0
|
1522 if ($slash != '') {
|
danielebarchiesi@0
|
1523 return "</$elem>";
|
danielebarchiesi@0
|
1524 }
|
danielebarchiesi@0
|
1525
|
danielebarchiesi@0
|
1526 // Is there a closing XHTML slash at the end of the attributes?
|
danielebarchiesi@0
|
1527 $attrlist = preg_replace('%(\s?)/\s*$%', '\1', $attrlist, -1, $count);
|
danielebarchiesi@0
|
1528 $xhtml_slash = $count ? ' /' : '';
|
danielebarchiesi@0
|
1529
|
danielebarchiesi@0
|
1530 // Clean up attributes.
|
danielebarchiesi@0
|
1531 $attr2 = implode(' ', _filter_xss_attributes($attrlist));
|
danielebarchiesi@0
|
1532 $attr2 = preg_replace('/[<>]/', '', $attr2);
|
danielebarchiesi@0
|
1533 $attr2 = strlen($attr2) ? ' ' . $attr2 : '';
|
danielebarchiesi@0
|
1534
|
danielebarchiesi@0
|
1535 return "<$elem$attr2$xhtml_slash>";
|
danielebarchiesi@0
|
1536 }
|
danielebarchiesi@0
|
1537
|
danielebarchiesi@0
|
1538 /**
|
danielebarchiesi@0
|
1539 * Processes a string of HTML attributes.
|
danielebarchiesi@0
|
1540 *
|
danielebarchiesi@0
|
1541 * @return
|
danielebarchiesi@0
|
1542 * Cleaned up version of the HTML attributes.
|
danielebarchiesi@0
|
1543 */
|
danielebarchiesi@0
|
1544 function _filter_xss_attributes($attr) {
|
danielebarchiesi@0
|
1545 $attrarr = array();
|
danielebarchiesi@0
|
1546 $mode = 0;
|
danielebarchiesi@0
|
1547 $attrname = '';
|
danielebarchiesi@0
|
1548
|
danielebarchiesi@0
|
1549 while (strlen($attr) != 0) {
|
danielebarchiesi@0
|
1550 // Was the last operation successful?
|
danielebarchiesi@0
|
1551 $working = 0;
|
danielebarchiesi@0
|
1552
|
danielebarchiesi@0
|
1553 switch ($mode) {
|
danielebarchiesi@0
|
1554 case 0:
|
danielebarchiesi@0
|
1555 // Attribute name, href for instance.
|
danielebarchiesi@0
|
1556 if (preg_match('/^([-a-zA-Z]+)/', $attr, $match)) {
|
danielebarchiesi@0
|
1557 $attrname = strtolower($match[1]);
|
danielebarchiesi@0
|
1558 $skip = ($attrname == 'style' || substr($attrname, 0, 2) == 'on');
|
danielebarchiesi@0
|
1559 $working = $mode = 1;
|
danielebarchiesi@0
|
1560 $attr = preg_replace('/^[-a-zA-Z]+/', '', $attr);
|
danielebarchiesi@0
|
1561 }
|
danielebarchiesi@0
|
1562 break;
|
danielebarchiesi@0
|
1563
|
danielebarchiesi@0
|
1564 case 1:
|
danielebarchiesi@0
|
1565 // Equals sign or valueless ("selected").
|
danielebarchiesi@0
|
1566 if (preg_match('/^\s*=\s*/', $attr)) {
|
danielebarchiesi@0
|
1567 $working = 1; $mode = 2;
|
danielebarchiesi@0
|
1568 $attr = preg_replace('/^\s*=\s*/', '', $attr);
|
danielebarchiesi@0
|
1569 break;
|
danielebarchiesi@0
|
1570 }
|
danielebarchiesi@0
|
1571
|
danielebarchiesi@0
|
1572 if (preg_match('/^\s+/', $attr)) {
|
danielebarchiesi@0
|
1573 $working = 1; $mode = 0;
|
danielebarchiesi@0
|
1574 if (!$skip) {
|
danielebarchiesi@0
|
1575 $attrarr[] = $attrname;
|
danielebarchiesi@0
|
1576 }
|
danielebarchiesi@0
|
1577 $attr = preg_replace('/^\s+/', '', $attr);
|
danielebarchiesi@0
|
1578 }
|
danielebarchiesi@0
|
1579 break;
|
danielebarchiesi@0
|
1580
|
danielebarchiesi@0
|
1581 case 2:
|
danielebarchiesi@0
|
1582 // Attribute value, a URL after href= for instance.
|
danielebarchiesi@0
|
1583 if (preg_match('/^"([^"]*)"(\s+|$)/', $attr, $match)) {
|
danielebarchiesi@0
|
1584 $thisval = filter_xss_bad_protocol($match[1]);
|
danielebarchiesi@0
|
1585
|
danielebarchiesi@0
|
1586 if (!$skip) {
|
danielebarchiesi@0
|
1587 $attrarr[] = "$attrname=\"$thisval\"";
|
danielebarchiesi@0
|
1588 }
|
danielebarchiesi@0
|
1589 $working = 1;
|
danielebarchiesi@0
|
1590 $mode = 0;
|
danielebarchiesi@0
|
1591 $attr = preg_replace('/^"[^"]*"(\s+|$)/', '', $attr);
|
danielebarchiesi@0
|
1592 break;
|
danielebarchiesi@0
|
1593 }
|
danielebarchiesi@0
|
1594
|
danielebarchiesi@0
|
1595 if (preg_match("/^'([^']*)'(\s+|$)/", $attr, $match)) {
|
danielebarchiesi@0
|
1596 $thisval = filter_xss_bad_protocol($match[1]);
|
danielebarchiesi@0
|
1597
|
danielebarchiesi@0
|
1598 if (!$skip) {
|
danielebarchiesi@0
|
1599 $attrarr[] = "$attrname='$thisval'";
|
danielebarchiesi@0
|
1600 }
|
danielebarchiesi@0
|
1601 $working = 1; $mode = 0;
|
danielebarchiesi@0
|
1602 $attr = preg_replace("/^'[^']*'(\s+|$)/", '', $attr);
|
danielebarchiesi@0
|
1603 break;
|
danielebarchiesi@0
|
1604 }
|
danielebarchiesi@0
|
1605
|
danielebarchiesi@0
|
1606 if (preg_match("%^([^\s\"']+)(\s+|$)%", $attr, $match)) {
|
danielebarchiesi@0
|
1607 $thisval = filter_xss_bad_protocol($match[1]);
|
danielebarchiesi@0
|
1608
|
danielebarchiesi@0
|
1609 if (!$skip) {
|
danielebarchiesi@0
|
1610 $attrarr[] = "$attrname=\"$thisval\"";
|
danielebarchiesi@0
|
1611 }
|
danielebarchiesi@0
|
1612 $working = 1; $mode = 0;
|
danielebarchiesi@0
|
1613 $attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr);
|
danielebarchiesi@0
|
1614 }
|
danielebarchiesi@0
|
1615 break;
|
danielebarchiesi@0
|
1616 }
|
danielebarchiesi@0
|
1617
|
danielebarchiesi@0
|
1618 if ($working == 0) {
|
danielebarchiesi@0
|
1619 // Not well formed; remove and try again.
|
danielebarchiesi@0
|
1620 $attr = preg_replace('/
|
danielebarchiesi@0
|
1621 ^
|
danielebarchiesi@0
|
1622 (
|
danielebarchiesi@0
|
1623 "[^"]*("|$) # - a string that starts with a double quote, up until the next double quote or the end of the string
|
danielebarchiesi@0
|
1624 | # or
|
danielebarchiesi@0
|
1625 \'[^\']*(\'|$)| # - a string that starts with a quote, up until the next quote or the end of the string
|
danielebarchiesi@0
|
1626 | # or
|
danielebarchiesi@0
|
1627 \S # - a non-whitespace character
|
danielebarchiesi@0
|
1628 )* # any number of the above three
|
danielebarchiesi@0
|
1629 \s* # any number of whitespaces
|
danielebarchiesi@0
|
1630 /x', '', $attr);
|
danielebarchiesi@0
|
1631 $mode = 0;
|
danielebarchiesi@0
|
1632 }
|
danielebarchiesi@0
|
1633 }
|
danielebarchiesi@0
|
1634
|
danielebarchiesi@0
|
1635 // The attribute list ends with a valueless attribute like "selected".
|
danielebarchiesi@0
|
1636 if ($mode == 1 && !$skip) {
|
danielebarchiesi@0
|
1637 $attrarr[] = $attrname;
|
danielebarchiesi@0
|
1638 }
|
danielebarchiesi@0
|
1639 return $attrarr;
|
danielebarchiesi@0
|
1640 }
|
danielebarchiesi@0
|
1641
|
danielebarchiesi@0
|
1642 /**
|
danielebarchiesi@0
|
1643 * Processes an HTML attribute value and strips dangerous protocols from URLs.
|
danielebarchiesi@0
|
1644 *
|
danielebarchiesi@0
|
1645 * @param $string
|
danielebarchiesi@0
|
1646 * The string with the attribute value.
|
danielebarchiesi@0
|
1647 * @param $decode
|
danielebarchiesi@0
|
1648 * (deprecated) Whether to decode entities in the $string. Set to FALSE if the
|
danielebarchiesi@0
|
1649 * $string is in plain text, TRUE otherwise. Defaults to TRUE. This parameter
|
danielebarchiesi@0
|
1650 * is deprecated and will be removed in Drupal 8. To process a plain-text URI,
|
danielebarchiesi@0
|
1651 * call drupal_strip_dangerous_protocols() or check_url() instead.
|
danielebarchiesi@0
|
1652 *
|
danielebarchiesi@0
|
1653 * @return
|
danielebarchiesi@0
|
1654 * Cleaned up and HTML-escaped version of $string.
|
danielebarchiesi@0
|
1655 */
|
danielebarchiesi@0
|
1656 function filter_xss_bad_protocol($string, $decode = TRUE) {
|
danielebarchiesi@0
|
1657 // Get the plain text representation of the attribute value (i.e. its meaning).
|
danielebarchiesi@0
|
1658 // @todo Remove the $decode parameter in Drupal 8, and always assume an HTML
|
danielebarchiesi@0
|
1659 // string that needs decoding.
|
danielebarchiesi@0
|
1660 if ($decode) {
|
danielebarchiesi@0
|
1661 if (!function_exists('decode_entities')) {
|
danielebarchiesi@0
|
1662 require_once DRUPAL_ROOT . '/includes/unicode.inc';
|
danielebarchiesi@0
|
1663 }
|
danielebarchiesi@0
|
1664
|
danielebarchiesi@0
|
1665 $string = decode_entities($string);
|
danielebarchiesi@0
|
1666 }
|
danielebarchiesi@0
|
1667 return check_plain(drupal_strip_dangerous_protocols($string));
|
danielebarchiesi@0
|
1668 }
|
danielebarchiesi@0
|
1669
|
danielebarchiesi@0
|
1670 /**
|
danielebarchiesi@0
|
1671 * @} End of "defgroup sanitization".
|
danielebarchiesi@0
|
1672 */
|
danielebarchiesi@0
|
1673
|
danielebarchiesi@0
|
1674 /**
|
danielebarchiesi@0
|
1675 * @defgroup format Formatting
|
danielebarchiesi@0
|
1676 * @{
|
danielebarchiesi@0
|
1677 * Functions to format numbers, strings, dates, etc.
|
danielebarchiesi@0
|
1678 */
|
danielebarchiesi@0
|
1679
|
danielebarchiesi@0
|
1680 /**
|
danielebarchiesi@0
|
1681 * Formats an RSS channel.
|
danielebarchiesi@0
|
1682 *
|
danielebarchiesi@0
|
1683 * Arbitrary elements may be added using the $args associative array.
|
danielebarchiesi@0
|
1684 */
|
danielebarchiesi@0
|
1685 function format_rss_channel($title, $link, $description, $items, $langcode = NULL, $args = array()) {
|
danielebarchiesi@0
|
1686 global $language_content;
|
danielebarchiesi@0
|
1687 $langcode = $langcode ? $langcode : $language_content->language;
|
danielebarchiesi@0
|
1688
|
danielebarchiesi@0
|
1689 $output = "<channel>\n";
|
danielebarchiesi@0
|
1690 $output .= ' <title>' . check_plain($title) . "</title>\n";
|
danielebarchiesi@0
|
1691 $output .= ' <link>' . check_url($link) . "</link>\n";
|
danielebarchiesi@0
|
1692
|
danielebarchiesi@0
|
1693 // The RSS 2.0 "spec" doesn't indicate HTML can be used in the description.
|
danielebarchiesi@0
|
1694 // We strip all HTML tags, but need to prevent double encoding from properly
|
danielebarchiesi@0
|
1695 // escaped source data (such as & becoming &amp;).
|
danielebarchiesi@0
|
1696 $output .= ' <description>' . check_plain(decode_entities(strip_tags($description))) . "</description>\n";
|
danielebarchiesi@0
|
1697 $output .= ' <language>' . check_plain($langcode) . "</language>\n";
|
danielebarchiesi@0
|
1698 $output .= format_xml_elements($args);
|
danielebarchiesi@0
|
1699 $output .= $items;
|
danielebarchiesi@0
|
1700 $output .= "</channel>\n";
|
danielebarchiesi@0
|
1701
|
danielebarchiesi@0
|
1702 return $output;
|
danielebarchiesi@0
|
1703 }
|
danielebarchiesi@0
|
1704
|
danielebarchiesi@0
|
1705 /**
|
danielebarchiesi@0
|
1706 * Formats a single RSS item.
|
danielebarchiesi@0
|
1707 *
|
danielebarchiesi@0
|
1708 * Arbitrary elements may be added using the $args associative array.
|
danielebarchiesi@0
|
1709 */
|
danielebarchiesi@0
|
1710 function format_rss_item($title, $link, $description, $args = array()) {
|
danielebarchiesi@0
|
1711 $output = "<item>\n";
|
danielebarchiesi@0
|
1712 $output .= ' <title>' . check_plain($title) . "</title>\n";
|
danielebarchiesi@0
|
1713 $output .= ' <link>' . check_url($link) . "</link>\n";
|
danielebarchiesi@0
|
1714 $output .= ' <description>' . check_plain($description) . "</description>\n";
|
danielebarchiesi@0
|
1715 $output .= format_xml_elements($args);
|
danielebarchiesi@0
|
1716 $output .= "</item>\n";
|
danielebarchiesi@0
|
1717
|
danielebarchiesi@0
|
1718 return $output;
|
danielebarchiesi@0
|
1719 }
|
danielebarchiesi@0
|
1720
|
danielebarchiesi@0
|
1721 /**
|
danielebarchiesi@0
|
1722 * Formats XML elements.
|
danielebarchiesi@0
|
1723 *
|
danielebarchiesi@0
|
1724 * @param $array
|
danielebarchiesi@0
|
1725 * An array where each item represents an element and is either a:
|
danielebarchiesi@0
|
1726 * - (key => value) pair (<key>value</key>)
|
danielebarchiesi@0
|
1727 * - Associative array with fields:
|
danielebarchiesi@0
|
1728 * - 'key': element name
|
danielebarchiesi@0
|
1729 * - 'value': element contents
|
danielebarchiesi@0
|
1730 * - 'attributes': associative array of element attributes
|
danielebarchiesi@0
|
1731 *
|
danielebarchiesi@0
|
1732 * In both cases, 'value' can be a simple string, or it can be another array
|
danielebarchiesi@0
|
1733 * with the same format as $array itself for nesting.
|
danielebarchiesi@0
|
1734 */
|
danielebarchiesi@0
|
1735 function format_xml_elements($array) {
|
danielebarchiesi@0
|
1736 $output = '';
|
danielebarchiesi@0
|
1737 foreach ($array as $key => $value) {
|
danielebarchiesi@0
|
1738 if (is_numeric($key)) {
|
danielebarchiesi@0
|
1739 if ($value['key']) {
|
danielebarchiesi@0
|
1740 $output .= ' <' . $value['key'];
|
danielebarchiesi@0
|
1741 if (isset($value['attributes']) && is_array($value['attributes'])) {
|
danielebarchiesi@0
|
1742 $output .= drupal_attributes($value['attributes']);
|
danielebarchiesi@0
|
1743 }
|
danielebarchiesi@0
|
1744
|
danielebarchiesi@0
|
1745 if (isset($value['value']) && $value['value'] != '') {
|
danielebarchiesi@0
|
1746 $output .= '>' . (is_array($value['value']) ? format_xml_elements($value['value']) : check_plain($value['value'])) . '</' . $value['key'] . ">\n";
|
danielebarchiesi@0
|
1747 }
|
danielebarchiesi@0
|
1748 else {
|
danielebarchiesi@0
|
1749 $output .= " />\n";
|
danielebarchiesi@0
|
1750 }
|
danielebarchiesi@0
|
1751 }
|
danielebarchiesi@0
|
1752 }
|
danielebarchiesi@0
|
1753 else {
|
danielebarchiesi@0
|
1754 $output .= ' <' . $key . '>' . (is_array($value) ? format_xml_elements($value) : check_plain($value)) . "</$key>\n";
|
danielebarchiesi@0
|
1755 }
|
danielebarchiesi@0
|
1756 }
|
danielebarchiesi@0
|
1757 return $output;
|
danielebarchiesi@0
|
1758 }
|
danielebarchiesi@0
|
1759
|
danielebarchiesi@0
|
1760 /**
|
danielebarchiesi@0
|
1761 * Formats a string containing a count of items.
|
danielebarchiesi@0
|
1762 *
|
danielebarchiesi@0
|
1763 * This function ensures that the string is pluralized correctly. Since t() is
|
danielebarchiesi@0
|
1764 * called by this function, make sure not to pass already-localized strings to
|
danielebarchiesi@0
|
1765 * it.
|
danielebarchiesi@0
|
1766 *
|
danielebarchiesi@0
|
1767 * For example:
|
danielebarchiesi@0
|
1768 * @code
|
danielebarchiesi@0
|
1769 * $output = format_plural($node->comment_count, '1 comment', '@count comments');
|
danielebarchiesi@0
|
1770 * @endcode
|
danielebarchiesi@0
|
1771 *
|
danielebarchiesi@0
|
1772 * Example with additional replacements:
|
danielebarchiesi@0
|
1773 * @code
|
danielebarchiesi@0
|
1774 * $output = format_plural($update_count,
|
danielebarchiesi@0
|
1775 * 'Changed the content type of 1 post from %old-type to %new-type.',
|
danielebarchiesi@0
|
1776 * 'Changed the content type of @count posts from %old-type to %new-type.',
|
danielebarchiesi@0
|
1777 * array('%old-type' => $info->old_type, '%new-type' => $info->new_type));
|
danielebarchiesi@0
|
1778 * @endcode
|
danielebarchiesi@0
|
1779 *
|
danielebarchiesi@0
|
1780 * @param $count
|
danielebarchiesi@0
|
1781 * The item count to display.
|
danielebarchiesi@0
|
1782 * @param $singular
|
danielebarchiesi@0
|
1783 * The string for the singular case. Make sure it is clear this is singular,
|
danielebarchiesi@0
|
1784 * to ease translation (e.g. use "1 new comment" instead of "1 new"). Do not
|
danielebarchiesi@0
|
1785 * use @count in the singular string.
|
danielebarchiesi@0
|
1786 * @param $plural
|
danielebarchiesi@0
|
1787 * The string for the plural case. Make sure it is clear this is plural, to
|
danielebarchiesi@0
|
1788 * ease translation. Use @count in place of the item count, as in
|
danielebarchiesi@0
|
1789 * "@count new comments".
|
danielebarchiesi@0
|
1790 * @param $args
|
danielebarchiesi@0
|
1791 * An associative array of replacements to make after translation. Instances
|
danielebarchiesi@0
|
1792 * of any key in this array are replaced with the corresponding value.
|
danielebarchiesi@0
|
1793 * Based on the first character of the key, the value is escaped and/or
|
danielebarchiesi@0
|
1794 * themed. See format_string(). Note that you do not need to include @count
|
danielebarchiesi@0
|
1795 * in this array; this replacement is done automatically for the plural case.
|
danielebarchiesi@0
|
1796 * @param $options
|
danielebarchiesi@0
|
1797 * An associative array of additional options. See t() for allowed keys.
|
danielebarchiesi@0
|
1798 *
|
danielebarchiesi@0
|
1799 * @return
|
danielebarchiesi@0
|
1800 * A translated string.
|
danielebarchiesi@0
|
1801 *
|
danielebarchiesi@0
|
1802 * @see t()
|
danielebarchiesi@0
|
1803 * @see format_string()
|
danielebarchiesi@0
|
1804 */
|
danielebarchiesi@0
|
1805 function format_plural($count, $singular, $plural, array $args = array(), array $options = array()) {
|
danielebarchiesi@0
|
1806 $args['@count'] = $count;
|
danielebarchiesi@0
|
1807 if ($count == 1) {
|
danielebarchiesi@0
|
1808 return t($singular, $args, $options);
|
danielebarchiesi@0
|
1809 }
|
danielebarchiesi@0
|
1810
|
danielebarchiesi@0
|
1811 // Get the plural index through the gettext formula.
|
danielebarchiesi@0
|
1812 $index = (function_exists('locale_get_plural')) ? locale_get_plural($count, isset($options['langcode']) ? $options['langcode'] : NULL) : -1;
|
danielebarchiesi@0
|
1813 // If the index cannot be computed, use the plural as a fallback (which
|
danielebarchiesi@0
|
1814 // allows for most flexiblity with the replaceable @count value).
|
danielebarchiesi@0
|
1815 if ($index < 0) {
|
danielebarchiesi@0
|
1816 return t($plural, $args, $options);
|
danielebarchiesi@0
|
1817 }
|
danielebarchiesi@0
|
1818 else {
|
danielebarchiesi@0
|
1819 switch ($index) {
|
danielebarchiesi@0
|
1820 case "0":
|
danielebarchiesi@0
|
1821 return t($singular, $args, $options);
|
danielebarchiesi@0
|
1822 case "1":
|
danielebarchiesi@0
|
1823 return t($plural, $args, $options);
|
danielebarchiesi@0
|
1824 default:
|
danielebarchiesi@0
|
1825 unset($args['@count']);
|
danielebarchiesi@0
|
1826 $args['@count[' . $index . ']'] = $count;
|
danielebarchiesi@0
|
1827 return t(strtr($plural, array('@count' => '@count[' . $index . ']')), $args, $options);
|
danielebarchiesi@0
|
1828 }
|
danielebarchiesi@0
|
1829 }
|
danielebarchiesi@0
|
1830 }
|
danielebarchiesi@0
|
1831
|
danielebarchiesi@0
|
1832 /**
|
danielebarchiesi@0
|
1833 * Parses a given byte count.
|
danielebarchiesi@0
|
1834 *
|
danielebarchiesi@0
|
1835 * @param $size
|
danielebarchiesi@0
|
1836 * A size expressed as a number of bytes with optional SI or IEC binary unit
|
danielebarchiesi@0
|
1837 * prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8 bytes, 9mbytes).
|
danielebarchiesi@0
|
1838 *
|
danielebarchiesi@0
|
1839 * @return
|
danielebarchiesi@0
|
1840 * An integer representation of the size in bytes.
|
danielebarchiesi@0
|
1841 */
|
danielebarchiesi@0
|
1842 function parse_size($size) {
|
danielebarchiesi@0
|
1843 $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
|
danielebarchiesi@0
|
1844 $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
|
danielebarchiesi@0
|
1845 if ($unit) {
|
danielebarchiesi@0
|
1846 // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
|
danielebarchiesi@0
|
1847 return round($size * pow(DRUPAL_KILOBYTE, stripos('bkmgtpezy', $unit[0])));
|
danielebarchiesi@0
|
1848 }
|
danielebarchiesi@0
|
1849 else {
|
danielebarchiesi@0
|
1850 return round($size);
|
danielebarchiesi@0
|
1851 }
|
danielebarchiesi@0
|
1852 }
|
danielebarchiesi@0
|
1853
|
danielebarchiesi@0
|
1854 /**
|
danielebarchiesi@0
|
1855 * Generates a string representation for the given byte count.
|
danielebarchiesi@0
|
1856 *
|
danielebarchiesi@0
|
1857 * @param $size
|
danielebarchiesi@0
|
1858 * A size in bytes.
|
danielebarchiesi@0
|
1859 * @param $langcode
|
danielebarchiesi@0
|
1860 * Optional language code to translate to a language other than what is used
|
danielebarchiesi@0
|
1861 * to display the page.
|
danielebarchiesi@0
|
1862 *
|
danielebarchiesi@0
|
1863 * @return
|
danielebarchiesi@0
|
1864 * A translated string representation of the size.
|
danielebarchiesi@0
|
1865 */
|
danielebarchiesi@0
|
1866 function format_size($size, $langcode = NULL) {
|
danielebarchiesi@0
|
1867 if ($size < DRUPAL_KILOBYTE) {
|
danielebarchiesi@0
|
1868 return format_plural($size, '1 byte', '@count bytes', array(), array('langcode' => $langcode));
|
danielebarchiesi@0
|
1869 }
|
danielebarchiesi@0
|
1870 else {
|
danielebarchiesi@0
|
1871 $size = $size / DRUPAL_KILOBYTE; // Convert bytes to kilobytes.
|
danielebarchiesi@0
|
1872 $units = array(
|
danielebarchiesi@0
|
1873 t('@size KB', array(), array('langcode' => $langcode)),
|
danielebarchiesi@0
|
1874 t('@size MB', array(), array('langcode' => $langcode)),
|
danielebarchiesi@0
|
1875 t('@size GB', array(), array('langcode' => $langcode)),
|
danielebarchiesi@0
|
1876 t('@size TB', array(), array('langcode' => $langcode)),
|
danielebarchiesi@0
|
1877 t('@size PB', array(), array('langcode' => $langcode)),
|
danielebarchiesi@0
|
1878 t('@size EB', array(), array('langcode' => $langcode)),
|
danielebarchiesi@0
|
1879 t('@size ZB', array(), array('langcode' => $langcode)),
|
danielebarchiesi@0
|
1880 t('@size YB', array(), array('langcode' => $langcode)),
|
danielebarchiesi@0
|
1881 );
|
danielebarchiesi@0
|
1882 foreach ($units as $unit) {
|
danielebarchiesi@0
|
1883 if (round($size, 2) >= DRUPAL_KILOBYTE) {
|
danielebarchiesi@0
|
1884 $size = $size / DRUPAL_KILOBYTE;
|
danielebarchiesi@0
|
1885 }
|
danielebarchiesi@0
|
1886 else {
|
danielebarchiesi@0
|
1887 break;
|
danielebarchiesi@0
|
1888 }
|
danielebarchiesi@0
|
1889 }
|
danielebarchiesi@0
|
1890 return str_replace('@size', round($size, 2), $unit);
|
danielebarchiesi@0
|
1891 }
|
danielebarchiesi@0
|
1892 }
|
danielebarchiesi@0
|
1893
|
danielebarchiesi@0
|
1894 /**
|
danielebarchiesi@0
|
1895 * Formats a time interval with the requested granularity.
|
danielebarchiesi@0
|
1896 *
|
danielebarchiesi@0
|
1897 * @param $interval
|
danielebarchiesi@0
|
1898 * The length of the interval in seconds.
|
danielebarchiesi@0
|
1899 * @param $granularity
|
danielebarchiesi@0
|
1900 * How many different units to display in the string.
|
danielebarchiesi@0
|
1901 * @param $langcode
|
danielebarchiesi@0
|
1902 * Optional language code to translate to a language other than
|
danielebarchiesi@0
|
1903 * what is used to display the page.
|
danielebarchiesi@0
|
1904 *
|
danielebarchiesi@0
|
1905 * @return
|
danielebarchiesi@0
|
1906 * A translated string representation of the interval.
|
danielebarchiesi@0
|
1907 */
|
danielebarchiesi@0
|
1908 function format_interval($interval, $granularity = 2, $langcode = NULL) {
|
danielebarchiesi@0
|
1909 $units = array(
|
danielebarchiesi@0
|
1910 '1 year|@count years' => 31536000,
|
danielebarchiesi@0
|
1911 '1 month|@count months' => 2592000,
|
danielebarchiesi@0
|
1912 '1 week|@count weeks' => 604800,
|
danielebarchiesi@0
|
1913 '1 day|@count days' => 86400,
|
danielebarchiesi@0
|
1914 '1 hour|@count hours' => 3600,
|
danielebarchiesi@0
|
1915 '1 min|@count min' => 60,
|
danielebarchiesi@0
|
1916 '1 sec|@count sec' => 1
|
danielebarchiesi@0
|
1917 );
|
danielebarchiesi@0
|
1918 $output = '';
|
danielebarchiesi@0
|
1919 foreach ($units as $key => $value) {
|
danielebarchiesi@0
|
1920 $key = explode('|', $key);
|
danielebarchiesi@0
|
1921 if ($interval >= $value) {
|
danielebarchiesi@0
|
1922 $output .= ($output ? ' ' : '') . format_plural(floor($interval / $value), $key[0], $key[1], array(), array('langcode' => $langcode));
|
danielebarchiesi@0
|
1923 $interval %= $value;
|
danielebarchiesi@0
|
1924 $granularity--;
|
danielebarchiesi@0
|
1925 }
|
danielebarchiesi@0
|
1926
|
danielebarchiesi@0
|
1927 if ($granularity == 0) {
|
danielebarchiesi@0
|
1928 break;
|
danielebarchiesi@0
|
1929 }
|
danielebarchiesi@0
|
1930 }
|
danielebarchiesi@0
|
1931 return $output ? $output : t('0 sec', array(), array('langcode' => $langcode));
|
danielebarchiesi@0
|
1932 }
|
danielebarchiesi@0
|
1933
|
danielebarchiesi@0
|
1934 /**
|
danielebarchiesi@0
|
1935 * Formats a date, using a date type or a custom date format string.
|
danielebarchiesi@0
|
1936 *
|
danielebarchiesi@0
|
1937 * @param $timestamp
|
danielebarchiesi@0
|
1938 * A UNIX timestamp to format.
|
danielebarchiesi@0
|
1939 * @param $type
|
danielebarchiesi@0
|
1940 * (optional) The format to use, one of:
|
danielebarchiesi@0
|
1941 * - 'short', 'medium', or 'long' (the corresponding built-in date formats).
|
danielebarchiesi@0
|
1942 * - The name of a date type defined by a module in hook_date_format_types(),
|
danielebarchiesi@0
|
1943 * if it's been assigned a format.
|
danielebarchiesi@0
|
1944 * - The machine name of an administrator-defined date format.
|
danielebarchiesi@0
|
1945 * - 'custom', to use $format.
|
danielebarchiesi@0
|
1946 * Defaults to 'medium'.
|
danielebarchiesi@0
|
1947 * @param $format
|
danielebarchiesi@0
|
1948 * (optional) If $type is 'custom', a PHP date format string suitable for
|
danielebarchiesi@0
|
1949 * input to date(). Use a backslash to escape ordinary text, so it does not
|
danielebarchiesi@0
|
1950 * get interpreted as date format characters.
|
danielebarchiesi@0
|
1951 * @param $timezone
|
danielebarchiesi@0
|
1952 * (optional) Time zone identifier, as described at
|
danielebarchiesi@0
|
1953 * http://php.net/manual/en/timezones.php Defaults to the time zone used to
|
danielebarchiesi@0
|
1954 * display the page.
|
danielebarchiesi@0
|
1955 * @param $langcode
|
danielebarchiesi@0
|
1956 * (optional) Language code to translate to. Defaults to the language used to
|
danielebarchiesi@0
|
1957 * display the page.
|
danielebarchiesi@0
|
1958 *
|
danielebarchiesi@0
|
1959 * @return
|
danielebarchiesi@0
|
1960 * A translated date string in the requested format.
|
danielebarchiesi@0
|
1961 */
|
danielebarchiesi@0
|
1962 function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
|
danielebarchiesi@0
|
1963 // Use the advanced drupal_static() pattern, since this is called very often.
|
danielebarchiesi@0
|
1964 static $drupal_static_fast;
|
danielebarchiesi@0
|
1965 if (!isset($drupal_static_fast)) {
|
danielebarchiesi@0
|
1966 $drupal_static_fast['timezones'] = &drupal_static(__FUNCTION__);
|
danielebarchiesi@0
|
1967 }
|
danielebarchiesi@0
|
1968 $timezones = &$drupal_static_fast['timezones'];
|
danielebarchiesi@0
|
1969
|
danielebarchiesi@0
|
1970 if (!isset($timezone)) {
|
danielebarchiesi@0
|
1971 $timezone = date_default_timezone_get();
|
danielebarchiesi@0
|
1972 }
|
danielebarchiesi@0
|
1973 // Store DateTimeZone objects in an array rather than repeatedly
|
danielebarchiesi@0
|
1974 // constructing identical objects over the life of a request.
|
danielebarchiesi@0
|
1975 if (!isset($timezones[$timezone])) {
|
danielebarchiesi@0
|
1976 $timezones[$timezone] = timezone_open($timezone);
|
danielebarchiesi@0
|
1977 }
|
danielebarchiesi@0
|
1978
|
danielebarchiesi@0
|
1979 // Use the default langcode if none is set.
|
danielebarchiesi@0
|
1980 global $language;
|
danielebarchiesi@0
|
1981 if (empty($langcode)) {
|
danielebarchiesi@0
|
1982 $langcode = isset($language->language) ? $language->language : 'en';
|
danielebarchiesi@0
|
1983 }
|
danielebarchiesi@0
|
1984
|
danielebarchiesi@0
|
1985 switch ($type) {
|
danielebarchiesi@0
|
1986 case 'short':
|
danielebarchiesi@0
|
1987 $format = variable_get('date_format_short', 'm/d/Y - H:i');
|
danielebarchiesi@0
|
1988 break;
|
danielebarchiesi@0
|
1989
|
danielebarchiesi@0
|
1990 case 'long':
|
danielebarchiesi@0
|
1991 $format = variable_get('date_format_long', 'l, F j, Y - H:i');
|
danielebarchiesi@0
|
1992 break;
|
danielebarchiesi@0
|
1993
|
danielebarchiesi@0
|
1994 case 'custom':
|
danielebarchiesi@0
|
1995 // No change to format.
|
danielebarchiesi@0
|
1996 break;
|
danielebarchiesi@0
|
1997
|
danielebarchiesi@0
|
1998 case 'medium':
|
danielebarchiesi@0
|
1999 default:
|
danielebarchiesi@0
|
2000 // Retrieve the format of the custom $type passed.
|
danielebarchiesi@0
|
2001 if ($type != 'medium') {
|
danielebarchiesi@0
|
2002 $format = variable_get('date_format_' . $type, '');
|
danielebarchiesi@0
|
2003 }
|
danielebarchiesi@0
|
2004 // Fall back to 'medium'.
|
danielebarchiesi@0
|
2005 if ($format === '') {
|
danielebarchiesi@0
|
2006 $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
|
danielebarchiesi@0
|
2007 }
|
danielebarchiesi@0
|
2008 break;
|
danielebarchiesi@0
|
2009 }
|
danielebarchiesi@0
|
2010
|
danielebarchiesi@0
|
2011 // Create a DateTime object from the timestamp.
|
danielebarchiesi@0
|
2012 $date_time = date_create('@' . $timestamp);
|
danielebarchiesi@0
|
2013 // Set the time zone for the DateTime object.
|
danielebarchiesi@0
|
2014 date_timezone_set($date_time, $timezones[$timezone]);
|
danielebarchiesi@0
|
2015
|
danielebarchiesi@0
|
2016 // Encode markers that should be translated. 'A' becomes '\xEF\AA\xFF'.
|
danielebarchiesi@0
|
2017 // xEF and xFF are invalid UTF-8 sequences, and we assume they are not in the
|
danielebarchiesi@0
|
2018 // input string.
|
danielebarchiesi@0
|
2019 // Paired backslashes are isolated to prevent errors in read-ahead evaluation.
|
danielebarchiesi@0
|
2020 // The read-ahead expression ensures that A matches, but not \A.
|
danielebarchiesi@0
|
2021 $format = preg_replace(array('/\\\\\\\\/', '/(?<!\\\\)([AaeDlMTF])/'), array("\xEF\\\\\\\\\xFF", "\xEF\\\\\$1\$1\xFF"), $format);
|
danielebarchiesi@0
|
2022
|
danielebarchiesi@0
|
2023 // Call date_format().
|
danielebarchiesi@0
|
2024 $format = date_format($date_time, $format);
|
danielebarchiesi@0
|
2025
|
danielebarchiesi@0
|
2026 // Pass the langcode to _format_date_callback().
|
danielebarchiesi@0
|
2027 _format_date_callback(NULL, $langcode);
|
danielebarchiesi@0
|
2028
|
danielebarchiesi@0
|
2029 // Translate the marked sequences.
|
danielebarchiesi@0
|
2030 return preg_replace_callback('/\xEF([AaeDlMTF]?)(.*?)\xFF/', '_format_date_callback', $format);
|
danielebarchiesi@0
|
2031 }
|
danielebarchiesi@0
|
2032
|
danielebarchiesi@0
|
2033 /**
|
danielebarchiesi@0
|
2034 * Returns an ISO8601 formatted date based on the given date.
|
danielebarchiesi@0
|
2035 *
|
danielebarchiesi@0
|
2036 * Callback for use within hook_rdf_mapping() implementations.
|
danielebarchiesi@0
|
2037 *
|
danielebarchiesi@0
|
2038 * @param $date
|
danielebarchiesi@0
|
2039 * A UNIX timestamp.
|
danielebarchiesi@0
|
2040 *
|
danielebarchiesi@0
|
2041 * @return string
|
danielebarchiesi@0
|
2042 * An ISO8601 formatted date.
|
danielebarchiesi@0
|
2043 */
|
danielebarchiesi@0
|
2044 function date_iso8601($date) {
|
danielebarchiesi@0
|
2045 // The DATE_ISO8601 constant cannot be used here because it does not match
|
danielebarchiesi@0
|
2046 // date('c') and produces invalid RDF markup.
|
danielebarchiesi@0
|
2047 return date('c', $date);
|
danielebarchiesi@0
|
2048 }
|
danielebarchiesi@0
|
2049
|
danielebarchiesi@0
|
2050 /**
|
danielebarchiesi@0
|
2051 * Translates a formatted date string.
|
danielebarchiesi@0
|
2052 *
|
danielebarchiesi@0
|
2053 * Callback for preg_replace_callback() within format_date().
|
danielebarchiesi@0
|
2054 */
|
danielebarchiesi@0
|
2055 function _format_date_callback(array $matches = NULL, $new_langcode = NULL) {
|
danielebarchiesi@0
|
2056 // We cache translations to avoid redundant and rather costly calls to t().
|
danielebarchiesi@0
|
2057 static $cache, $langcode;
|
danielebarchiesi@0
|
2058
|
danielebarchiesi@0
|
2059 if (!isset($matches)) {
|
danielebarchiesi@0
|
2060 $langcode = $new_langcode;
|
danielebarchiesi@0
|
2061 return;
|
danielebarchiesi@0
|
2062 }
|
danielebarchiesi@0
|
2063
|
danielebarchiesi@0
|
2064 $code = $matches[1];
|
danielebarchiesi@0
|
2065 $string = $matches[2];
|
danielebarchiesi@0
|
2066
|
danielebarchiesi@0
|
2067 if (!isset($cache[$langcode][$code][$string])) {
|
danielebarchiesi@0
|
2068 $options = array(
|
danielebarchiesi@0
|
2069 'langcode' => $langcode,
|
danielebarchiesi@0
|
2070 );
|
danielebarchiesi@0
|
2071
|
danielebarchiesi@0
|
2072 if ($code == 'F') {
|
danielebarchiesi@0
|
2073 $options['context'] = 'Long month name';
|
danielebarchiesi@0
|
2074 }
|
danielebarchiesi@0
|
2075
|
danielebarchiesi@0
|
2076 if ($code == '') {
|
danielebarchiesi@0
|
2077 $cache[$langcode][$code][$string] = $string;
|
danielebarchiesi@0
|
2078 }
|
danielebarchiesi@0
|
2079 else {
|
danielebarchiesi@0
|
2080 $cache[$langcode][$code][$string] = t($string, array(), $options);
|
danielebarchiesi@0
|
2081 }
|
danielebarchiesi@0
|
2082 }
|
danielebarchiesi@0
|
2083 return $cache[$langcode][$code][$string];
|
danielebarchiesi@0
|
2084 }
|
danielebarchiesi@0
|
2085
|
danielebarchiesi@0
|
2086 /**
|
danielebarchiesi@0
|
2087 * Format a username.
|
danielebarchiesi@0
|
2088 *
|
danielebarchiesi@0
|
2089 * This is also the label callback implementation of
|
danielebarchiesi@0
|
2090 * callback_entity_info_label() for user_entity_info().
|
danielebarchiesi@0
|
2091 *
|
danielebarchiesi@0
|
2092 * By default, the passed-in object's 'name' property is used if it exists, or
|
danielebarchiesi@0
|
2093 * else, the site-defined value for the 'anonymous' variable. However, a module
|
danielebarchiesi@0
|
2094 * may override this by implementing hook_username_alter(&$name, $account).
|
danielebarchiesi@0
|
2095 *
|
danielebarchiesi@0
|
2096 * @see hook_username_alter()
|
danielebarchiesi@0
|
2097 *
|
danielebarchiesi@0
|
2098 * @param $account
|
danielebarchiesi@0
|
2099 * The account object for the user whose name is to be formatted.
|
danielebarchiesi@0
|
2100 *
|
danielebarchiesi@0
|
2101 * @return
|
danielebarchiesi@0
|
2102 * An unsanitized string with the username to display. The code receiving
|
danielebarchiesi@0
|
2103 * this result must ensure that check_plain() is called on it before it is
|
danielebarchiesi@0
|
2104 * printed to the page.
|
danielebarchiesi@0
|
2105 */
|
danielebarchiesi@0
|
2106 function format_username($account) {
|
danielebarchiesi@0
|
2107 $name = !empty($account->name) ? $account->name : variable_get('anonymous', t('Anonymous'));
|
danielebarchiesi@0
|
2108 drupal_alter('username', $name, $account);
|
danielebarchiesi@0
|
2109 return $name;
|
danielebarchiesi@0
|
2110 }
|
danielebarchiesi@0
|
2111
|
danielebarchiesi@0
|
2112 /**
|
danielebarchiesi@0
|
2113 * @} End of "defgroup format".
|
danielebarchiesi@0
|
2114 */
|
danielebarchiesi@0
|
2115
|
danielebarchiesi@0
|
2116 /**
|
danielebarchiesi@0
|
2117 * Generates an internal or external URL.
|
danielebarchiesi@0
|
2118 *
|
danielebarchiesi@0
|
2119 * When creating links in modules, consider whether l() could be a better
|
danielebarchiesi@0
|
2120 * alternative than url().
|
danielebarchiesi@0
|
2121 *
|
danielebarchiesi@0
|
2122 * @param $path
|
danielebarchiesi@0
|
2123 * (optional) The internal path or external URL being linked to, such as
|
danielebarchiesi@0
|
2124 * "node/34" or "http://example.com/foo". The default value is equivalent to
|
danielebarchiesi@0
|
2125 * passing in '<front>'. A few notes:
|
danielebarchiesi@0
|
2126 * - If you provide a full URL, it will be considered an external URL.
|
danielebarchiesi@0
|
2127 * - If you provide only the path (e.g. "node/34"), it will be
|
danielebarchiesi@0
|
2128 * considered an internal link. In this case, it should be a system URL,
|
danielebarchiesi@0
|
2129 * and it will be replaced with the alias, if one exists. Additional query
|
danielebarchiesi@0
|
2130 * arguments for internal paths must be supplied in $options['query'], not
|
danielebarchiesi@0
|
2131 * included in $path.
|
danielebarchiesi@0
|
2132 * - If you provide an internal path and $options['alias'] is set to TRUE, the
|
danielebarchiesi@0
|
2133 * path is assumed already to be the correct path alias, and the alias is
|
danielebarchiesi@0
|
2134 * not looked up.
|
danielebarchiesi@0
|
2135 * - The special string '<front>' generates a link to the site's base URL.
|
danielebarchiesi@0
|
2136 * - If your external URL contains a query (e.g. http://example.com/foo?a=b),
|
danielebarchiesi@0
|
2137 * then you can either URL encode the query keys and values yourself and
|
danielebarchiesi@0
|
2138 * include them in $path, or use $options['query'] to let this function
|
danielebarchiesi@0
|
2139 * URL encode them.
|
danielebarchiesi@0
|
2140 * @param $options
|
danielebarchiesi@0
|
2141 * (optional) An associative array of additional options, with the following
|
danielebarchiesi@0
|
2142 * elements:
|
danielebarchiesi@0
|
2143 * - 'query': An array of query key/value-pairs (without any URL-encoding) to
|
danielebarchiesi@0
|
2144 * append to the URL.
|
danielebarchiesi@0
|
2145 * - 'fragment': A fragment identifier (named anchor) to append to the URL.
|
danielebarchiesi@0
|
2146 * Do not include the leading '#' character.
|
danielebarchiesi@0
|
2147 * - 'absolute': Defaults to FALSE. Whether to force the output to be an
|
danielebarchiesi@0
|
2148 * absolute link (beginning with http:). Useful for links that will be
|
danielebarchiesi@0
|
2149 * displayed outside the site, such as in an RSS feed.
|
danielebarchiesi@0
|
2150 * - 'alias': Defaults to FALSE. Whether the given path is a URL alias
|
danielebarchiesi@0
|
2151 * already.
|
danielebarchiesi@0
|
2152 * - 'external': Whether the given path is an external URL.
|
danielebarchiesi@0
|
2153 * - 'language': An optional language object. If the path being linked to is
|
danielebarchiesi@0
|
2154 * internal to the site, $options['language'] is used to look up the alias
|
danielebarchiesi@0
|
2155 * for the URL. If $options['language'] is omitted, the global $language_url
|
danielebarchiesi@0
|
2156 * will be used.
|
danielebarchiesi@0
|
2157 * - 'https': Whether this URL should point to a secure location. If not
|
danielebarchiesi@0
|
2158 * defined, the current scheme is used, so the user stays on HTTP or HTTPS
|
danielebarchiesi@0
|
2159 * respectively. TRUE enforces HTTPS and FALSE enforces HTTP, but HTTPS can
|
danielebarchiesi@0
|
2160 * only be enforced when the variable 'https' is set to TRUE.
|
danielebarchiesi@0
|
2161 * - 'base_url': Only used internally, to modify the base URL when a language
|
danielebarchiesi@0
|
2162 * dependent URL requires so.
|
danielebarchiesi@0
|
2163 * - 'prefix': Only used internally, to modify the path when a language
|
danielebarchiesi@0
|
2164 * dependent URL requires so.
|
danielebarchiesi@0
|
2165 * - 'script': The script filename in Drupal's root directory to use when
|
danielebarchiesi@0
|
2166 * clean URLs are disabled, such as 'index.php'. Defaults to an empty
|
danielebarchiesi@0
|
2167 * string, as most modern web servers automatically find 'index.php'. If
|
danielebarchiesi@0
|
2168 * clean URLs are disabled, the value of $path is appended as query
|
danielebarchiesi@0
|
2169 * parameter 'q' to $options['script'] in the returned URL. When deploying
|
danielebarchiesi@0
|
2170 * Drupal on a web server that cannot be configured to automatically find
|
danielebarchiesi@0
|
2171 * index.php, then hook_url_outbound_alter() can be implemented to force
|
danielebarchiesi@0
|
2172 * this value to 'index.php'.
|
danielebarchiesi@0
|
2173 * - 'entity_type': The entity type of the object that called url(). Only
|
danielebarchiesi@0
|
2174 * set if url() is invoked by entity_uri().
|
danielebarchiesi@0
|
2175 * - 'entity': The entity object (such as a node) for which the URL is being
|
danielebarchiesi@0
|
2176 * generated. Only set if url() is invoked by entity_uri().
|
danielebarchiesi@0
|
2177 *
|
danielebarchiesi@0
|
2178 * @return
|
danielebarchiesi@0
|
2179 * A string containing a URL to the given path.
|
danielebarchiesi@0
|
2180 */
|
danielebarchiesi@0
|
2181 function url($path = NULL, array $options = array()) {
|
danielebarchiesi@0
|
2182 // Merge in defaults.
|
danielebarchiesi@0
|
2183 $options += array(
|
danielebarchiesi@0
|
2184 'fragment' => '',
|
danielebarchiesi@0
|
2185 'query' => array(),
|
danielebarchiesi@0
|
2186 'absolute' => FALSE,
|
danielebarchiesi@0
|
2187 'alias' => FALSE,
|
danielebarchiesi@0
|
2188 'prefix' => ''
|
danielebarchiesi@0
|
2189 );
|
danielebarchiesi@0
|
2190
|
danielebarchiesi@0
|
2191 if (!isset($options['external'])) {
|
danielebarchiesi@0
|
2192 // Return an external link if $path contains an allowed absolute URL. Only
|
danielebarchiesi@0
|
2193 // call the slow drupal_strip_dangerous_protocols() if $path contains a ':'
|
danielebarchiesi@0
|
2194 // before any / ? or #. Note: we could use url_is_external($path) here, but
|
danielebarchiesi@0
|
2195 // that would require another function call, and performance inside url() is
|
danielebarchiesi@0
|
2196 // critical.
|
danielebarchiesi@0
|
2197 $colonpos = strpos($path, ':');
|
danielebarchiesi@0
|
2198 $options['external'] = ($colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos)) && drupal_strip_dangerous_protocols($path) == $path);
|
danielebarchiesi@0
|
2199 }
|
danielebarchiesi@0
|
2200
|
danielebarchiesi@0
|
2201 // Preserve the original path before altering or aliasing.
|
danielebarchiesi@0
|
2202 $original_path = $path;
|
danielebarchiesi@0
|
2203
|
danielebarchiesi@0
|
2204 // Allow other modules to alter the outbound URL and options.
|
danielebarchiesi@0
|
2205 drupal_alter('url_outbound', $path, $options, $original_path);
|
danielebarchiesi@0
|
2206
|
danielebarchiesi@0
|
2207 if (isset($options['fragment']) && $options['fragment'] !== '') {
|
danielebarchiesi@0
|
2208 $options['fragment'] = '#' . $options['fragment'];
|
danielebarchiesi@0
|
2209 }
|
danielebarchiesi@0
|
2210
|
danielebarchiesi@0
|
2211 if ($options['external']) {
|
danielebarchiesi@0
|
2212 // Split off the fragment.
|
danielebarchiesi@0
|
2213 if (strpos($path, '#') !== FALSE) {
|
danielebarchiesi@0
|
2214 list($path, $old_fragment) = explode('#', $path, 2);
|
danielebarchiesi@0
|
2215 // If $options contains no fragment, take it over from the path.
|
danielebarchiesi@0
|
2216 if (isset($old_fragment) && !$options['fragment']) {
|
danielebarchiesi@0
|
2217 $options['fragment'] = '#' . $old_fragment;
|
danielebarchiesi@0
|
2218 }
|
danielebarchiesi@0
|
2219 }
|
danielebarchiesi@0
|
2220 // Append the query.
|
danielebarchiesi@0
|
2221 if ($options['query']) {
|
danielebarchiesi@0
|
2222 $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . drupal_http_build_query($options['query']);
|
danielebarchiesi@0
|
2223 }
|
danielebarchiesi@0
|
2224 if (isset($options['https']) && variable_get('https', FALSE)) {
|
danielebarchiesi@0
|
2225 if ($options['https'] === TRUE) {
|
danielebarchiesi@0
|
2226 $path = str_replace('http://', 'https://', $path);
|
danielebarchiesi@0
|
2227 }
|
danielebarchiesi@0
|
2228 elseif ($options['https'] === FALSE) {
|
danielebarchiesi@0
|
2229 $path = str_replace('https://', 'http://', $path);
|
danielebarchiesi@0
|
2230 }
|
danielebarchiesi@0
|
2231 }
|
danielebarchiesi@0
|
2232 // Reassemble.
|
danielebarchiesi@0
|
2233 return $path . $options['fragment'];
|
danielebarchiesi@0
|
2234 }
|
danielebarchiesi@0
|
2235
|
danielebarchiesi@0
|
2236 global $base_url, $base_secure_url, $base_insecure_url;
|
danielebarchiesi@0
|
2237
|
danielebarchiesi@0
|
2238 // The base_url might be rewritten from the language rewrite in domain mode.
|
danielebarchiesi@0
|
2239 if (!isset($options['base_url'])) {
|
danielebarchiesi@0
|
2240 if (isset($options['https']) && variable_get('https', FALSE)) {
|
danielebarchiesi@0
|
2241 if ($options['https'] === TRUE) {
|
danielebarchiesi@0
|
2242 $options['base_url'] = $base_secure_url;
|
danielebarchiesi@0
|
2243 $options['absolute'] = TRUE;
|
danielebarchiesi@0
|
2244 }
|
danielebarchiesi@0
|
2245 elseif ($options['https'] === FALSE) {
|
danielebarchiesi@0
|
2246 $options['base_url'] = $base_insecure_url;
|
danielebarchiesi@0
|
2247 $options['absolute'] = TRUE;
|
danielebarchiesi@0
|
2248 }
|
danielebarchiesi@0
|
2249 }
|
danielebarchiesi@0
|
2250 else {
|
danielebarchiesi@0
|
2251 $options['base_url'] = $base_url;
|
danielebarchiesi@0
|
2252 }
|
danielebarchiesi@0
|
2253 }
|
danielebarchiesi@0
|
2254
|
danielebarchiesi@0
|
2255 // The special path '<front>' links to the default front page.
|
danielebarchiesi@0
|
2256 if ($path == '<front>') {
|
danielebarchiesi@0
|
2257 $path = '';
|
danielebarchiesi@0
|
2258 }
|
danielebarchiesi@0
|
2259 elseif (!empty($path) && !$options['alias']) {
|
danielebarchiesi@0
|
2260 $language = isset($options['language']) && isset($options['language']->language) ? $options['language']->language : '';
|
danielebarchiesi@0
|
2261 $alias = drupal_get_path_alias($original_path, $language);
|
danielebarchiesi@0
|
2262 if ($alias != $original_path) {
|
danielebarchiesi@0
|
2263 $path = $alias;
|
danielebarchiesi@0
|
2264 }
|
danielebarchiesi@0
|
2265 }
|
danielebarchiesi@0
|
2266
|
danielebarchiesi@0
|
2267 $base = $options['absolute'] ? $options['base_url'] . '/' : base_path();
|
danielebarchiesi@0
|
2268 $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix'];
|
danielebarchiesi@0
|
2269
|
danielebarchiesi@0
|
2270 // With Clean URLs.
|
danielebarchiesi@0
|
2271 if (!empty($GLOBALS['conf']['clean_url'])) {
|
danielebarchiesi@0
|
2272 $path = drupal_encode_path($prefix . $path);
|
danielebarchiesi@0
|
2273 if ($options['query']) {
|
danielebarchiesi@0
|
2274 return $base . $path . '?' . drupal_http_build_query($options['query']) . $options['fragment'];
|
danielebarchiesi@0
|
2275 }
|
danielebarchiesi@0
|
2276 else {
|
danielebarchiesi@0
|
2277 return $base . $path . $options['fragment'];
|
danielebarchiesi@0
|
2278 }
|
danielebarchiesi@0
|
2279 }
|
danielebarchiesi@0
|
2280 // Without Clean URLs.
|
danielebarchiesi@0
|
2281 else {
|
danielebarchiesi@0
|
2282 $path = $prefix . $path;
|
danielebarchiesi@0
|
2283 $query = array();
|
danielebarchiesi@0
|
2284 if (!empty($path)) {
|
danielebarchiesi@0
|
2285 $query['q'] = $path;
|
danielebarchiesi@0
|
2286 }
|
danielebarchiesi@0
|
2287 if ($options['query']) {
|
danielebarchiesi@0
|
2288 // We do not use array_merge() here to prevent overriding $path via query
|
danielebarchiesi@0
|
2289 // parameters.
|
danielebarchiesi@0
|
2290 $query += $options['query'];
|
danielebarchiesi@0
|
2291 }
|
danielebarchiesi@0
|
2292 $query = $query ? ('?' . drupal_http_build_query($query)) : '';
|
danielebarchiesi@0
|
2293 $script = isset($options['script']) ? $options['script'] : '';
|
danielebarchiesi@0
|
2294 return $base . $script . $query . $options['fragment'];
|
danielebarchiesi@0
|
2295 }
|
danielebarchiesi@0
|
2296 }
|
danielebarchiesi@0
|
2297
|
danielebarchiesi@0
|
2298 /**
|
danielebarchiesi@0
|
2299 * Returns TRUE if a path is external to Drupal (e.g. http://example.com).
|
danielebarchiesi@0
|
2300 *
|
danielebarchiesi@0
|
2301 * If a path cannot be assessed by Drupal's menu handler, then we must
|
danielebarchiesi@0
|
2302 * treat it as potentially insecure.
|
danielebarchiesi@0
|
2303 *
|
danielebarchiesi@0
|
2304 * @param $path
|
danielebarchiesi@0
|
2305 * The internal path or external URL being linked to, such as "node/34" or
|
danielebarchiesi@0
|
2306 * "http://example.com/foo".
|
danielebarchiesi@0
|
2307 *
|
danielebarchiesi@0
|
2308 * @return
|
danielebarchiesi@0
|
2309 * Boolean TRUE or FALSE, where TRUE indicates an external path.
|
danielebarchiesi@0
|
2310 */
|
danielebarchiesi@0
|
2311 function url_is_external($path) {
|
danielebarchiesi@0
|
2312 $colonpos = strpos($path, ':');
|
danielebarchiesi@0
|
2313 // Avoid calling drupal_strip_dangerous_protocols() if there is any
|
danielebarchiesi@0
|
2314 // slash (/), hash (#) or question_mark (?) before the colon (:)
|
danielebarchiesi@0
|
2315 // occurrence - if any - as this would clearly mean it is not a URL.
|
danielebarchiesi@0
|
2316 return $colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos)) && drupal_strip_dangerous_protocols($path) == $path;
|
danielebarchiesi@0
|
2317 }
|
danielebarchiesi@0
|
2318
|
danielebarchiesi@0
|
2319 /**
|
danielebarchiesi@0
|
2320 * Formats an attribute string for an HTTP header.
|
danielebarchiesi@0
|
2321 *
|
danielebarchiesi@0
|
2322 * @param $attributes
|
danielebarchiesi@0
|
2323 * An associative array of attributes such as 'rel'.
|
danielebarchiesi@0
|
2324 *
|
danielebarchiesi@0
|
2325 * @return
|
danielebarchiesi@0
|
2326 * A ; separated string ready for insertion in a HTTP header. No escaping is
|
danielebarchiesi@0
|
2327 * performed for HTML entities, so this string is not safe to be printed.
|
danielebarchiesi@0
|
2328 *
|
danielebarchiesi@0
|
2329 * @see drupal_add_http_header()
|
danielebarchiesi@0
|
2330 */
|
danielebarchiesi@0
|
2331 function drupal_http_header_attributes(array $attributes = array()) {
|
danielebarchiesi@0
|
2332 foreach ($attributes as $attribute => &$data) {
|
danielebarchiesi@0
|
2333 if (is_array($data)) {
|
danielebarchiesi@0
|
2334 $data = implode(' ', $data);
|
danielebarchiesi@0
|
2335 }
|
danielebarchiesi@0
|
2336 $data = $attribute . '="' . $data . '"';
|
danielebarchiesi@0
|
2337 }
|
danielebarchiesi@0
|
2338 return $attributes ? ' ' . implode('; ', $attributes) : '';
|
danielebarchiesi@0
|
2339 }
|
danielebarchiesi@0
|
2340
|
danielebarchiesi@0
|
2341 /**
|
danielebarchiesi@0
|
2342 * Converts an associative array to an XML/HTML tag attribute string.
|
danielebarchiesi@0
|
2343 *
|
danielebarchiesi@0
|
2344 * Each array key and its value will be formatted into an attribute string.
|
danielebarchiesi@0
|
2345 * If a value is itself an array, then its elements are concatenated to a single
|
danielebarchiesi@0
|
2346 * space-delimited string (for example, a class attribute with multiple values).
|
danielebarchiesi@0
|
2347 *
|
danielebarchiesi@0
|
2348 * Attribute values are sanitized by running them through check_plain().
|
danielebarchiesi@0
|
2349 * Attribute names are not automatically sanitized. When using user-supplied
|
danielebarchiesi@0
|
2350 * attribute names, it is strongly recommended to allow only white-listed names,
|
danielebarchiesi@0
|
2351 * since certain attributes carry security risks and can be abused.
|
danielebarchiesi@0
|
2352 *
|
danielebarchiesi@0
|
2353 * Examples of security aspects when using drupal_attributes:
|
danielebarchiesi@0
|
2354 * @code
|
danielebarchiesi@0
|
2355 * // By running the value in the following statement through check_plain,
|
danielebarchiesi@0
|
2356 * // the malicious script is neutralized.
|
danielebarchiesi@0
|
2357 * drupal_attributes(array('title' => t('<script>steal_cookie();</script>')));
|
danielebarchiesi@0
|
2358 *
|
danielebarchiesi@0
|
2359 * // The statement below demonstrates dangerous use of drupal_attributes, and
|
danielebarchiesi@0
|
2360 * // will return an onmouseout attribute with JavaScript code that, when used
|
danielebarchiesi@0
|
2361 * // as attribute in a tag, will cause users to be redirected to another site.
|
danielebarchiesi@0
|
2362 * //
|
danielebarchiesi@0
|
2363 * // In this case, the 'onmouseout' attribute should not be whitelisted --
|
danielebarchiesi@0
|
2364 * // you don't want users to have the ability to add this attribute or others
|
danielebarchiesi@0
|
2365 * // that take JavaScript commands.
|
danielebarchiesi@0
|
2366 * drupal_attributes(array('onmouseout' => 'window.location="http://malicious.com/";')));
|
danielebarchiesi@0
|
2367 * @endcode
|
danielebarchiesi@0
|
2368 *
|
danielebarchiesi@0
|
2369 * @param $attributes
|
danielebarchiesi@0
|
2370 * An associative array of key-value pairs to be converted to attributes.
|
danielebarchiesi@0
|
2371 *
|
danielebarchiesi@0
|
2372 * @return
|
danielebarchiesi@0
|
2373 * A string ready for insertion in a tag (starts with a space).
|
danielebarchiesi@0
|
2374 *
|
danielebarchiesi@0
|
2375 * @ingroup sanitization
|
danielebarchiesi@0
|
2376 */
|
danielebarchiesi@0
|
2377 function drupal_attributes(array $attributes = array()) {
|
danielebarchiesi@0
|
2378 foreach ($attributes as $attribute => &$data) {
|
danielebarchiesi@0
|
2379 $data = implode(' ', (array) $data);
|
danielebarchiesi@0
|
2380 $data = $attribute . '="' . check_plain($data) . '"';
|
danielebarchiesi@0
|
2381 }
|
danielebarchiesi@0
|
2382 return $attributes ? ' ' . implode(' ', $attributes) : '';
|
danielebarchiesi@0
|
2383 }
|
danielebarchiesi@0
|
2384
|
danielebarchiesi@0
|
2385 /**
|
danielebarchiesi@0
|
2386 * Formats an internal or external URL link as an HTML anchor tag.
|
danielebarchiesi@0
|
2387 *
|
danielebarchiesi@0
|
2388 * This function correctly handles aliased paths and adds an 'active' class
|
danielebarchiesi@0
|
2389 * attribute to links that point to the current page (for theming), so all
|
danielebarchiesi@0
|
2390 * internal links output by modules should be generated by this function if
|
danielebarchiesi@0
|
2391 * possible.
|
danielebarchiesi@0
|
2392 *
|
danielebarchiesi@0
|
2393 * However, for links enclosed in translatable text you should use t() and
|
danielebarchiesi@0
|
2394 * embed the HTML anchor tag directly in the translated string. For example:
|
danielebarchiesi@0
|
2395 * @code
|
danielebarchiesi@0
|
2396 * t('Visit the <a href="@url">settings</a> page', array('@url' => url('admin')));
|
danielebarchiesi@0
|
2397 * @endcode
|
danielebarchiesi@0
|
2398 * This keeps the context of the link title ('settings' in the example) for
|
danielebarchiesi@0
|
2399 * translators.
|
danielebarchiesi@0
|
2400 *
|
danielebarchiesi@0
|
2401 * @param string $text
|
danielebarchiesi@0
|
2402 * The translated link text for the anchor tag.
|
danielebarchiesi@0
|
2403 * @param string $path
|
danielebarchiesi@0
|
2404 * The internal path or external URL being linked to, such as "node/34" or
|
danielebarchiesi@0
|
2405 * "http://example.com/foo". After the url() function is called to construct
|
danielebarchiesi@0
|
2406 * the URL from $path and $options, the resulting URL is passed through
|
danielebarchiesi@0
|
2407 * check_plain() before it is inserted into the HTML anchor tag, to ensure
|
danielebarchiesi@0
|
2408 * well-formed HTML. See url() for more information and notes.
|
danielebarchiesi@0
|
2409 * @param array $options
|
danielebarchiesi@0
|
2410 * An associative array of additional options. Defaults to an empty array. It
|
danielebarchiesi@0
|
2411 * may contain the following elements.
|
danielebarchiesi@0
|
2412 * - 'attributes': An associative array of HTML attributes to apply to the
|
danielebarchiesi@0
|
2413 * anchor tag. If element 'class' is included, it must be an array; 'title'
|
danielebarchiesi@0
|
2414 * must be a string; other elements are more flexible, as they just need
|
danielebarchiesi@0
|
2415 * to work in a call to drupal_attributes($options['attributes']).
|
danielebarchiesi@0
|
2416 * - 'html' (default FALSE): Whether $text is HTML or just plain-text. For
|
danielebarchiesi@0
|
2417 * example, to make an image tag into a link, this must be set to TRUE, or
|
danielebarchiesi@0
|
2418 * you will see the escaped HTML image tag. $text is not sanitized if
|
danielebarchiesi@0
|
2419 * 'html' is TRUE. The calling function must ensure that $text is already
|
danielebarchiesi@0
|
2420 * safe.
|
danielebarchiesi@0
|
2421 * - 'language': An optional language object. If the path being linked to is
|
danielebarchiesi@0
|
2422 * internal to the site, $options['language'] is used to determine whether
|
danielebarchiesi@0
|
2423 * the link is "active", or pointing to the current page (the language as
|
danielebarchiesi@0
|
2424 * well as the path must match). This element is also used by url().
|
danielebarchiesi@0
|
2425 * - Additional $options elements used by the url() function.
|
danielebarchiesi@0
|
2426 *
|
danielebarchiesi@0
|
2427 * @return string
|
danielebarchiesi@0
|
2428 * An HTML string containing a link to the given path.
|
danielebarchiesi@0
|
2429 *
|
danielebarchiesi@0
|
2430 * @see url()
|
danielebarchiesi@0
|
2431 */
|
danielebarchiesi@0
|
2432 function l($text, $path, array $options = array()) {
|
danielebarchiesi@0
|
2433 global $language_url;
|
danielebarchiesi@0
|
2434 static $use_theme = NULL;
|
danielebarchiesi@0
|
2435
|
danielebarchiesi@0
|
2436 // Merge in defaults.
|
danielebarchiesi@0
|
2437 $options += array(
|
danielebarchiesi@0
|
2438 'attributes' => array(),
|
danielebarchiesi@0
|
2439 'html' => FALSE,
|
danielebarchiesi@0
|
2440 );
|
danielebarchiesi@0
|
2441
|
danielebarchiesi@0
|
2442 // Append active class.
|
danielebarchiesi@0
|
2443 if (($path == $_GET['q'] || ($path == '<front>' && drupal_is_front_page())) &&
|
danielebarchiesi@0
|
2444 (empty($options['language']) || $options['language']->language == $language_url->language)) {
|
danielebarchiesi@0
|
2445 $options['attributes']['class'][] = 'active';
|
danielebarchiesi@0
|
2446 }
|
danielebarchiesi@0
|
2447
|
danielebarchiesi@0
|
2448 // Remove all HTML and PHP tags from a tooltip. For best performance, we act only
|
danielebarchiesi@0
|
2449 // if a quick strpos() pre-check gave a suspicion (because strip_tags() is expensive).
|
danielebarchiesi@0
|
2450 if (isset($options['attributes']['title']) && strpos($options['attributes']['title'], '<') !== FALSE) {
|
danielebarchiesi@0
|
2451 $options['attributes']['title'] = strip_tags($options['attributes']['title']);
|
danielebarchiesi@0
|
2452 }
|
danielebarchiesi@0
|
2453
|
danielebarchiesi@0
|
2454 // Determine if rendering of the link is to be done with a theme function
|
danielebarchiesi@0
|
2455 // or the inline default. Inline is faster, but if the theme system has been
|
danielebarchiesi@0
|
2456 // loaded and a module or theme implements a preprocess or process function
|
danielebarchiesi@0
|
2457 // or overrides the theme_link() function, then invoke theme(). Preliminary
|
danielebarchiesi@0
|
2458 // benchmarks indicate that invoking theme() can slow down the l() function
|
danielebarchiesi@0
|
2459 // by 20% or more, and that some of the link-heavy Drupal pages spend more
|
danielebarchiesi@0
|
2460 // than 10% of the total page request time in the l() function.
|
danielebarchiesi@0
|
2461 if (!isset($use_theme) && function_exists('theme')) {
|
danielebarchiesi@0
|
2462 // Allow edge cases to prevent theme initialization and force inline link
|
danielebarchiesi@0
|
2463 // rendering.
|
danielebarchiesi@0
|
2464 if (variable_get('theme_link', TRUE)) {
|
danielebarchiesi@0
|
2465 drupal_theme_initialize();
|
danielebarchiesi@0
|
2466 $registry = theme_get_registry(FALSE);
|
danielebarchiesi@0
|
2467 // We don't want to duplicate functionality that's in theme(), so any
|
danielebarchiesi@0
|
2468 // hint of a module or theme doing anything at all special with the 'link'
|
danielebarchiesi@0
|
2469 // theme hook should simply result in theme() being called. This includes
|
danielebarchiesi@0
|
2470 // the overriding of theme_link() with an alternate function or template,
|
danielebarchiesi@0
|
2471 // the presence of preprocess or process functions, or the presence of
|
danielebarchiesi@0
|
2472 // include files.
|
danielebarchiesi@0
|
2473 $use_theme = !isset($registry['link']['function']) || ($registry['link']['function'] != 'theme_link');
|
danielebarchiesi@0
|
2474 $use_theme = $use_theme || !empty($registry['link']['preprocess functions']) || !empty($registry['link']['process functions']) || !empty($registry['link']['includes']);
|
danielebarchiesi@0
|
2475 }
|
danielebarchiesi@0
|
2476 else {
|
danielebarchiesi@0
|
2477 $use_theme = FALSE;
|
danielebarchiesi@0
|
2478 }
|
danielebarchiesi@0
|
2479 }
|
danielebarchiesi@0
|
2480 if ($use_theme) {
|
danielebarchiesi@0
|
2481 return theme('link', array('text' => $text, 'path' => $path, 'options' => $options));
|
danielebarchiesi@0
|
2482 }
|
danielebarchiesi@0
|
2483 // The result of url() is a plain-text URL. Because we are using it here
|
danielebarchiesi@0
|
2484 // in an HTML argument context, we need to encode it properly.
|
danielebarchiesi@0
|
2485 return '<a href="' . check_plain(url($path, $options)) . '"' . drupal_attributes($options['attributes']) . '>' . ($options['html'] ? $text : check_plain($text)) . '</a>';
|
danielebarchiesi@0
|
2486 }
|
danielebarchiesi@0
|
2487
|
danielebarchiesi@0
|
2488 /**
|
danielebarchiesi@0
|
2489 * Delivers a page callback result to the browser in the appropriate format.
|
danielebarchiesi@0
|
2490 *
|
danielebarchiesi@0
|
2491 * This function is most commonly called by menu_execute_active_handler(), but
|
danielebarchiesi@0
|
2492 * can also be called by error conditions such as drupal_not_found(),
|
danielebarchiesi@0
|
2493 * drupal_access_denied(), and drupal_site_offline().
|
danielebarchiesi@0
|
2494 *
|
danielebarchiesi@0
|
2495 * When a user requests a page, index.php calls menu_execute_active_handler(),
|
danielebarchiesi@0
|
2496 * which calls the 'page callback' function registered in hook_menu(). The page
|
danielebarchiesi@0
|
2497 * callback function can return one of:
|
danielebarchiesi@0
|
2498 * - NULL: to indicate no content.
|
danielebarchiesi@0
|
2499 * - An integer menu status constant: to indicate an error condition.
|
danielebarchiesi@0
|
2500 * - A string of HTML content.
|
danielebarchiesi@0
|
2501 * - A renderable array of content.
|
danielebarchiesi@0
|
2502 * Returning a renderable array rather than a string of HTML is preferred,
|
danielebarchiesi@0
|
2503 * because that provides modules with more flexibility in customizing the final
|
danielebarchiesi@0
|
2504 * result.
|
danielebarchiesi@0
|
2505 *
|
danielebarchiesi@0
|
2506 * When the page callback returns its constructed content to
|
danielebarchiesi@0
|
2507 * menu_execute_active_handler(), this function gets called. The purpose of
|
danielebarchiesi@0
|
2508 * this function is to determine the most appropriate 'delivery callback'
|
danielebarchiesi@0
|
2509 * function to route the content to. The delivery callback function then
|
danielebarchiesi@0
|
2510 * sends the content to the browser in the needed format. The default delivery
|
danielebarchiesi@0
|
2511 * callback is drupal_deliver_html_page(), which delivers the content as an HTML
|
danielebarchiesi@0
|
2512 * page, complete with blocks in addition to the content. This default can be
|
danielebarchiesi@0
|
2513 * overridden on a per menu router item basis by setting 'delivery callback' in
|
danielebarchiesi@0
|
2514 * hook_menu() or hook_menu_alter(), and can also be overridden on a per request
|
danielebarchiesi@0
|
2515 * basis in hook_page_delivery_callback_alter().
|
danielebarchiesi@0
|
2516 *
|
danielebarchiesi@0
|
2517 * For example, the same page callback function can be used for an HTML
|
danielebarchiesi@0
|
2518 * version of the page and an Ajax version of the page. The page callback
|
danielebarchiesi@0
|
2519 * function just needs to decide what content is to be returned and the
|
danielebarchiesi@0
|
2520 * delivery callback function will send it as an HTML page or an Ajax
|
danielebarchiesi@0
|
2521 * response, as appropriate.
|
danielebarchiesi@0
|
2522 *
|
danielebarchiesi@0
|
2523 * In order for page callbacks to be reusable in different delivery formats,
|
danielebarchiesi@0
|
2524 * they should not issue any "print" or "echo" statements, but instead just
|
danielebarchiesi@0
|
2525 * return content.
|
danielebarchiesi@0
|
2526 *
|
danielebarchiesi@0
|
2527 * Also note that this function does not perform access checks. The delivery
|
danielebarchiesi@0
|
2528 * callback function specified in hook_menu(), hook_menu_alter(), or
|
danielebarchiesi@0
|
2529 * hook_page_delivery_callback_alter() will be called even if the router item
|
danielebarchiesi@0
|
2530 * access checks fail. This is intentional (it is needed for JSON and other
|
danielebarchiesi@0
|
2531 * purposes), but it has security implications. Do not call this function
|
danielebarchiesi@0
|
2532 * directly unless you understand the security implications, and be careful in
|
danielebarchiesi@0
|
2533 * writing delivery callbacks, so that they do not violate security. See
|
danielebarchiesi@0
|
2534 * drupal_deliver_html_page() for an example of a delivery callback that
|
danielebarchiesi@0
|
2535 * respects security.
|
danielebarchiesi@0
|
2536 *
|
danielebarchiesi@0
|
2537 * @param $page_callback_result
|
danielebarchiesi@0
|
2538 * The result of a page callback. Can be one of:
|
danielebarchiesi@0
|
2539 * - NULL: to indicate no content.
|
danielebarchiesi@0
|
2540 * - An integer menu status constant: to indicate an error condition.
|
danielebarchiesi@0
|
2541 * - A string of HTML content.
|
danielebarchiesi@0
|
2542 * - A renderable array of content.
|
danielebarchiesi@0
|
2543 * @param $default_delivery_callback
|
danielebarchiesi@0
|
2544 * (Optional) If given, it is the name of a delivery function most likely
|
danielebarchiesi@0
|
2545 * to be appropriate for the page request as determined by the calling
|
danielebarchiesi@0
|
2546 * function (e.g., menu_execute_active_handler()). If not given, it is
|
danielebarchiesi@0
|
2547 * determined from the menu router information of the current page.
|
danielebarchiesi@0
|
2548 *
|
danielebarchiesi@0
|
2549 * @see menu_execute_active_handler()
|
danielebarchiesi@0
|
2550 * @see hook_menu()
|
danielebarchiesi@0
|
2551 * @see hook_menu_alter()
|
danielebarchiesi@0
|
2552 * @see hook_page_delivery_callback_alter()
|
danielebarchiesi@0
|
2553 */
|
danielebarchiesi@0
|
2554 function drupal_deliver_page($page_callback_result, $default_delivery_callback = NULL) {
|
danielebarchiesi@0
|
2555 if (!isset($default_delivery_callback) && ($router_item = menu_get_item())) {
|
danielebarchiesi@0
|
2556 $default_delivery_callback = $router_item['delivery_callback'];
|
danielebarchiesi@0
|
2557 }
|
danielebarchiesi@0
|
2558 $delivery_callback = !empty($default_delivery_callback) ? $default_delivery_callback : 'drupal_deliver_html_page';
|
danielebarchiesi@0
|
2559 // Give modules a chance to alter the delivery callback used, based on
|
danielebarchiesi@0
|
2560 // request-time context (e.g., HTTP request headers).
|
danielebarchiesi@0
|
2561 drupal_alter('page_delivery_callback', $delivery_callback);
|
danielebarchiesi@0
|
2562 if (function_exists($delivery_callback)) {
|
danielebarchiesi@0
|
2563 $delivery_callback($page_callback_result);
|
danielebarchiesi@0
|
2564 }
|
danielebarchiesi@0
|
2565 else {
|
danielebarchiesi@0
|
2566 // If a delivery callback is specified, but doesn't exist as a function,
|
danielebarchiesi@0
|
2567 // something is wrong, but don't print anything, since it's not known
|
danielebarchiesi@0
|
2568 // what format the response needs to be in.
|
danielebarchiesi@0
|
2569 watchdog('delivery callback not found', 'callback %callback not found: %q.', array('%callback' => $delivery_callback, '%q' => $_GET['q']), WATCHDOG_ERROR);
|
danielebarchiesi@0
|
2570 }
|
danielebarchiesi@0
|
2571 }
|
danielebarchiesi@0
|
2572
|
danielebarchiesi@0
|
2573 /**
|
danielebarchiesi@0
|
2574 * Packages and sends the result of a page callback to the browser as HTML.
|
danielebarchiesi@0
|
2575 *
|
danielebarchiesi@0
|
2576 * @param $page_callback_result
|
danielebarchiesi@0
|
2577 * The result of a page callback. Can be one of:
|
danielebarchiesi@0
|
2578 * - NULL: to indicate no content.
|
danielebarchiesi@0
|
2579 * - An integer menu status constant: to indicate an error condition.
|
danielebarchiesi@0
|
2580 * - A string of HTML content.
|
danielebarchiesi@0
|
2581 * - A renderable array of content.
|
danielebarchiesi@0
|
2582 *
|
danielebarchiesi@0
|
2583 * @see drupal_deliver_page()
|
danielebarchiesi@0
|
2584 */
|
danielebarchiesi@0
|
2585 function drupal_deliver_html_page($page_callback_result) {
|
danielebarchiesi@0
|
2586 // Emit the correct charset HTTP header, but not if the page callback
|
danielebarchiesi@0
|
2587 // result is NULL, since that likely indicates that it printed something
|
danielebarchiesi@0
|
2588 // in which case, no further headers may be sent, and not if code running
|
danielebarchiesi@0
|
2589 // for this page request has already set the content type header.
|
danielebarchiesi@0
|
2590 if (isset($page_callback_result) && is_null(drupal_get_http_header('Content-Type'))) {
|
danielebarchiesi@0
|
2591 drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
|
danielebarchiesi@0
|
2592 }
|
danielebarchiesi@0
|
2593
|
danielebarchiesi@0
|
2594 // Send appropriate HTTP-Header for browsers and search engines.
|
danielebarchiesi@0
|
2595 global $language;
|
danielebarchiesi@0
|
2596 drupal_add_http_header('Content-Language', $language->language);
|
danielebarchiesi@0
|
2597
|
danielebarchiesi@0
|
2598 // Menu status constants are integers; page content is a string or array.
|
danielebarchiesi@0
|
2599 if (is_int($page_callback_result)) {
|
danielebarchiesi@0
|
2600 // @todo: Break these up into separate functions?
|
danielebarchiesi@0
|
2601 switch ($page_callback_result) {
|
danielebarchiesi@0
|
2602 case MENU_NOT_FOUND:
|
danielebarchiesi@0
|
2603 // Print a 404 page.
|
danielebarchiesi@0
|
2604 drupal_add_http_header('Status', '404 Not Found');
|
danielebarchiesi@0
|
2605
|
danielebarchiesi@0
|
2606 watchdog('page not found', check_plain($_GET['q']), NULL, WATCHDOG_WARNING);
|
danielebarchiesi@0
|
2607
|
danielebarchiesi@0
|
2608 // Check for and return a fast 404 page if configured.
|
danielebarchiesi@0
|
2609 drupal_fast_404();
|
danielebarchiesi@0
|
2610
|
danielebarchiesi@0
|
2611 // Keep old path for reference, and to allow forms to redirect to it.
|
danielebarchiesi@0
|
2612 if (!isset($_GET['destination'])) {
|
danielebarchiesi@0
|
2613 $_GET['destination'] = $_GET['q'];
|
danielebarchiesi@0
|
2614 }
|
danielebarchiesi@0
|
2615
|
danielebarchiesi@0
|
2616 $path = drupal_get_normal_path(variable_get('site_404', ''));
|
danielebarchiesi@0
|
2617 if ($path && $path != $_GET['q']) {
|
danielebarchiesi@0
|
2618 // Custom 404 handler. Set the active item in case there are tabs to
|
danielebarchiesi@0
|
2619 // display, or other dependencies on the path.
|
danielebarchiesi@0
|
2620 menu_set_active_item($path);
|
danielebarchiesi@0
|
2621 $return = menu_execute_active_handler($path, FALSE);
|
danielebarchiesi@0
|
2622 }
|
danielebarchiesi@0
|
2623
|
danielebarchiesi@0
|
2624 if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) {
|
danielebarchiesi@0
|
2625 // Standard 404 handler.
|
danielebarchiesi@0
|
2626 drupal_set_title(t('Page not found'));
|
danielebarchiesi@0
|
2627 $return = t('The requested page "@path" could not be found.', array('@path' => request_uri()));
|
danielebarchiesi@0
|
2628 }
|
danielebarchiesi@0
|
2629
|
danielebarchiesi@0
|
2630 drupal_set_page_content($return);
|
danielebarchiesi@0
|
2631 $page = element_info('page');
|
danielebarchiesi@0
|
2632 print drupal_render_page($page);
|
danielebarchiesi@0
|
2633 break;
|
danielebarchiesi@0
|
2634
|
danielebarchiesi@0
|
2635 case MENU_ACCESS_DENIED:
|
danielebarchiesi@0
|
2636 // Print a 403 page.
|
danielebarchiesi@0
|
2637 drupal_add_http_header('Status', '403 Forbidden');
|
danielebarchiesi@0
|
2638 watchdog('access denied', check_plain($_GET['q']), NULL, WATCHDOG_WARNING);
|
danielebarchiesi@0
|
2639
|
danielebarchiesi@0
|
2640 // Keep old path for reference, and to allow forms to redirect to it.
|
danielebarchiesi@0
|
2641 if (!isset($_GET['destination'])) {
|
danielebarchiesi@0
|
2642 $_GET['destination'] = $_GET['q'];
|
danielebarchiesi@0
|
2643 }
|
danielebarchiesi@0
|
2644
|
danielebarchiesi@0
|
2645 $path = drupal_get_normal_path(variable_get('site_403', ''));
|
danielebarchiesi@0
|
2646 if ($path && $path != $_GET['q']) {
|
danielebarchiesi@0
|
2647 // Custom 403 handler. Set the active item in case there are tabs to
|
danielebarchiesi@0
|
2648 // display or other dependencies on the path.
|
danielebarchiesi@0
|
2649 menu_set_active_item($path);
|
danielebarchiesi@0
|
2650 $return = menu_execute_active_handler($path, FALSE);
|
danielebarchiesi@0
|
2651 }
|
danielebarchiesi@0
|
2652
|
danielebarchiesi@0
|
2653 if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) {
|
danielebarchiesi@0
|
2654 // Standard 403 handler.
|
danielebarchiesi@0
|
2655 drupal_set_title(t('Access denied'));
|
danielebarchiesi@0
|
2656 $return = t('You are not authorized to access this page.');
|
danielebarchiesi@0
|
2657 }
|
danielebarchiesi@0
|
2658
|
danielebarchiesi@0
|
2659 print drupal_render_page($return);
|
danielebarchiesi@0
|
2660 break;
|
danielebarchiesi@0
|
2661
|
danielebarchiesi@0
|
2662 case MENU_SITE_OFFLINE:
|
danielebarchiesi@0
|
2663 // Print a 503 page.
|
danielebarchiesi@0
|
2664 drupal_maintenance_theme();
|
danielebarchiesi@0
|
2665 drupal_add_http_header('Status', '503 Service unavailable');
|
danielebarchiesi@0
|
2666 drupal_set_title(t('Site under maintenance'));
|
danielebarchiesi@0
|
2667 print theme('maintenance_page', array('content' => filter_xss_admin(variable_get('maintenance_mode_message',
|
danielebarchiesi@0
|
2668 t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal')))))));
|
danielebarchiesi@0
|
2669 break;
|
danielebarchiesi@0
|
2670 }
|
danielebarchiesi@0
|
2671 }
|
danielebarchiesi@0
|
2672 elseif (isset($page_callback_result)) {
|
danielebarchiesi@0
|
2673 // Print anything besides a menu constant, assuming it's not NULL or
|
danielebarchiesi@0
|
2674 // undefined.
|
danielebarchiesi@0
|
2675 print drupal_render_page($page_callback_result);
|
danielebarchiesi@0
|
2676 }
|
danielebarchiesi@0
|
2677
|
danielebarchiesi@0
|
2678 // Perform end-of-request tasks.
|
danielebarchiesi@0
|
2679 drupal_page_footer();
|
danielebarchiesi@0
|
2680 }
|
danielebarchiesi@0
|
2681
|
danielebarchiesi@0
|
2682 /**
|
danielebarchiesi@0
|
2683 * Performs end-of-request tasks.
|
danielebarchiesi@0
|
2684 *
|
danielebarchiesi@0
|
2685 * This function sets the page cache if appropriate, and allows modules to
|
danielebarchiesi@0
|
2686 * react to the closing of the page by calling hook_exit().
|
danielebarchiesi@0
|
2687 */
|
danielebarchiesi@0
|
2688 function drupal_page_footer() {
|
danielebarchiesi@0
|
2689 global $user;
|
danielebarchiesi@0
|
2690
|
danielebarchiesi@0
|
2691 module_invoke_all('exit');
|
danielebarchiesi@0
|
2692
|
danielebarchiesi@0
|
2693 // Commit the user session, if needed.
|
danielebarchiesi@0
|
2694 drupal_session_commit();
|
danielebarchiesi@0
|
2695
|
danielebarchiesi@0
|
2696 if (variable_get('cache', 0) && ($cache = drupal_page_set_cache())) {
|
danielebarchiesi@0
|
2697 drupal_serve_page_from_cache($cache);
|
danielebarchiesi@0
|
2698 }
|
danielebarchiesi@0
|
2699 else {
|
danielebarchiesi@0
|
2700 ob_flush();
|
danielebarchiesi@0
|
2701 }
|
danielebarchiesi@0
|
2702
|
danielebarchiesi@0
|
2703 _registry_check_code(REGISTRY_WRITE_LOOKUP_CACHE);
|
danielebarchiesi@0
|
2704 drupal_cache_system_paths();
|
danielebarchiesi@0
|
2705 module_implements_write_cache();
|
danielebarchiesi@0
|
2706 system_run_automated_cron();
|
danielebarchiesi@0
|
2707 }
|
danielebarchiesi@0
|
2708
|
danielebarchiesi@0
|
2709 /**
|
danielebarchiesi@0
|
2710 * Performs end-of-request tasks.
|
danielebarchiesi@0
|
2711 *
|
danielebarchiesi@0
|
2712 * In some cases page requests need to end without calling drupal_page_footer().
|
danielebarchiesi@0
|
2713 * In these cases, call drupal_exit() instead. There should rarely be a reason
|
danielebarchiesi@0
|
2714 * to call exit instead of drupal_exit();
|
danielebarchiesi@0
|
2715 *
|
danielebarchiesi@0
|
2716 * @param $destination
|
danielebarchiesi@0
|
2717 * If this function is called from drupal_goto(), then this argument
|
danielebarchiesi@0
|
2718 * will be a fully-qualified URL that is the destination of the redirect.
|
danielebarchiesi@0
|
2719 * This should be passed along to hook_exit() implementations.
|
danielebarchiesi@0
|
2720 */
|
danielebarchiesi@0
|
2721 function drupal_exit($destination = NULL) {
|
danielebarchiesi@0
|
2722 if (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL) {
|
danielebarchiesi@0
|
2723 if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {
|
danielebarchiesi@0
|
2724 module_invoke_all('exit', $destination);
|
danielebarchiesi@0
|
2725 }
|
danielebarchiesi@0
|
2726 drupal_session_commit();
|
danielebarchiesi@0
|
2727 }
|
danielebarchiesi@0
|
2728 exit;
|
danielebarchiesi@0
|
2729 }
|
danielebarchiesi@0
|
2730
|
danielebarchiesi@0
|
2731 /**
|
danielebarchiesi@0
|
2732 * Forms an associative array from a linear array.
|
danielebarchiesi@0
|
2733 *
|
danielebarchiesi@0
|
2734 * This function walks through the provided array and constructs an associative
|
danielebarchiesi@0
|
2735 * array out of it. The keys of the resulting array will be the values of the
|
danielebarchiesi@0
|
2736 * input array. The values will be the same as the keys unless a function is
|
danielebarchiesi@0
|
2737 * specified, in which case the output of the function is used for the values
|
danielebarchiesi@0
|
2738 * instead.
|
danielebarchiesi@0
|
2739 *
|
danielebarchiesi@0
|
2740 * @param $array
|
danielebarchiesi@0
|
2741 * A linear array.
|
danielebarchiesi@0
|
2742 * @param $function
|
danielebarchiesi@0
|
2743 * A name of a function to apply to all values before output.
|
danielebarchiesi@0
|
2744 *
|
danielebarchiesi@0
|
2745 * @return
|
danielebarchiesi@0
|
2746 * An associative array.
|
danielebarchiesi@0
|
2747 */
|
danielebarchiesi@0
|
2748 function drupal_map_assoc($array, $function = NULL) {
|
danielebarchiesi@0
|
2749 // array_combine() fails with empty arrays:
|
danielebarchiesi@0
|
2750 // http://bugs.php.net/bug.php?id=34857.
|
danielebarchiesi@0
|
2751 $array = !empty($array) ? array_combine($array, $array) : array();
|
danielebarchiesi@0
|
2752 if (is_callable($function)) {
|
danielebarchiesi@0
|
2753 $array = array_map($function, $array);
|
danielebarchiesi@0
|
2754 }
|
danielebarchiesi@0
|
2755 return $array;
|
danielebarchiesi@0
|
2756 }
|
danielebarchiesi@0
|
2757
|
danielebarchiesi@0
|
2758 /**
|
danielebarchiesi@0
|
2759 * Attempts to set the PHP maximum execution time.
|
danielebarchiesi@0
|
2760 *
|
danielebarchiesi@0
|
2761 * This function is a wrapper around the PHP function set_time_limit().
|
danielebarchiesi@0
|
2762 * When called, set_time_limit() restarts the timeout counter from zero.
|
danielebarchiesi@0
|
2763 * In other words, if the timeout is the default 30 seconds, and 25 seconds
|
danielebarchiesi@0
|
2764 * into script execution a call such as set_time_limit(20) is made, the
|
danielebarchiesi@0
|
2765 * script will run for a total of 45 seconds before timing out.
|
danielebarchiesi@0
|
2766 *
|
danielebarchiesi@0
|
2767 * It also means that it is possible to decrease the total time limit if
|
danielebarchiesi@0
|
2768 * the sum of the new time limit and the current time spent running the
|
danielebarchiesi@0
|
2769 * script is inferior to the original time limit. It is inherent to the way
|
danielebarchiesi@0
|
2770 * set_time_limit() works, it should rather be called with an appropriate
|
danielebarchiesi@0
|
2771 * value every time you need to allocate a certain amount of time
|
danielebarchiesi@0
|
2772 * to execute a task than only once at the beginning of the script.
|
danielebarchiesi@0
|
2773 *
|
danielebarchiesi@0
|
2774 * Before calling set_time_limit(), we check if this function is available
|
danielebarchiesi@0
|
2775 * because it could be disabled by the server administrator. We also hide all
|
danielebarchiesi@0
|
2776 * the errors that could occur when calling set_time_limit(), because it is
|
danielebarchiesi@0
|
2777 * not possible to reliably ensure that PHP or a security extension will
|
danielebarchiesi@0
|
2778 * not issue a warning/error if they prevent the use of this function.
|
danielebarchiesi@0
|
2779 *
|
danielebarchiesi@0
|
2780 * @param $time_limit
|
danielebarchiesi@0
|
2781 * An integer specifying the new time limit, in seconds. A value of 0
|
danielebarchiesi@0
|
2782 * indicates unlimited execution time.
|
danielebarchiesi@0
|
2783 *
|
danielebarchiesi@0
|
2784 * @ingroup php_wrappers
|
danielebarchiesi@0
|
2785 */
|
danielebarchiesi@0
|
2786 function drupal_set_time_limit($time_limit) {
|
danielebarchiesi@0
|
2787 if (function_exists('set_time_limit')) {
|
danielebarchiesi@0
|
2788 @set_time_limit($time_limit);
|
danielebarchiesi@0
|
2789 }
|
danielebarchiesi@0
|
2790 }
|
danielebarchiesi@0
|
2791
|
danielebarchiesi@0
|
2792 /**
|
danielebarchiesi@0
|
2793 * Returns the path to a system item (module, theme, etc.).
|
danielebarchiesi@0
|
2794 *
|
danielebarchiesi@0
|
2795 * @param $type
|
danielebarchiesi@0
|
2796 * The type of the item (i.e. theme, theme_engine, module, profile).
|
danielebarchiesi@0
|
2797 * @param $name
|
danielebarchiesi@0
|
2798 * The name of the item for which the path is requested.
|
danielebarchiesi@0
|
2799 *
|
danielebarchiesi@0
|
2800 * @return
|
danielebarchiesi@0
|
2801 * The path to the requested item or an empty string if the item is not found.
|
danielebarchiesi@0
|
2802 */
|
danielebarchiesi@0
|
2803 function drupal_get_path($type, $name) {
|
danielebarchiesi@0
|
2804 return dirname(drupal_get_filename($type, $name));
|
danielebarchiesi@0
|
2805 }
|
danielebarchiesi@0
|
2806
|
danielebarchiesi@0
|
2807 /**
|
danielebarchiesi@0
|
2808 * Returns the base URL path (i.e., directory) of the Drupal installation.
|
danielebarchiesi@0
|
2809 *
|
danielebarchiesi@0
|
2810 * base_path() adds a "/" to the beginning and end of the returned path if the
|
danielebarchiesi@0
|
2811 * path is not empty. At the very least, this will return "/".
|
danielebarchiesi@0
|
2812 *
|
danielebarchiesi@0
|
2813 * Examples:
|
danielebarchiesi@0
|
2814 * - http://example.com returns "/" because the path is empty.
|
danielebarchiesi@0
|
2815 * - http://example.com/drupal/folder returns "/drupal/folder/".
|
danielebarchiesi@0
|
2816 */
|
danielebarchiesi@0
|
2817 function base_path() {
|
danielebarchiesi@0
|
2818 return $GLOBALS['base_path'];
|
danielebarchiesi@0
|
2819 }
|
danielebarchiesi@0
|
2820
|
danielebarchiesi@0
|
2821 /**
|
danielebarchiesi@0
|
2822 * Adds a LINK tag with a distinct 'rel' attribute to the page's HEAD.
|
danielebarchiesi@0
|
2823 *
|
danielebarchiesi@0
|
2824 * This function can be called as long the HTML header hasn't been sent, which
|
danielebarchiesi@0
|
2825 * on normal pages is up through the preprocess step of theme('html'). Adding
|
danielebarchiesi@0
|
2826 * a link will overwrite a prior link with the exact same 'rel' and 'href'
|
danielebarchiesi@0
|
2827 * attributes.
|
danielebarchiesi@0
|
2828 *
|
danielebarchiesi@0
|
2829 * @param $attributes
|
danielebarchiesi@0
|
2830 * Associative array of element attributes including 'href' and 'rel'.
|
danielebarchiesi@0
|
2831 * @param $header
|
danielebarchiesi@0
|
2832 * Optional flag to determine if a HTTP 'Link:' header should be sent.
|
danielebarchiesi@0
|
2833 */
|
danielebarchiesi@0
|
2834 function drupal_add_html_head_link($attributes, $header = FALSE) {
|
danielebarchiesi@0
|
2835 $element = array(
|
danielebarchiesi@0
|
2836 '#tag' => 'link',
|
danielebarchiesi@0
|
2837 '#attributes' => $attributes,
|
danielebarchiesi@0
|
2838 );
|
danielebarchiesi@0
|
2839 $href = $attributes['href'];
|
danielebarchiesi@0
|
2840
|
danielebarchiesi@0
|
2841 if ($header) {
|
danielebarchiesi@0
|
2842 // Also add a HTTP header "Link:".
|
danielebarchiesi@0
|
2843 $href = '<' . check_plain($attributes['href']) . '>;';
|
danielebarchiesi@0
|
2844 unset($attributes['href']);
|
danielebarchiesi@0
|
2845 $element['#attached']['drupal_add_http_header'][] = array('Link', $href . drupal_http_header_attributes($attributes), TRUE);
|
danielebarchiesi@0
|
2846 }
|
danielebarchiesi@0
|
2847
|
danielebarchiesi@0
|
2848 drupal_add_html_head($element, 'drupal_add_html_head_link:' . $attributes['rel'] . ':' . $href);
|
danielebarchiesi@0
|
2849 }
|
danielebarchiesi@0
|
2850
|
danielebarchiesi@0
|
2851 /**
|
danielebarchiesi@0
|
2852 * Adds a cascading stylesheet to the stylesheet queue.
|
danielebarchiesi@0
|
2853 *
|
danielebarchiesi@0
|
2854 * Calling drupal_static_reset('drupal_add_css') will clear all cascading
|
danielebarchiesi@0
|
2855 * stylesheets added so far.
|
danielebarchiesi@0
|
2856 *
|
danielebarchiesi@0
|
2857 * If CSS aggregation/compression is enabled, all cascading style sheets added
|
danielebarchiesi@0
|
2858 * with $options['preprocess'] set to TRUE will be merged into one aggregate
|
danielebarchiesi@0
|
2859 * file and compressed by removing all extraneous white space.
|
danielebarchiesi@0
|
2860 * Preprocessed inline stylesheets will not be aggregated into this single file;
|
danielebarchiesi@0
|
2861 * instead, they are just compressed upon output on the page. Externally hosted
|
danielebarchiesi@0
|
2862 * stylesheets are never aggregated or compressed.
|
danielebarchiesi@0
|
2863 *
|
danielebarchiesi@0
|
2864 * The reason for aggregating the files is outlined quite thoroughly here:
|
danielebarchiesi@0
|
2865 * http://www.die.net/musings/page_load_time/ "Load fewer external objects. Due
|
danielebarchiesi@0
|
2866 * to request overhead, one bigger file just loads faster than two smaller ones
|
danielebarchiesi@0
|
2867 * half its size."
|
danielebarchiesi@0
|
2868 *
|
danielebarchiesi@0
|
2869 * $options['preprocess'] should be only set to TRUE when a file is required for
|
danielebarchiesi@0
|
2870 * all typical visitors and most pages of a site. It is critical that all
|
danielebarchiesi@0
|
2871 * preprocessed files are added unconditionally on every page, even if the
|
danielebarchiesi@0
|
2872 * files do not happen to be needed on a page. This is normally done by calling
|
danielebarchiesi@0
|
2873 * drupal_add_css() in a hook_init() implementation.
|
danielebarchiesi@0
|
2874 *
|
danielebarchiesi@0
|
2875 * Non-preprocessed files should only be added to the page when they are
|
danielebarchiesi@0
|
2876 * actually needed.
|
danielebarchiesi@0
|
2877 *
|
danielebarchiesi@0
|
2878 * @param $data
|
danielebarchiesi@0
|
2879 * (optional) The stylesheet data to be added, depending on what is passed
|
danielebarchiesi@0
|
2880 * through to the $options['type'] parameter:
|
danielebarchiesi@0
|
2881 * - 'file': The path to the CSS file relative to the base_path(), or a
|
danielebarchiesi@0
|
2882 * stream wrapper URI. For example: "modules/devel/devel.css" or
|
danielebarchiesi@0
|
2883 * "public://generated_css/stylesheet_1.css". Note that Modules should
|
danielebarchiesi@0
|
2884 * always prefix the names of their CSS files with the module name; for
|
danielebarchiesi@0
|
2885 * example, system-menus.css rather than simply menus.css. Themes can
|
danielebarchiesi@0
|
2886 * override module-supplied CSS files based on their filenames, and this
|
danielebarchiesi@0
|
2887 * prefixing helps prevent confusing name collisions for theme developers.
|
danielebarchiesi@0
|
2888 * See drupal_get_css() where the overrides are performed. Also, if the
|
danielebarchiesi@0
|
2889 * direction of the current language is right-to-left (Hebrew, Arabic,
|
danielebarchiesi@0
|
2890 * etc.), the function will also look for an RTL CSS file and append it to
|
danielebarchiesi@0
|
2891 * the list. The name of this file should have an '-rtl.css' suffix. For
|
danielebarchiesi@0
|
2892 * example, a CSS file called 'mymodule-name.css' will have a
|
danielebarchiesi@0
|
2893 * 'mymodule-name-rtl.css' file added to the list, if exists in the same
|
danielebarchiesi@0
|
2894 * directory. This CSS file should contain overrides for properties which
|
danielebarchiesi@0
|
2895 * should be reversed or otherwise different in a right-to-left display.
|
danielebarchiesi@0
|
2896 * - 'inline': A string of CSS that should be placed in the given scope. Note
|
danielebarchiesi@0
|
2897 * that it is better practice to use 'file' stylesheets, rather than
|
danielebarchiesi@0
|
2898 * 'inline', as the CSS would then be aggregated and cached.
|
danielebarchiesi@0
|
2899 * - 'external': The absolute path to an external CSS file that is not hosted
|
danielebarchiesi@0
|
2900 * on the local server. These files will not be aggregated if CSS
|
danielebarchiesi@0
|
2901 * aggregation is enabled.
|
danielebarchiesi@0
|
2902 * @param $options
|
danielebarchiesi@0
|
2903 * (optional) A string defining the 'type' of CSS that is being added in the
|
danielebarchiesi@0
|
2904 * $data parameter ('file', 'inline', or 'external'), or an array which can
|
danielebarchiesi@0
|
2905 * have any or all of the following keys:
|
danielebarchiesi@0
|
2906 * - 'type': The type of stylesheet being added. Available options are 'file',
|
danielebarchiesi@0
|
2907 * 'inline' or 'external'. Defaults to 'file'.
|
danielebarchiesi@0
|
2908 * - 'basename': Force a basename for the file being added. Modules are
|
danielebarchiesi@0
|
2909 * expected to use stylesheets with unique filenames, but integration of
|
danielebarchiesi@0
|
2910 * external libraries may make this impossible. The basename of
|
danielebarchiesi@0
|
2911 * 'modules/node/node.css' is 'node.css'. If the external library "node.js"
|
danielebarchiesi@0
|
2912 * ships with a 'node.css', then a different, unique basename would be
|
danielebarchiesi@0
|
2913 * 'node.js.css'.
|
danielebarchiesi@0
|
2914 * - 'group': A number identifying the group in which to add the stylesheet.
|
danielebarchiesi@0
|
2915 * Available constants are:
|
danielebarchiesi@0
|
2916 * - CSS_SYSTEM: Any system-layer CSS.
|
danielebarchiesi@0
|
2917 * - CSS_DEFAULT: (default) Any module-layer CSS.
|
danielebarchiesi@0
|
2918 * - CSS_THEME: Any theme-layer CSS.
|
danielebarchiesi@0
|
2919 * The group number serves as a weight: the markup for loading a stylesheet
|
danielebarchiesi@0
|
2920 * within a lower weight group is output to the page before the markup for
|
danielebarchiesi@0
|
2921 * loading a stylesheet within a higher weight group, so CSS within higher
|
danielebarchiesi@0
|
2922 * weight groups take precendence over CSS within lower weight groups.
|
danielebarchiesi@0
|
2923 * - 'every_page': For optimal front-end performance when aggregation is
|
danielebarchiesi@0
|
2924 * enabled, this should be set to TRUE if the stylesheet is present on every
|
danielebarchiesi@0
|
2925 * page of the website for users for whom it is present at all. This
|
danielebarchiesi@0
|
2926 * defaults to FALSE. It is set to TRUE for stylesheets added via module and
|
danielebarchiesi@0
|
2927 * theme .info files. Modules that add stylesheets within hook_init()
|
danielebarchiesi@0
|
2928 * implementations, or from other code that ensures that the stylesheet is
|
danielebarchiesi@0
|
2929 * added to all website pages, should also set this flag to TRUE. All
|
danielebarchiesi@0
|
2930 * stylesheets within the same group that have the 'every_page' flag set to
|
danielebarchiesi@0
|
2931 * TRUE and do not have 'preprocess' set to FALSE are aggregated together
|
danielebarchiesi@0
|
2932 * into a single aggregate file, and that aggregate file can be reused
|
danielebarchiesi@0
|
2933 * across a user's entire site visit, leading to faster navigation between
|
danielebarchiesi@0
|
2934 * pages. However, stylesheets that are only needed on pages less frequently
|
danielebarchiesi@0
|
2935 * visited, can be added by code that only runs for those particular pages,
|
danielebarchiesi@0
|
2936 * and that code should not set the 'every_page' flag. This minimizes the
|
danielebarchiesi@0
|
2937 * size of the aggregate file that the user needs to download when first
|
danielebarchiesi@0
|
2938 * visiting the website. Stylesheets without the 'every_page' flag are
|
danielebarchiesi@0
|
2939 * aggregated into a separate aggregate file. This other aggregate file is
|
danielebarchiesi@0
|
2940 * likely to change from page to page, and each new aggregate file needs to
|
danielebarchiesi@0
|
2941 * be downloaded when first encountered, so it should be kept relatively
|
danielebarchiesi@0
|
2942 * small by ensuring that most commonly needed stylesheets are added to
|
danielebarchiesi@0
|
2943 * every page.
|
danielebarchiesi@0
|
2944 * - 'weight': The weight of the stylesheet specifies the order in which the
|
danielebarchiesi@0
|
2945 * CSS will appear relative to other stylesheets with the same group and
|
danielebarchiesi@0
|
2946 * 'every_page' flag. The exact ordering of stylesheets is as follows:
|
danielebarchiesi@0
|
2947 * - First by group.
|
danielebarchiesi@0
|
2948 * - Then by the 'every_page' flag, with TRUE coming before FALSE.
|
danielebarchiesi@0
|
2949 * - Then by weight.
|
danielebarchiesi@0
|
2950 * - Then by the order in which the CSS was added. For example, all else
|
danielebarchiesi@0
|
2951 * being the same, a stylesheet added by a call to drupal_add_css() that
|
danielebarchiesi@0
|
2952 * happened later in the page request gets added to the page after one for
|
danielebarchiesi@0
|
2953 * which drupal_add_css() happened earlier in the page request.
|
danielebarchiesi@0
|
2954 * - 'media': The media type for the stylesheet, e.g., all, print, screen.
|
danielebarchiesi@0
|
2955 * Defaults to 'all'.
|
danielebarchiesi@0
|
2956 * - 'preprocess': If TRUE and CSS aggregation/compression is enabled, the
|
danielebarchiesi@0
|
2957 * styles will be aggregated and compressed. Defaults to TRUE.
|
danielebarchiesi@0
|
2958 * - 'browsers': An array containing information specifying which browsers
|
danielebarchiesi@0
|
2959 * should load the CSS item. See drupal_pre_render_conditional_comments()
|
danielebarchiesi@0
|
2960 * for details.
|
danielebarchiesi@0
|
2961 *
|
danielebarchiesi@0
|
2962 * @return
|
danielebarchiesi@0
|
2963 * An array of queued cascading stylesheets.
|
danielebarchiesi@0
|
2964 *
|
danielebarchiesi@0
|
2965 * @see drupal_get_css()
|
danielebarchiesi@0
|
2966 */
|
danielebarchiesi@0
|
2967 function drupal_add_css($data = NULL, $options = NULL) {
|
danielebarchiesi@0
|
2968 $css = &drupal_static(__FUNCTION__, array());
|
danielebarchiesi@0
|
2969
|
danielebarchiesi@0
|
2970 // Construct the options, taking the defaults into consideration.
|
danielebarchiesi@0
|
2971 if (isset($options)) {
|
danielebarchiesi@0
|
2972 if (!is_array($options)) {
|
danielebarchiesi@0
|
2973 $options = array('type' => $options);
|
danielebarchiesi@0
|
2974 }
|
danielebarchiesi@0
|
2975 }
|
danielebarchiesi@0
|
2976 else {
|
danielebarchiesi@0
|
2977 $options = array();
|
danielebarchiesi@0
|
2978 }
|
danielebarchiesi@0
|
2979
|
danielebarchiesi@0
|
2980 // Create an array of CSS files for each media type first, since each type needs to be served
|
danielebarchiesi@0
|
2981 // to the browser differently.
|
danielebarchiesi@0
|
2982 if (isset($data)) {
|
danielebarchiesi@0
|
2983 $options += array(
|
danielebarchiesi@0
|
2984 'type' => 'file',
|
danielebarchiesi@0
|
2985 'group' => CSS_DEFAULT,
|
danielebarchiesi@0
|
2986 'weight' => 0,
|
danielebarchiesi@0
|
2987 'every_page' => FALSE,
|
danielebarchiesi@0
|
2988 'media' => 'all',
|
danielebarchiesi@0
|
2989 'preprocess' => TRUE,
|
danielebarchiesi@0
|
2990 'data' => $data,
|
danielebarchiesi@0
|
2991 'browsers' => array(),
|
danielebarchiesi@0
|
2992 );
|
danielebarchiesi@0
|
2993 $options['browsers'] += array(
|
danielebarchiesi@0
|
2994 'IE' => TRUE,
|
danielebarchiesi@0
|
2995 '!IE' => TRUE,
|
danielebarchiesi@0
|
2996 );
|
danielebarchiesi@0
|
2997
|
danielebarchiesi@0
|
2998 // Files with a query string cannot be preprocessed.
|
danielebarchiesi@0
|
2999 if ($options['type'] === 'file' && $options['preprocess'] && strpos($options['data'], '?') !== FALSE) {
|
danielebarchiesi@0
|
3000 $options['preprocess'] = FALSE;
|
danielebarchiesi@0
|
3001 }
|
danielebarchiesi@0
|
3002
|
danielebarchiesi@0
|
3003 // Always add a tiny value to the weight, to conserve the insertion order.
|
danielebarchiesi@0
|
3004 $options['weight'] += count($css) / 1000;
|
danielebarchiesi@0
|
3005
|
danielebarchiesi@0
|
3006 // Add the data to the CSS array depending on the type.
|
danielebarchiesi@0
|
3007 switch ($options['type']) {
|
danielebarchiesi@0
|
3008 case 'inline':
|
danielebarchiesi@0
|
3009 // For inline stylesheets, we don't want to use the $data as the array
|
danielebarchiesi@0
|
3010 // key as $data could be a very long string of CSS.
|
danielebarchiesi@0
|
3011 $css[] = $options;
|
danielebarchiesi@0
|
3012 break;
|
danielebarchiesi@0
|
3013 default:
|
danielebarchiesi@0
|
3014 // Local and external files must keep their name as the associative key
|
danielebarchiesi@0
|
3015 // so the same CSS file is not be added twice.
|
danielebarchiesi@0
|
3016 $css[$data] = $options;
|
danielebarchiesi@0
|
3017 }
|
danielebarchiesi@0
|
3018 }
|
danielebarchiesi@0
|
3019
|
danielebarchiesi@0
|
3020 return $css;
|
danielebarchiesi@0
|
3021 }
|
danielebarchiesi@0
|
3022
|
danielebarchiesi@0
|
3023 /**
|
danielebarchiesi@0
|
3024 * Returns a themed representation of all stylesheets to attach to the page.
|
danielebarchiesi@0
|
3025 *
|
danielebarchiesi@0
|
3026 * It loads the CSS in order, with 'module' first, then 'theme' afterwards.
|
danielebarchiesi@0
|
3027 * This ensures proper cascading of styles so themes can easily override
|
danielebarchiesi@0
|
3028 * module styles through CSS selectors.
|
danielebarchiesi@0
|
3029 *
|
danielebarchiesi@0
|
3030 * Themes may replace module-defined CSS files by adding a stylesheet with the
|
danielebarchiesi@0
|
3031 * same filename. For example, themes/bartik/system-menus.css would replace
|
danielebarchiesi@0
|
3032 * modules/system/system-menus.css. This allows themes to override complete
|
danielebarchiesi@0
|
3033 * CSS files, rather than specific selectors, when necessary.
|
danielebarchiesi@0
|
3034 *
|
danielebarchiesi@0
|
3035 * If the original CSS file is being overridden by a theme, the theme is
|
danielebarchiesi@0
|
3036 * responsible for supplying an accompanying RTL CSS file to replace the
|
danielebarchiesi@0
|
3037 * module's.
|
danielebarchiesi@0
|
3038 *
|
danielebarchiesi@0
|
3039 * @param $css
|
danielebarchiesi@0
|
3040 * (optional) An array of CSS files. If no array is provided, the default
|
danielebarchiesi@0
|
3041 * stylesheets array is used instead.
|
danielebarchiesi@0
|
3042 * @param $skip_alter
|
danielebarchiesi@0
|
3043 * (optional) If set to TRUE, this function skips calling drupal_alter() on
|
danielebarchiesi@0
|
3044 * $css, useful when the calling function passes a $css array that has already
|
danielebarchiesi@0
|
3045 * been altered.
|
danielebarchiesi@0
|
3046 *
|
danielebarchiesi@0
|
3047 * @return
|
danielebarchiesi@0
|
3048 * A string of XHTML CSS tags.
|
danielebarchiesi@0
|
3049 *
|
danielebarchiesi@0
|
3050 * @see drupal_add_css()
|
danielebarchiesi@0
|
3051 */
|
danielebarchiesi@0
|
3052 function drupal_get_css($css = NULL, $skip_alter = FALSE) {
|
danielebarchiesi@0
|
3053 if (!isset($css)) {
|
danielebarchiesi@0
|
3054 $css = drupal_add_css();
|
danielebarchiesi@0
|
3055 }
|
danielebarchiesi@0
|
3056
|
danielebarchiesi@0
|
3057 // Allow modules and themes to alter the CSS items.
|
danielebarchiesi@0
|
3058 if (!$skip_alter) {
|
danielebarchiesi@0
|
3059 drupal_alter('css', $css);
|
danielebarchiesi@0
|
3060 }
|
danielebarchiesi@0
|
3061
|
danielebarchiesi@0
|
3062 // Sort CSS items, so that they appear in the correct order.
|
danielebarchiesi@0
|
3063 uasort($css, 'drupal_sort_css_js');
|
danielebarchiesi@0
|
3064
|
danielebarchiesi@0
|
3065 // Provide the page with information about the individual CSS files used,
|
danielebarchiesi@0
|
3066 // information not otherwise available when CSS aggregation is enabled. The
|
danielebarchiesi@0
|
3067 // setting is attached later in this function, but is set here, so that CSS
|
danielebarchiesi@0
|
3068 // files removed below are still considered "used" and prevented from being
|
danielebarchiesi@0
|
3069 // added in a later AJAX request.
|
danielebarchiesi@0
|
3070 // Skip if no files were added to the page or jQuery.extend() will overwrite
|
danielebarchiesi@0
|
3071 // the Drupal.settings.ajaxPageState.css object with an empty array.
|
danielebarchiesi@0
|
3072 if (!empty($css)) {
|
danielebarchiesi@0
|
3073 // Cast the array to an object to be on the safe side even if not empty.
|
danielebarchiesi@0
|
3074 $setting['ajaxPageState']['css'] = (object) array_fill_keys(array_keys($css), 1);
|
danielebarchiesi@0
|
3075 }
|
danielebarchiesi@0
|
3076
|
danielebarchiesi@0
|
3077 // Remove the overridden CSS files. Later CSS files override former ones.
|
danielebarchiesi@0
|
3078 $previous_item = array();
|
danielebarchiesi@0
|
3079 foreach ($css as $key => $item) {
|
danielebarchiesi@0
|
3080 if ($item['type'] == 'file') {
|
danielebarchiesi@0
|
3081 // If defined, force a unique basename for this file.
|
danielebarchiesi@0
|
3082 $basename = isset($item['basename']) ? $item['basename'] : drupal_basename($item['data']);
|
danielebarchiesi@0
|
3083 if (isset($previous_item[$basename])) {
|
danielebarchiesi@0
|
3084 // Remove the previous item that shared the same base name.
|
danielebarchiesi@0
|
3085 unset($css[$previous_item[$basename]]);
|
danielebarchiesi@0
|
3086 }
|
danielebarchiesi@0
|
3087 $previous_item[$basename] = $key;
|
danielebarchiesi@0
|
3088 }
|
danielebarchiesi@0
|
3089 }
|
danielebarchiesi@0
|
3090
|
danielebarchiesi@0
|
3091 // Render the HTML needed to load the CSS.
|
danielebarchiesi@0
|
3092 $styles = array(
|
danielebarchiesi@0
|
3093 '#type' => 'styles',
|
danielebarchiesi@0
|
3094 '#items' => $css,
|
danielebarchiesi@0
|
3095 );
|
danielebarchiesi@0
|
3096
|
danielebarchiesi@0
|
3097 if (!empty($setting)) {
|
danielebarchiesi@0
|
3098 $styles['#attached']['js'][] = array('type' => 'setting', 'data' => $setting);
|
danielebarchiesi@0
|
3099 }
|
danielebarchiesi@0
|
3100
|
danielebarchiesi@0
|
3101 return drupal_render($styles);
|
danielebarchiesi@0
|
3102 }
|
danielebarchiesi@0
|
3103
|
danielebarchiesi@0
|
3104 /**
|
danielebarchiesi@0
|
3105 * Sorts CSS and JavaScript resources.
|
danielebarchiesi@0
|
3106 *
|
danielebarchiesi@0
|
3107 * Callback for uasort() within:
|
danielebarchiesi@0
|
3108 * - drupal_get_css()
|
danielebarchiesi@0
|
3109 * - drupal_get_js()
|
danielebarchiesi@0
|
3110 *
|
danielebarchiesi@0
|
3111 * This sort order helps optimize front-end performance while providing modules
|
danielebarchiesi@0
|
3112 * and themes with the necessary control for ordering the CSS and JavaScript
|
danielebarchiesi@0
|
3113 * appearing on a page.
|
danielebarchiesi@0
|
3114 *
|
danielebarchiesi@0
|
3115 * @param $a
|
danielebarchiesi@0
|
3116 * First item for comparison. The compared items should be associative arrays
|
danielebarchiesi@0
|
3117 * of member items from drupal_add_css() or drupal_add_js().
|
danielebarchiesi@0
|
3118 * @param $b
|
danielebarchiesi@0
|
3119 * Second item for comparison.
|
danielebarchiesi@0
|
3120 *
|
danielebarchiesi@0
|
3121 * @see drupal_add_css()
|
danielebarchiesi@0
|
3122 * @see drupal_add_js()
|
danielebarchiesi@0
|
3123 */
|
danielebarchiesi@0
|
3124 function drupal_sort_css_js($a, $b) {
|
danielebarchiesi@0
|
3125 // First order by group, so that, for example, all items in the CSS_SYSTEM
|
danielebarchiesi@0
|
3126 // group appear before items in the CSS_DEFAULT group, which appear before
|
danielebarchiesi@0
|
3127 // all items in the CSS_THEME group. Modules may create additional groups by
|
danielebarchiesi@0
|
3128 // defining their own constants.
|
danielebarchiesi@0
|
3129 if ($a['group'] < $b['group']) {
|
danielebarchiesi@0
|
3130 return -1;
|
danielebarchiesi@0
|
3131 }
|
danielebarchiesi@0
|
3132 elseif ($a['group'] > $b['group']) {
|
danielebarchiesi@0
|
3133 return 1;
|
danielebarchiesi@0
|
3134 }
|
danielebarchiesi@0
|
3135 // Within a group, order all infrequently needed, page-specific files after
|
danielebarchiesi@0
|
3136 // common files needed throughout the website. Separating this way allows for
|
danielebarchiesi@0
|
3137 // the aggregate file generated for all of the common files to be reused
|
danielebarchiesi@0
|
3138 // across a site visit without being cut by a page using a less common file.
|
danielebarchiesi@0
|
3139 elseif ($a['every_page'] && !$b['every_page']) {
|
danielebarchiesi@0
|
3140 return -1;
|
danielebarchiesi@0
|
3141 }
|
danielebarchiesi@0
|
3142 elseif (!$a['every_page'] && $b['every_page']) {
|
danielebarchiesi@0
|
3143 return 1;
|
danielebarchiesi@0
|
3144 }
|
danielebarchiesi@0
|
3145 // Finally, order by weight.
|
danielebarchiesi@0
|
3146 elseif ($a['weight'] < $b['weight']) {
|
danielebarchiesi@0
|
3147 return -1;
|
danielebarchiesi@0
|
3148 }
|
danielebarchiesi@0
|
3149 elseif ($a['weight'] > $b['weight']) {
|
danielebarchiesi@0
|
3150 return 1;
|
danielebarchiesi@0
|
3151 }
|
danielebarchiesi@0
|
3152 else {
|
danielebarchiesi@0
|
3153 return 0;
|
danielebarchiesi@0
|
3154 }
|
danielebarchiesi@0
|
3155 }
|
danielebarchiesi@0
|
3156
|
danielebarchiesi@0
|
3157 /**
|
danielebarchiesi@0
|
3158 * Default callback to group CSS items.
|
danielebarchiesi@0
|
3159 *
|
danielebarchiesi@0
|
3160 * This function arranges the CSS items that are in the #items property of the
|
danielebarchiesi@0
|
3161 * styles element into groups. Arranging the CSS items into groups serves two
|
danielebarchiesi@0
|
3162 * purposes. When aggregation is enabled, files within a group are aggregated
|
danielebarchiesi@0
|
3163 * into a single file, significantly improving page loading performance by
|
danielebarchiesi@0
|
3164 * minimizing network traffic overhead. When aggregation is disabled, grouping
|
danielebarchiesi@0
|
3165 * allows multiple files to be loaded from a single STYLE tag, enabling sites
|
danielebarchiesi@0
|
3166 * with many modules enabled or a complex theme being used to stay within IE's
|
danielebarchiesi@0
|
3167 * 31 CSS inclusion tag limit: http://drupal.org/node/228818.
|
danielebarchiesi@0
|
3168 *
|
danielebarchiesi@0
|
3169 * This function puts multiple items into the same group if they are groupable
|
danielebarchiesi@0
|
3170 * and if they are for the same 'media' and 'browsers'. Items of the 'file' type
|
danielebarchiesi@0
|
3171 * are groupable if their 'preprocess' flag is TRUE, items of the 'inline' type
|
danielebarchiesi@0
|
3172 * are always groupable, and items of the 'external' type are never groupable.
|
danielebarchiesi@0
|
3173 * This function also ensures that the process of grouping items does not change
|
danielebarchiesi@0
|
3174 * their relative order. This requirement may result in multiple groups for the
|
danielebarchiesi@0
|
3175 * same type, media, and browsers, if needed to accommodate other items in
|
danielebarchiesi@0
|
3176 * between.
|
danielebarchiesi@0
|
3177 *
|
danielebarchiesi@0
|
3178 * @param $css
|
danielebarchiesi@0
|
3179 * An array of CSS items, as returned by drupal_add_css(), but after
|
danielebarchiesi@0
|
3180 * alteration performed by drupal_get_css().
|
danielebarchiesi@0
|
3181 *
|
danielebarchiesi@0
|
3182 * @return
|
danielebarchiesi@0
|
3183 * An array of CSS groups. Each group contains the same keys (e.g., 'media',
|
danielebarchiesi@0
|
3184 * 'data', etc.) as a CSS item from the $css parameter, with the value of
|
danielebarchiesi@0
|
3185 * each key applying to the group as a whole. Each group also contains an
|
danielebarchiesi@0
|
3186 * 'items' key, which is the subset of items from $css that are in the group.
|
danielebarchiesi@0
|
3187 *
|
danielebarchiesi@0
|
3188 * @see drupal_pre_render_styles()
|
danielebarchiesi@0
|
3189 * @see system_element_info()
|
danielebarchiesi@0
|
3190 */
|
danielebarchiesi@0
|
3191 function drupal_group_css($css) {
|
danielebarchiesi@0
|
3192 $groups = array();
|
danielebarchiesi@0
|
3193 // If a group can contain multiple items, we track the information that must
|
danielebarchiesi@0
|
3194 // be the same for each item in the group, so that when we iterate the next
|
danielebarchiesi@0
|
3195 // item, we can determine if it can be put into the current group, or if a
|
danielebarchiesi@0
|
3196 // new group needs to be made for it.
|
danielebarchiesi@0
|
3197 $current_group_keys = NULL;
|
danielebarchiesi@0
|
3198 // When creating a new group, we pre-increment $i, so by initializing it to
|
danielebarchiesi@0
|
3199 // -1, the first group will have index 0.
|
danielebarchiesi@0
|
3200 $i = -1;
|
danielebarchiesi@0
|
3201 foreach ($css as $item) {
|
danielebarchiesi@0
|
3202 // The browsers for which the CSS item needs to be loaded is part of the
|
danielebarchiesi@0
|
3203 // information that determines when a new group is needed, but the order of
|
danielebarchiesi@0
|
3204 // keys in the array doesn't matter, and we don't want a new group if all
|
danielebarchiesi@0
|
3205 // that's different is that order.
|
danielebarchiesi@0
|
3206 ksort($item['browsers']);
|
danielebarchiesi@0
|
3207
|
danielebarchiesi@0
|
3208 // If the item can be grouped with other items, set $group_keys to an array
|
danielebarchiesi@0
|
3209 // of information that must be the same for all items in its group. If the
|
danielebarchiesi@0
|
3210 // item can't be grouped with other items, set $group_keys to FALSE. We
|
danielebarchiesi@0
|
3211 // put items into a group that can be aggregated together: whether they will
|
danielebarchiesi@0
|
3212 // be aggregated is up to the _drupal_css_aggregate() function or an
|
danielebarchiesi@0
|
3213 // override of that function specified in hook_css_alter(), but regardless
|
danielebarchiesi@0
|
3214 // of the details of that function, a group represents items that can be
|
danielebarchiesi@0
|
3215 // aggregated. Since a group may be rendered with a single HTML tag, all
|
danielebarchiesi@0
|
3216 // items in the group must share the same information that would need to be
|
danielebarchiesi@0
|
3217 // part of that HTML tag.
|
danielebarchiesi@0
|
3218 switch ($item['type']) {
|
danielebarchiesi@0
|
3219 case 'file':
|
danielebarchiesi@0
|
3220 // Group file items if their 'preprocess' flag is TRUE.
|
danielebarchiesi@0
|
3221 // Help ensure maximum reuse of aggregate files by only grouping
|
danielebarchiesi@0
|
3222 // together items that share the same 'group' value and 'every_page'
|
danielebarchiesi@0
|
3223 // flag. See drupal_add_css() for details about that.
|
danielebarchiesi@0
|
3224 $group_keys = $item['preprocess'] ? array($item['type'], $item['group'], $item['every_page'], $item['media'], $item['browsers']) : FALSE;
|
danielebarchiesi@0
|
3225 break;
|
danielebarchiesi@0
|
3226 case 'inline':
|
danielebarchiesi@0
|
3227 // Always group inline items.
|
danielebarchiesi@0
|
3228 $group_keys = array($item['type'], $item['media'], $item['browsers']);
|
danielebarchiesi@0
|
3229 break;
|
danielebarchiesi@0
|
3230 case 'external':
|
danielebarchiesi@0
|
3231 // Do not group external items.
|
danielebarchiesi@0
|
3232 $group_keys = FALSE;
|
danielebarchiesi@0
|
3233 break;
|
danielebarchiesi@0
|
3234 }
|
danielebarchiesi@0
|
3235
|
danielebarchiesi@0
|
3236 // If the group keys don't match the most recent group we're working with,
|
danielebarchiesi@0
|
3237 // then a new group must be made.
|
danielebarchiesi@0
|
3238 if ($group_keys !== $current_group_keys) {
|
danielebarchiesi@0
|
3239 $i++;
|
danielebarchiesi@0
|
3240 // Initialize the new group with the same properties as the first item
|
danielebarchiesi@0
|
3241 // being placed into it. The item's 'data' and 'weight' properties are
|
danielebarchiesi@0
|
3242 // unique to the item and should not be carried over to the group.
|
danielebarchiesi@0
|
3243 $groups[$i] = $item;
|
danielebarchiesi@0
|
3244 unset($groups[$i]['data'], $groups[$i]['weight']);
|
danielebarchiesi@0
|
3245 $groups[$i]['items'] = array();
|
danielebarchiesi@0
|
3246 $current_group_keys = $group_keys ? $group_keys : NULL;
|
danielebarchiesi@0
|
3247 }
|
danielebarchiesi@0
|
3248
|
danielebarchiesi@0
|
3249 // Add the item to the current group.
|
danielebarchiesi@0
|
3250 $groups[$i]['items'][] = $item;
|
danielebarchiesi@0
|
3251 }
|
danielebarchiesi@0
|
3252 return $groups;
|
danielebarchiesi@0
|
3253 }
|
danielebarchiesi@0
|
3254
|
danielebarchiesi@0
|
3255 /**
|
danielebarchiesi@0
|
3256 * Default callback to aggregate CSS files and inline content.
|
danielebarchiesi@0
|
3257 *
|
danielebarchiesi@0
|
3258 * Having the browser load fewer CSS files results in much faster page loads
|
danielebarchiesi@0
|
3259 * than when it loads many small files. This function aggregates files within
|
danielebarchiesi@0
|
3260 * the same group into a single file unless the site-wide setting to do so is
|
danielebarchiesi@0
|
3261 * disabled (commonly the case during site development). To optimize download,
|
danielebarchiesi@0
|
3262 * it also compresses the aggregate files by removing comments, whitespace, and
|
danielebarchiesi@0
|
3263 * other unnecessary content. Additionally, this functions aggregates inline
|
danielebarchiesi@0
|
3264 * content together, regardless of the site-wide aggregation setting.
|
danielebarchiesi@0
|
3265 *
|
danielebarchiesi@0
|
3266 * @param $css_groups
|
danielebarchiesi@0
|
3267 * An array of CSS groups as returned by drupal_group_css(). This function
|
danielebarchiesi@0
|
3268 * modifies the group's 'data' property for each group that is aggregated.
|
danielebarchiesi@0
|
3269 *
|
danielebarchiesi@0
|
3270 * @see drupal_group_css()
|
danielebarchiesi@0
|
3271 * @see drupal_pre_render_styles()
|
danielebarchiesi@0
|
3272 * @see system_element_info()
|
danielebarchiesi@0
|
3273 */
|
danielebarchiesi@0
|
3274 function drupal_aggregate_css(&$css_groups) {
|
danielebarchiesi@0
|
3275 $preprocess_css = (variable_get('preprocess_css', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
|
danielebarchiesi@0
|
3276
|
danielebarchiesi@0
|
3277 // For each group that needs aggregation, aggregate its items.
|
danielebarchiesi@0
|
3278 foreach ($css_groups as $key => $group) {
|
danielebarchiesi@0
|
3279 switch ($group['type']) {
|
danielebarchiesi@0
|
3280 // If a file group can be aggregated into a single file, do so, and set
|
danielebarchiesi@0
|
3281 // the group's data property to the file path of the aggregate file.
|
danielebarchiesi@0
|
3282 case 'file':
|
danielebarchiesi@0
|
3283 if ($group['preprocess'] && $preprocess_css) {
|
danielebarchiesi@0
|
3284 $css_groups[$key]['data'] = drupal_build_css_cache($group['items']);
|
danielebarchiesi@0
|
3285 }
|
danielebarchiesi@0
|
3286 break;
|
danielebarchiesi@0
|
3287 // Aggregate all inline CSS content into the group's data property.
|
danielebarchiesi@0
|
3288 case 'inline':
|
danielebarchiesi@0
|
3289 $css_groups[$key]['data'] = '';
|
danielebarchiesi@0
|
3290 foreach ($group['items'] as $item) {
|
danielebarchiesi@0
|
3291 $css_groups[$key]['data'] .= drupal_load_stylesheet_content($item['data'], $item['preprocess']);
|
danielebarchiesi@0
|
3292 }
|
danielebarchiesi@0
|
3293 break;
|
danielebarchiesi@0
|
3294 }
|
danielebarchiesi@0
|
3295 }
|
danielebarchiesi@0
|
3296 }
|
danielebarchiesi@0
|
3297
|
danielebarchiesi@0
|
3298 /**
|
danielebarchiesi@0
|
3299 * #pre_render callback to add the elements needed for CSS tags to be rendered.
|
danielebarchiesi@0
|
3300 *
|
danielebarchiesi@0
|
3301 * For production websites, LINK tags are preferable to STYLE tags with @import
|
danielebarchiesi@0
|
3302 * statements, because:
|
danielebarchiesi@0
|
3303 * - They are the standard tag intended for linking to a resource.
|
danielebarchiesi@0
|
3304 * - On Firefox 2 and perhaps other browsers, CSS files included with @import
|
danielebarchiesi@0
|
3305 * statements don't get saved when saving the complete web page for offline
|
danielebarchiesi@0
|
3306 * use: http://drupal.org/node/145218.
|
danielebarchiesi@0
|
3307 * - On IE, if only LINK tags and no @import statements are used, all the CSS
|
danielebarchiesi@0
|
3308 * files are downloaded in parallel, resulting in faster page load, but if
|
danielebarchiesi@0
|
3309 * @import statements are used and span across multiple STYLE tags, all the
|
danielebarchiesi@0
|
3310 * ones from one STYLE tag must be downloaded before downloading begins for
|
danielebarchiesi@0
|
3311 * the next STYLE tag. Furthermore, IE7 does not support media declaration on
|
danielebarchiesi@0
|
3312 * the @import statement, so multiple STYLE tags must be used when different
|
danielebarchiesi@0
|
3313 * files are for different media types. Non-IE browsers always download in
|
danielebarchiesi@0
|
3314 * parallel, so this is an IE-specific performance quirk:
|
danielebarchiesi@0
|
3315 * http://www.stevesouders.com/blog/2009/04/09/dont-use-import/.
|
danielebarchiesi@0
|
3316 *
|
danielebarchiesi@0
|
3317 * However, IE has an annoying limit of 31 total CSS inclusion tags
|
danielebarchiesi@0
|
3318 * (http://drupal.org/node/228818) and LINK tags are limited to one file per
|
danielebarchiesi@0
|
3319 * tag, whereas STYLE tags can contain multiple @import statements allowing
|
danielebarchiesi@0
|
3320 * multiple files to be loaded per tag. When CSS aggregation is disabled, a
|
danielebarchiesi@0
|
3321 * Drupal site can easily have more than 31 CSS files that need to be loaded, so
|
danielebarchiesi@0
|
3322 * using LINK tags exclusively would result in a site that would display
|
danielebarchiesi@0
|
3323 * incorrectly in IE. Depending on different needs, different strategies can be
|
danielebarchiesi@0
|
3324 * employed to decide when to use LINK tags and when to use STYLE tags.
|
danielebarchiesi@0
|
3325 *
|
danielebarchiesi@0
|
3326 * The strategy employed by this function is to use LINK tags for all aggregate
|
danielebarchiesi@0
|
3327 * files and for all files that cannot be aggregated (e.g., if 'preprocess' is
|
danielebarchiesi@0
|
3328 * set to FALSE or the type is 'external'), and to use STYLE tags for groups
|
danielebarchiesi@0
|
3329 * of files that could be aggregated together but aren't (e.g., if the site-wide
|
danielebarchiesi@0
|
3330 * aggregation setting is disabled). This results in all LINK tags when
|
danielebarchiesi@0
|
3331 * aggregation is enabled, a guarantee that as many or only slightly more tags
|
danielebarchiesi@0
|
3332 * are used with aggregation disabled than enabled (so that if the limit were to
|
danielebarchiesi@0
|
3333 * be crossed with aggregation enabled, the site developer would also notice the
|
danielebarchiesi@0
|
3334 * problem while aggregation is disabled), and an easy way for a developer to
|
danielebarchiesi@0
|
3335 * view HTML source while aggregation is disabled and know what files will be
|
danielebarchiesi@0
|
3336 * aggregated together when aggregation becomes enabled.
|
danielebarchiesi@0
|
3337 *
|
danielebarchiesi@0
|
3338 * This function evaluates the aggregation enabled/disabled condition on a group
|
danielebarchiesi@0
|
3339 * by group basis by testing whether an aggregate file has been made for the
|
danielebarchiesi@0
|
3340 * group rather than by testing the site-wide aggregation setting. This allows
|
danielebarchiesi@0
|
3341 * this function to work correctly even if modules have implemented custom
|
danielebarchiesi@0
|
3342 * logic for grouping and aggregating files.
|
danielebarchiesi@0
|
3343 *
|
danielebarchiesi@0
|
3344 * @param $element
|
danielebarchiesi@0
|
3345 * A render array containing:
|
danielebarchiesi@0
|
3346 * - '#items': The CSS items as returned by drupal_add_css() and altered by
|
danielebarchiesi@0
|
3347 * drupal_get_css().
|
danielebarchiesi@0
|
3348 * - '#group_callback': A function to call to group #items to enable the use
|
danielebarchiesi@0
|
3349 * of fewer tags by aggregating files and/or using multiple @import
|
danielebarchiesi@0
|
3350 * statements within a single tag.
|
danielebarchiesi@0
|
3351 * - '#aggregate_callback': A function to call to aggregate the items within
|
danielebarchiesi@0
|
3352 * the groups arranged by the #group_callback function.
|
danielebarchiesi@0
|
3353 *
|
danielebarchiesi@0
|
3354 * @return
|
danielebarchiesi@0
|
3355 * A render array that will render to a string of XHTML CSS tags.
|
danielebarchiesi@0
|
3356 *
|
danielebarchiesi@0
|
3357 * @see drupal_get_css()
|
danielebarchiesi@0
|
3358 */
|
danielebarchiesi@0
|
3359 function drupal_pre_render_styles($elements) {
|
danielebarchiesi@0
|
3360 // Group and aggregate the items.
|
danielebarchiesi@0
|
3361 if (isset($elements['#group_callback'])) {
|
danielebarchiesi@0
|
3362 $elements['#groups'] = $elements['#group_callback']($elements['#items']);
|
danielebarchiesi@0
|
3363 }
|
danielebarchiesi@0
|
3364 if (isset($elements['#aggregate_callback'])) {
|
danielebarchiesi@0
|
3365 $elements['#aggregate_callback']($elements['#groups']);
|
danielebarchiesi@0
|
3366 }
|
danielebarchiesi@0
|
3367
|
danielebarchiesi@0
|
3368 // A dummy query-string is added to filenames, to gain control over
|
danielebarchiesi@0
|
3369 // browser-caching. The string changes on every update or full cache
|
danielebarchiesi@0
|
3370 // flush, forcing browsers to load a new copy of the files, as the
|
danielebarchiesi@0
|
3371 // URL changed.
|
danielebarchiesi@0
|
3372 $query_string = variable_get('css_js_query_string', '0');
|
danielebarchiesi@0
|
3373
|
danielebarchiesi@0
|
3374 // For inline CSS to validate as XHTML, all CSS containing XHTML needs to be
|
danielebarchiesi@0
|
3375 // wrapped in CDATA. To make that backwards compatible with HTML 4, we need to
|
danielebarchiesi@0
|
3376 // comment out the CDATA-tag.
|
danielebarchiesi@0
|
3377 $embed_prefix = "\n<!--/*--><![CDATA[/*><!--*/\n";
|
danielebarchiesi@0
|
3378 $embed_suffix = "\n/*]]>*/-->\n";
|
danielebarchiesi@0
|
3379
|
danielebarchiesi@0
|
3380 // Defaults for LINK and STYLE elements.
|
danielebarchiesi@0
|
3381 $link_element_defaults = array(
|
danielebarchiesi@0
|
3382 '#type' => 'html_tag',
|
danielebarchiesi@0
|
3383 '#tag' => 'link',
|
danielebarchiesi@0
|
3384 '#attributes' => array(
|
danielebarchiesi@0
|
3385 'type' => 'text/css',
|
danielebarchiesi@0
|
3386 'rel' => 'stylesheet',
|
danielebarchiesi@0
|
3387 ),
|
danielebarchiesi@0
|
3388 );
|
danielebarchiesi@0
|
3389 $style_element_defaults = array(
|
danielebarchiesi@0
|
3390 '#type' => 'html_tag',
|
danielebarchiesi@0
|
3391 '#tag' => 'style',
|
danielebarchiesi@0
|
3392 '#attributes' => array(
|
danielebarchiesi@0
|
3393 'type' => 'text/css',
|
danielebarchiesi@0
|
3394 ),
|
danielebarchiesi@0
|
3395 );
|
danielebarchiesi@0
|
3396
|
danielebarchiesi@0
|
3397 // Loop through each group.
|
danielebarchiesi@0
|
3398 foreach ($elements['#groups'] as $group) {
|
danielebarchiesi@0
|
3399 switch ($group['type']) {
|
danielebarchiesi@0
|
3400 // For file items, there are three possibilites.
|
danielebarchiesi@0
|
3401 // - The group has been aggregated: in this case, output a LINK tag for
|
danielebarchiesi@0
|
3402 // the aggregate file.
|
danielebarchiesi@0
|
3403 // - The group can be aggregated but has not been (most likely because
|
danielebarchiesi@0
|
3404 // the site administrator disabled the site-wide setting): in this case,
|
danielebarchiesi@0
|
3405 // output as few STYLE tags for the group as possible, using @import
|
danielebarchiesi@0
|
3406 // statement for each file in the group. This enables us to stay within
|
danielebarchiesi@0
|
3407 // IE's limit of 31 total CSS inclusion tags.
|
danielebarchiesi@0
|
3408 // - The group contains items not eligible for aggregation (their
|
danielebarchiesi@0
|
3409 // 'preprocess' flag has been set to FALSE): in this case, output a LINK
|
danielebarchiesi@0
|
3410 // tag for each file.
|
danielebarchiesi@0
|
3411 case 'file':
|
danielebarchiesi@0
|
3412 // The group has been aggregated into a single file: output a LINK tag
|
danielebarchiesi@0
|
3413 // for the aggregate file.
|
danielebarchiesi@0
|
3414 if (isset($group['data'])) {
|
danielebarchiesi@0
|
3415 $element = $link_element_defaults;
|
danielebarchiesi@0
|
3416 $element['#attributes']['href'] = file_create_url($group['data']);
|
danielebarchiesi@0
|
3417 $element['#attributes']['media'] = $group['media'];
|
danielebarchiesi@0
|
3418 $element['#browsers'] = $group['browsers'];
|
danielebarchiesi@0
|
3419 $elements[] = $element;
|
danielebarchiesi@0
|
3420 }
|
danielebarchiesi@0
|
3421 // The group can be aggregated, but hasn't been: combine multiple items
|
danielebarchiesi@0
|
3422 // into as few STYLE tags as possible.
|
danielebarchiesi@0
|
3423 elseif ($group['preprocess']) {
|
danielebarchiesi@0
|
3424 $import = array();
|
danielebarchiesi@0
|
3425 foreach ($group['items'] as $item) {
|
danielebarchiesi@0
|
3426 // A theme's .info file may have an entry for a file that doesn't
|
danielebarchiesi@0
|
3427 // exist as a way of overriding a module or base theme CSS file from
|
danielebarchiesi@0
|
3428 // being added to the page. Normally, file_exists() calls that need
|
danielebarchiesi@0
|
3429 // to run for every page request should be minimized, but this one
|
danielebarchiesi@0
|
3430 // is okay, because it only runs when CSS aggregation is disabled.
|
danielebarchiesi@0
|
3431 // On a server under heavy enough load that file_exists() calls need
|
danielebarchiesi@0
|
3432 // to be minimized, CSS aggregation should be enabled, in which case
|
danielebarchiesi@0
|
3433 // this code is not run. When aggregation is enabled,
|
danielebarchiesi@0
|
3434 // drupal_load_stylesheet() checks file_exists(), but only when
|
danielebarchiesi@0
|
3435 // building the aggregate file, which is then reused for many page
|
danielebarchiesi@0
|
3436 // requests.
|
danielebarchiesi@0
|
3437 if (file_exists($item['data'])) {
|
danielebarchiesi@0
|
3438 // The dummy query string needs to be added to the URL to control
|
danielebarchiesi@0
|
3439 // browser-caching. IE7 does not support a media type on the
|
danielebarchiesi@0
|
3440 // @import statement, so we instead specify the media for the
|
danielebarchiesi@0
|
3441 // group on the STYLE tag.
|
danielebarchiesi@0
|
3442 $import[] = '@import url("' . check_plain(file_create_url($item['data']) . '?' . $query_string) . '");';
|
danielebarchiesi@0
|
3443 }
|
danielebarchiesi@0
|
3444 }
|
danielebarchiesi@0
|
3445 // In addition to IE's limit of 31 total CSS inclusion tags, it also
|
danielebarchiesi@0
|
3446 // has a limit of 31 @import statements per STYLE tag.
|
danielebarchiesi@0
|
3447 while (!empty($import)) {
|
danielebarchiesi@0
|
3448 $import_batch = array_slice($import, 0, 31);
|
danielebarchiesi@0
|
3449 $import = array_slice($import, 31);
|
danielebarchiesi@0
|
3450 $element = $style_element_defaults;
|
danielebarchiesi@0
|
3451 $element['#value'] = implode("\n", $import_batch);
|
danielebarchiesi@0
|
3452 $element['#attributes']['media'] = $group['media'];
|
danielebarchiesi@0
|
3453 $element['#browsers'] = $group['browsers'];
|
danielebarchiesi@0
|
3454 $elements[] = $element;
|
danielebarchiesi@0
|
3455 }
|
danielebarchiesi@0
|
3456 }
|
danielebarchiesi@0
|
3457 // The group contains items ineligible for aggregation: output a LINK
|
danielebarchiesi@0
|
3458 // tag for each file.
|
danielebarchiesi@0
|
3459 else {
|
danielebarchiesi@0
|
3460 foreach ($group['items'] as $item) {
|
danielebarchiesi@0
|
3461 $element = $link_element_defaults;
|
danielebarchiesi@0
|
3462 // We do not check file_exists() here, because this code runs for
|
danielebarchiesi@0
|
3463 // files whose 'preprocess' is set to FALSE, and therefore, even
|
danielebarchiesi@0
|
3464 // when aggregation is enabled, and we want to avoid needlessly
|
danielebarchiesi@0
|
3465 // taxing a server that may be under heavy load. The file_exists()
|
danielebarchiesi@0
|
3466 // performed above for files whose 'preprocess' is TRUE is done for
|
danielebarchiesi@0
|
3467 // the benefit of theme .info files, but code that deals with files
|
danielebarchiesi@0
|
3468 // whose 'preprocess' is FALSE is responsible for ensuring the file
|
danielebarchiesi@0
|
3469 // exists.
|
danielebarchiesi@0
|
3470 // The dummy query string needs to be added to the URL to control
|
danielebarchiesi@0
|
3471 // browser-caching.
|
danielebarchiesi@0
|
3472 $query_string_separator = (strpos($item['data'], '?') !== FALSE) ? '&' : '?';
|
danielebarchiesi@0
|
3473 $element['#attributes']['href'] = file_create_url($item['data']) . $query_string_separator . $query_string;
|
danielebarchiesi@0
|
3474 $element['#attributes']['media'] = $item['media'];
|
danielebarchiesi@0
|
3475 $element['#browsers'] = $group['browsers'];
|
danielebarchiesi@0
|
3476 $elements[] = $element;
|
danielebarchiesi@0
|
3477 }
|
danielebarchiesi@0
|
3478 }
|
danielebarchiesi@0
|
3479 break;
|
danielebarchiesi@0
|
3480 // For inline content, the 'data' property contains the CSS content. If
|
danielebarchiesi@0
|
3481 // the group's 'data' property is set, then output it in a single STYLE
|
danielebarchiesi@0
|
3482 // tag. Otherwise, output a separate STYLE tag for each item.
|
danielebarchiesi@0
|
3483 case 'inline':
|
danielebarchiesi@0
|
3484 if (isset($group['data'])) {
|
danielebarchiesi@0
|
3485 $element = $style_element_defaults;
|
danielebarchiesi@0
|
3486 $element['#value'] = $group['data'];
|
danielebarchiesi@0
|
3487 $element['#value_prefix'] = $embed_prefix;
|
danielebarchiesi@0
|
3488 $element['#value_suffix'] = $embed_suffix;
|
danielebarchiesi@0
|
3489 $element['#attributes']['media'] = $group['media'];
|
danielebarchiesi@0
|
3490 $element['#browsers'] = $group['browsers'];
|
danielebarchiesi@0
|
3491 $elements[] = $element;
|
danielebarchiesi@0
|
3492 }
|
danielebarchiesi@0
|
3493 else {
|
danielebarchiesi@0
|
3494 foreach ($group['items'] as $item) {
|
danielebarchiesi@0
|
3495 $element = $style_element_defaults;
|
danielebarchiesi@0
|
3496 $element['#value'] = $item['data'];
|
danielebarchiesi@0
|
3497 $element['#value_prefix'] = $embed_prefix;
|
danielebarchiesi@0
|
3498 $element['#value_suffix'] = $embed_suffix;
|
danielebarchiesi@0
|
3499 $element['#attributes']['media'] = $item['media'];
|
danielebarchiesi@0
|
3500 $element['#browsers'] = $group['browsers'];
|
danielebarchiesi@0
|
3501 $elements[] = $element;
|
danielebarchiesi@0
|
3502 }
|
danielebarchiesi@0
|
3503 }
|
danielebarchiesi@0
|
3504 break;
|
danielebarchiesi@0
|
3505 // Output a LINK tag for each external item. The item's 'data' property
|
danielebarchiesi@0
|
3506 // contains the full URL.
|
danielebarchiesi@0
|
3507 case 'external':
|
danielebarchiesi@0
|
3508 foreach ($group['items'] as $item) {
|
danielebarchiesi@0
|
3509 $element = $link_element_defaults;
|
danielebarchiesi@0
|
3510 $element['#attributes']['href'] = $item['data'];
|
danielebarchiesi@0
|
3511 $element['#attributes']['media'] = $item['media'];
|
danielebarchiesi@0
|
3512 $element['#browsers'] = $group['browsers'];
|
danielebarchiesi@0
|
3513 $elements[] = $element;
|
danielebarchiesi@0
|
3514 }
|
danielebarchiesi@0
|
3515 break;
|
danielebarchiesi@0
|
3516 }
|
danielebarchiesi@0
|
3517 }
|
danielebarchiesi@0
|
3518
|
danielebarchiesi@0
|
3519 return $elements;
|
danielebarchiesi@0
|
3520 }
|
danielebarchiesi@0
|
3521
|
danielebarchiesi@0
|
3522 /**
|
danielebarchiesi@0
|
3523 * Aggregates and optimizes CSS files into a cache file in the files directory.
|
danielebarchiesi@0
|
3524 *
|
danielebarchiesi@0
|
3525 * The file name for the CSS cache file is generated from the hash of the
|
danielebarchiesi@0
|
3526 * aggregated contents of the files in $css. This forces proxies and browsers
|
danielebarchiesi@0
|
3527 * to download new CSS when the CSS changes.
|
danielebarchiesi@0
|
3528 *
|
danielebarchiesi@0
|
3529 * The cache file name is retrieved on a page load via a lookup variable that
|
danielebarchiesi@0
|
3530 * contains an associative array. The array key is the hash of the file names
|
danielebarchiesi@0
|
3531 * in $css while the value is the cache file name. The cache file is generated
|
danielebarchiesi@0
|
3532 * in two cases. First, if there is no file name value for the key, which will
|
danielebarchiesi@0
|
3533 * happen if a new file name has been added to $css or after the lookup
|
danielebarchiesi@0
|
3534 * variable is emptied to force a rebuild of the cache. Second, the cache file
|
danielebarchiesi@0
|
3535 * is generated if it is missing on disk. Old cache files are not deleted
|
danielebarchiesi@0
|
3536 * immediately when the lookup variable is emptied, but are deleted after a set
|
danielebarchiesi@0
|
3537 * period by drupal_delete_file_if_stale(). This ensures that files referenced
|
danielebarchiesi@0
|
3538 * by a cached page will still be available.
|
danielebarchiesi@0
|
3539 *
|
danielebarchiesi@0
|
3540 * @param $css
|
danielebarchiesi@0
|
3541 * An array of CSS files to aggregate and compress into one file.
|
danielebarchiesi@0
|
3542 *
|
danielebarchiesi@0
|
3543 * @return
|
danielebarchiesi@0
|
3544 * The URI of the CSS cache file, or FALSE if the file could not be saved.
|
danielebarchiesi@0
|
3545 */
|
danielebarchiesi@0
|
3546 function drupal_build_css_cache($css) {
|
danielebarchiesi@0
|
3547 $data = '';
|
danielebarchiesi@0
|
3548 $uri = '';
|
danielebarchiesi@0
|
3549 $map = variable_get('drupal_css_cache_files', array());
|
danielebarchiesi@0
|
3550 // Create a new array so that only the file names are used to create the hash.
|
danielebarchiesi@0
|
3551 // This prevents new aggregates from being created unnecessarily.
|
danielebarchiesi@0
|
3552 $css_data = array();
|
danielebarchiesi@0
|
3553 foreach ($css as $css_file) {
|
danielebarchiesi@0
|
3554 $css_data[] = $css_file['data'];
|
danielebarchiesi@0
|
3555 }
|
danielebarchiesi@0
|
3556 $key = hash('sha256', serialize($css_data));
|
danielebarchiesi@0
|
3557 if (isset($map[$key])) {
|
danielebarchiesi@0
|
3558 $uri = $map[$key];
|
danielebarchiesi@0
|
3559 }
|
danielebarchiesi@0
|
3560
|
danielebarchiesi@0
|
3561 if (empty($uri) || !file_exists($uri)) {
|
danielebarchiesi@0
|
3562 // Build aggregate CSS file.
|
danielebarchiesi@0
|
3563 foreach ($css as $stylesheet) {
|
danielebarchiesi@0
|
3564 // Only 'file' stylesheets can be aggregated.
|
danielebarchiesi@0
|
3565 if ($stylesheet['type'] == 'file') {
|
danielebarchiesi@0
|
3566 $contents = drupal_load_stylesheet($stylesheet['data'], TRUE);
|
danielebarchiesi@0
|
3567
|
danielebarchiesi@0
|
3568 // Build the base URL of this CSS file: start with the full URL.
|
danielebarchiesi@0
|
3569 $css_base_url = file_create_url($stylesheet['data']);
|
danielebarchiesi@0
|
3570 // Move to the parent.
|
danielebarchiesi@0
|
3571 $css_base_url = substr($css_base_url, 0, strrpos($css_base_url, '/'));
|
danielebarchiesi@0
|
3572 // Simplify to a relative URL if the stylesheet URL starts with the
|
danielebarchiesi@0
|
3573 // base URL of the website.
|
danielebarchiesi@0
|
3574 if (substr($css_base_url, 0, strlen($GLOBALS['base_root'])) == $GLOBALS['base_root']) {
|
danielebarchiesi@0
|
3575 $css_base_url = substr($css_base_url, strlen($GLOBALS['base_root']));
|
danielebarchiesi@0
|
3576 }
|
danielebarchiesi@0
|
3577
|
danielebarchiesi@0
|
3578 _drupal_build_css_path(NULL, $css_base_url . '/');
|
danielebarchiesi@0
|
3579 // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths.
|
danielebarchiesi@0
|
3580 $data .= preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $contents);
|
danielebarchiesi@0
|
3581 }
|
danielebarchiesi@0
|
3582 }
|
danielebarchiesi@0
|
3583
|
danielebarchiesi@0
|
3584 // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import,
|
danielebarchiesi@0
|
3585 // @import rules must proceed any other style, so we move those to the top.
|
danielebarchiesi@0
|
3586 $regexp = '/@import[^;]+;/i';
|
danielebarchiesi@0
|
3587 preg_match_all($regexp, $data, $matches);
|
danielebarchiesi@0
|
3588 $data = preg_replace($regexp, '', $data);
|
danielebarchiesi@0
|
3589 $data = implode('', $matches[0]) . $data;
|
danielebarchiesi@0
|
3590
|
danielebarchiesi@0
|
3591 // Prefix filename to prevent blocking by firewalls which reject files
|
danielebarchiesi@0
|
3592 // starting with "ad*".
|
danielebarchiesi@0
|
3593 $filename = 'css_' . drupal_hash_base64($data) . '.css';
|
danielebarchiesi@0
|
3594 // Create the css/ within the files folder.
|
danielebarchiesi@0
|
3595 $csspath = 'public://css';
|
danielebarchiesi@0
|
3596 $uri = $csspath . '/' . $filename;
|
danielebarchiesi@0
|
3597 // Create the CSS file.
|
danielebarchiesi@0
|
3598 file_prepare_directory($csspath, FILE_CREATE_DIRECTORY);
|
danielebarchiesi@0
|
3599 if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) {
|
danielebarchiesi@0
|
3600 return FALSE;
|
danielebarchiesi@0
|
3601 }
|
danielebarchiesi@0
|
3602 // If CSS gzip compression is enabled, clean URLs are enabled (which means
|
danielebarchiesi@0
|
3603 // that rewrite rules are working) and the zlib extension is available then
|
danielebarchiesi@0
|
3604 // create a gzipped version of this file. This file is served conditionally
|
danielebarchiesi@0
|
3605 // to browsers that accept gzip using .htaccess rules.
|
danielebarchiesi@0
|
3606 if (variable_get('css_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) {
|
danielebarchiesi@0
|
3607 if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {
|
danielebarchiesi@0
|
3608 return FALSE;
|
danielebarchiesi@0
|
3609 }
|
danielebarchiesi@0
|
3610 }
|
danielebarchiesi@0
|
3611 // Save the updated map.
|
danielebarchiesi@0
|
3612 $map[$key] = $uri;
|
danielebarchiesi@0
|
3613 variable_set('drupal_css_cache_files', $map);
|
danielebarchiesi@0
|
3614 }
|
danielebarchiesi@0
|
3615 return $uri;
|
danielebarchiesi@0
|
3616 }
|
danielebarchiesi@0
|
3617
|
danielebarchiesi@0
|
3618 /**
|
danielebarchiesi@0
|
3619 * Prefixes all paths within a CSS file for drupal_build_css_cache().
|
danielebarchiesi@0
|
3620 */
|
danielebarchiesi@0
|
3621 function _drupal_build_css_path($matches, $base = NULL) {
|
danielebarchiesi@0
|
3622 $_base = &drupal_static(__FUNCTION__);
|
danielebarchiesi@0
|
3623 // Store base path for preg_replace_callback.
|
danielebarchiesi@0
|
3624 if (isset($base)) {
|
danielebarchiesi@0
|
3625 $_base = $base;
|
danielebarchiesi@0
|
3626 }
|
danielebarchiesi@0
|
3627
|
danielebarchiesi@0
|
3628 // Prefix with base and remove '../' segments where possible.
|
danielebarchiesi@0
|
3629 $path = $_base . $matches[1];
|
danielebarchiesi@0
|
3630 $last = '';
|
danielebarchiesi@0
|
3631 while ($path != $last) {
|
danielebarchiesi@0
|
3632 $last = $path;
|
danielebarchiesi@0
|
3633 $path = preg_replace('`(^|/)(?!\.\./)([^/]+)/\.\./`', '$1', $path);
|
danielebarchiesi@0
|
3634 }
|
danielebarchiesi@0
|
3635 return 'url(' . $path . ')';
|
danielebarchiesi@0
|
3636 }
|
danielebarchiesi@0
|
3637
|
danielebarchiesi@0
|
3638 /**
|
danielebarchiesi@0
|
3639 * Loads the stylesheet and resolves all @import commands.
|
danielebarchiesi@0
|
3640 *
|
danielebarchiesi@0
|
3641 * Loads a stylesheet and replaces @import commands with the contents of the
|
danielebarchiesi@0
|
3642 * imported file. Use this instead of file_get_contents when processing
|
danielebarchiesi@0
|
3643 * stylesheets.
|
danielebarchiesi@0
|
3644 *
|
danielebarchiesi@0
|
3645 * The returned contents are compressed removing white space and comments only
|
danielebarchiesi@0
|
3646 * when CSS aggregation is enabled. This optimization will not apply for
|
danielebarchiesi@0
|
3647 * color.module enabled themes with CSS aggregation turned off.
|
danielebarchiesi@0
|
3648 *
|
danielebarchiesi@0
|
3649 * @param $file
|
danielebarchiesi@0
|
3650 * Name of the stylesheet to be processed.
|
danielebarchiesi@0
|
3651 * @param $optimize
|
danielebarchiesi@0
|
3652 * Defines if CSS contents should be compressed or not.
|
danielebarchiesi@0
|
3653 * @param $reset_basepath
|
danielebarchiesi@0
|
3654 * Used internally to facilitate recursive resolution of @import commands.
|
danielebarchiesi@0
|
3655 *
|
danielebarchiesi@0
|
3656 * @return
|
danielebarchiesi@0
|
3657 * Contents of the stylesheet, including any resolved @import commands.
|
danielebarchiesi@0
|
3658 */
|
danielebarchiesi@0
|
3659 function drupal_load_stylesheet($file, $optimize = NULL, $reset_basepath = TRUE) {
|
danielebarchiesi@0
|
3660 // These statics are not cache variables, so we don't use drupal_static().
|
danielebarchiesi@0
|
3661 static $_optimize, $basepath;
|
danielebarchiesi@0
|
3662 if ($reset_basepath) {
|
danielebarchiesi@0
|
3663 $basepath = '';
|
danielebarchiesi@0
|
3664 }
|
danielebarchiesi@0
|
3665 // Store the value of $optimize for preg_replace_callback with nested
|
danielebarchiesi@0
|
3666 // @import loops.
|
danielebarchiesi@0
|
3667 if (isset($optimize)) {
|
danielebarchiesi@0
|
3668 $_optimize = $optimize;
|
danielebarchiesi@0
|
3669 }
|
danielebarchiesi@0
|
3670
|
danielebarchiesi@0
|
3671 // Stylesheets are relative one to each other. Start by adding a base path
|
danielebarchiesi@0
|
3672 // prefix provided by the parent stylesheet (if necessary).
|
danielebarchiesi@0
|
3673 if ($basepath && !file_uri_scheme($file)) {
|
danielebarchiesi@0
|
3674 $file = $basepath . '/' . $file;
|
danielebarchiesi@0
|
3675 }
|
danielebarchiesi@0
|
3676 $basepath = dirname($file);
|
danielebarchiesi@0
|
3677
|
danielebarchiesi@0
|
3678 // Load the CSS stylesheet. We suppress errors because themes may specify
|
danielebarchiesi@0
|
3679 // stylesheets in their .info file that don't exist in the theme's path,
|
danielebarchiesi@0
|
3680 // but are merely there to disable certain module CSS files.
|
danielebarchiesi@0
|
3681 if ($contents = @file_get_contents($file)) {
|
danielebarchiesi@0
|
3682 // Return the processed stylesheet.
|
danielebarchiesi@0
|
3683 return drupal_load_stylesheet_content($contents, $_optimize);
|
danielebarchiesi@0
|
3684 }
|
danielebarchiesi@0
|
3685
|
danielebarchiesi@0
|
3686 return '';
|
danielebarchiesi@0
|
3687 }
|
danielebarchiesi@0
|
3688
|
danielebarchiesi@0
|
3689 /**
|
danielebarchiesi@0
|
3690 * Processes the contents of a stylesheet for aggregation.
|
danielebarchiesi@0
|
3691 *
|
danielebarchiesi@0
|
3692 * @param $contents
|
danielebarchiesi@0
|
3693 * The contents of the stylesheet.
|
danielebarchiesi@0
|
3694 * @param $optimize
|
danielebarchiesi@0
|
3695 * (optional) Boolean whether CSS contents should be minified. Defaults to
|
danielebarchiesi@0
|
3696 * FALSE.
|
danielebarchiesi@0
|
3697 *
|
danielebarchiesi@0
|
3698 * @return
|
danielebarchiesi@0
|
3699 * Contents of the stylesheet including the imported stylesheets.
|
danielebarchiesi@0
|
3700 */
|
danielebarchiesi@0
|
3701 function drupal_load_stylesheet_content($contents, $optimize = FALSE) {
|
danielebarchiesi@0
|
3702 // Remove multiple charset declarations for standards compliance (and fixing Safari problems).
|
danielebarchiesi@0
|
3703 $contents = preg_replace('/^@charset\s+[\'"](\S*)\b[\'"];/i', '', $contents);
|
danielebarchiesi@0
|
3704
|
danielebarchiesi@0
|
3705 if ($optimize) {
|
danielebarchiesi@0
|
3706 // Perform some safe CSS optimizations.
|
danielebarchiesi@0
|
3707 // Regexp to match comment blocks.
|
danielebarchiesi@0
|
3708 $comment = '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/';
|
danielebarchiesi@0
|
3709 // Regexp to match double quoted strings.
|
danielebarchiesi@0
|
3710 $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
|
danielebarchiesi@0
|
3711 // Regexp to match single quoted strings.
|
danielebarchiesi@0
|
3712 $single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";
|
danielebarchiesi@0
|
3713 // Strip all comment blocks, but keep double/single quoted strings.
|
danielebarchiesi@0
|
3714 $contents = preg_replace(
|
danielebarchiesi@0
|
3715 "<($double_quot|$single_quot)|$comment>Ss",
|
danielebarchiesi@0
|
3716 "$1",
|
danielebarchiesi@0
|
3717 $contents
|
danielebarchiesi@0
|
3718 );
|
danielebarchiesi@0
|
3719 // Remove certain whitespace.
|
danielebarchiesi@0
|
3720 // There are different conditions for removing leading and trailing
|
danielebarchiesi@0
|
3721 // whitespace.
|
danielebarchiesi@0
|
3722 // @see http://php.net/manual/en/regexp.reference.subpatterns.php
|
danielebarchiesi@0
|
3723 $contents = preg_replace('<
|
danielebarchiesi@0
|
3724 # Strip leading and trailing whitespace.
|
danielebarchiesi@0
|
3725 \s*([@{};,])\s*
|
danielebarchiesi@0
|
3726 # Strip only leading whitespace from:
|
danielebarchiesi@0
|
3727 # - Closing parenthesis: Retain "@media (bar) and foo".
|
danielebarchiesi@0
|
3728 | \s+([\)])
|
danielebarchiesi@0
|
3729 # Strip only trailing whitespace from:
|
danielebarchiesi@0
|
3730 # - Opening parenthesis: Retain "@media (bar) and foo".
|
danielebarchiesi@0
|
3731 # - Colon: Retain :pseudo-selectors.
|
danielebarchiesi@0
|
3732 | ([\(:])\s+
|
danielebarchiesi@0
|
3733 >xS',
|
danielebarchiesi@0
|
3734 // Only one of the three capturing groups will match, so its reference
|
danielebarchiesi@0
|
3735 // will contain the wanted value and the references for the
|
danielebarchiesi@0
|
3736 // two non-matching groups will be replaced with empty strings.
|
danielebarchiesi@0
|
3737 '$1$2$3',
|
danielebarchiesi@0
|
3738 $contents
|
danielebarchiesi@0
|
3739 );
|
danielebarchiesi@0
|
3740 // End the file with a new line.
|
danielebarchiesi@0
|
3741 $contents = trim($contents);
|
danielebarchiesi@0
|
3742 $contents .= "\n";
|
danielebarchiesi@0
|
3743 }
|
danielebarchiesi@0
|
3744
|
danielebarchiesi@0
|
3745 // Replaces @import commands with the actual stylesheet content.
|
danielebarchiesi@0
|
3746 // This happens recursively but omits external files.
|
danielebarchiesi@0
|
3747 $contents = preg_replace_callback('/@import\s*(?:url\(\s*)?[\'"]?(?![a-z]+:)([^\'"\()]+)[\'"]?\s*\)?\s*;/', '_drupal_load_stylesheet', $contents);
|
danielebarchiesi@0
|
3748 return $contents;
|
danielebarchiesi@0
|
3749 }
|
danielebarchiesi@0
|
3750
|
danielebarchiesi@0
|
3751 /**
|
danielebarchiesi@0
|
3752 * Loads stylesheets recursively and returns contents with corrected paths.
|
danielebarchiesi@0
|
3753 *
|
danielebarchiesi@0
|
3754 * This function is used for recursive loading of stylesheets and
|
danielebarchiesi@0
|
3755 * returns the stylesheet content with all url() paths corrected.
|
danielebarchiesi@0
|
3756 */
|
danielebarchiesi@0
|
3757 function _drupal_load_stylesheet($matches) {
|
danielebarchiesi@0
|
3758 $filename = $matches[1];
|
danielebarchiesi@0
|
3759 // Load the imported stylesheet and replace @import commands in there as well.
|
danielebarchiesi@0
|
3760 $file = drupal_load_stylesheet($filename, NULL, FALSE);
|
danielebarchiesi@0
|
3761
|
danielebarchiesi@0
|
3762 // Determine the file's directory.
|
danielebarchiesi@0
|
3763 $directory = dirname($filename);
|
danielebarchiesi@0
|
3764 // If the file is in the current directory, make sure '.' doesn't appear in
|
danielebarchiesi@0
|
3765 // the url() path.
|
danielebarchiesi@0
|
3766 $directory = $directory == '.' ? '' : $directory .'/';
|
danielebarchiesi@0
|
3767
|
danielebarchiesi@0
|
3768 // Alter all internal url() paths. Leave external paths alone. We don't need
|
danielebarchiesi@0
|
3769 // to normalize absolute paths here (i.e. remove folder/... segments) because
|
danielebarchiesi@0
|
3770 // that will be done later.
|
danielebarchiesi@0
|
3771 return preg_replace('/url\(\s*([\'"]?)(?![a-z]+:|\/+)/i', 'url(\1'. $directory, $file);
|
danielebarchiesi@0
|
3772 }
|
danielebarchiesi@0
|
3773
|
danielebarchiesi@0
|
3774 /**
|
danielebarchiesi@0
|
3775 * Deletes old cached CSS files.
|
danielebarchiesi@0
|
3776 */
|
danielebarchiesi@0
|
3777 function drupal_clear_css_cache() {
|
danielebarchiesi@0
|
3778 variable_del('drupal_css_cache_files');
|
danielebarchiesi@0
|
3779 file_scan_directory('public://css', '/.*/', array('callback' => 'drupal_delete_file_if_stale'));
|
danielebarchiesi@0
|
3780 }
|
danielebarchiesi@0
|
3781
|
danielebarchiesi@0
|
3782 /**
|
danielebarchiesi@0
|
3783 * Callback to delete files modified more than a set time ago.
|
danielebarchiesi@0
|
3784 */
|
danielebarchiesi@0
|
3785 function drupal_delete_file_if_stale($uri) {
|
danielebarchiesi@0
|
3786 // Default stale file threshold is 30 days.
|
danielebarchiesi@0
|
3787 if (REQUEST_TIME - filemtime($uri) > variable_get('drupal_stale_file_threshold', 2592000)) {
|
danielebarchiesi@0
|
3788 file_unmanaged_delete($uri);
|
danielebarchiesi@0
|
3789 }
|
danielebarchiesi@0
|
3790 }
|
danielebarchiesi@0
|
3791
|
danielebarchiesi@0
|
3792 /**
|
danielebarchiesi@0
|
3793 * Prepares a string for use as a CSS identifier (element, class, or ID name).
|
danielebarchiesi@0
|
3794 *
|
danielebarchiesi@0
|
3795 * http://www.w3.org/TR/CSS21/syndata.html#characters shows the syntax for valid
|
danielebarchiesi@0
|
3796 * CSS identifiers (including element names, classes, and IDs in selectors.)
|
danielebarchiesi@0
|
3797 *
|
danielebarchiesi@0
|
3798 * @param $identifier
|
danielebarchiesi@0
|
3799 * The identifier to clean.
|
danielebarchiesi@0
|
3800 * @param $filter
|
danielebarchiesi@0
|
3801 * An array of string replacements to use on the identifier.
|
danielebarchiesi@0
|
3802 *
|
danielebarchiesi@0
|
3803 * @return
|
danielebarchiesi@0
|
3804 * The cleaned identifier.
|
danielebarchiesi@0
|
3805 */
|
danielebarchiesi@0
|
3806 function drupal_clean_css_identifier($identifier, $filter = array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '')) {
|
danielebarchiesi@0
|
3807 // By default, we filter using Drupal's coding standards.
|
danielebarchiesi@0
|
3808 $identifier = strtr($identifier, $filter);
|
danielebarchiesi@0
|
3809
|
danielebarchiesi@0
|
3810 // Valid characters in a CSS identifier are:
|
danielebarchiesi@0
|
3811 // - the hyphen (U+002D)
|
danielebarchiesi@0
|
3812 // - a-z (U+0030 - U+0039)
|
danielebarchiesi@0
|
3813 // - A-Z (U+0041 - U+005A)
|
danielebarchiesi@0
|
3814 // - the underscore (U+005F)
|
danielebarchiesi@0
|
3815 // - 0-9 (U+0061 - U+007A)
|
danielebarchiesi@0
|
3816 // - ISO 10646 characters U+00A1 and higher
|
danielebarchiesi@0
|
3817 // We strip out any character not in the above list.
|
danielebarchiesi@0
|
3818 $identifier = preg_replace('/[^\x{002D}\x{0030}-\x{0039}\x{0041}-\x{005A}\x{005F}\x{0061}-\x{007A}\x{00A1}-\x{FFFF}]/u', '', $identifier);
|
danielebarchiesi@0
|
3819
|
danielebarchiesi@0
|
3820 return $identifier;
|
danielebarchiesi@0
|
3821 }
|
danielebarchiesi@0
|
3822
|
danielebarchiesi@0
|
3823 /**
|
danielebarchiesi@0
|
3824 * Prepares a string for use as a valid class name.
|
danielebarchiesi@0
|
3825 *
|
danielebarchiesi@0
|
3826 * Do not pass one string containing multiple classes as they will be
|
danielebarchiesi@0
|
3827 * incorrectly concatenated with dashes, i.e. "one two" will become "one-two".
|
danielebarchiesi@0
|
3828 *
|
danielebarchiesi@0
|
3829 * @param $class
|
danielebarchiesi@0
|
3830 * The class name to clean.
|
danielebarchiesi@0
|
3831 *
|
danielebarchiesi@0
|
3832 * @return
|
danielebarchiesi@0
|
3833 * The cleaned class name.
|
danielebarchiesi@0
|
3834 */
|
danielebarchiesi@0
|
3835 function drupal_html_class($class) {
|
danielebarchiesi@0
|
3836 return drupal_clean_css_identifier(drupal_strtolower($class));
|
danielebarchiesi@0
|
3837 }
|
danielebarchiesi@0
|
3838
|
danielebarchiesi@0
|
3839 /**
|
danielebarchiesi@0
|
3840 * Prepares a string for use as a valid HTML ID and guarantees uniqueness.
|
danielebarchiesi@0
|
3841 *
|
danielebarchiesi@0
|
3842 * This function ensures that each passed HTML ID value only exists once on the
|
danielebarchiesi@0
|
3843 * page. By tracking the already returned ids, this function enables forms,
|
danielebarchiesi@0
|
3844 * blocks, and other content to be output multiple times on the same page,
|
danielebarchiesi@0
|
3845 * without breaking (X)HTML validation.
|
danielebarchiesi@0
|
3846 *
|
danielebarchiesi@0
|
3847 * For already existing IDs, a counter is appended to the ID string. Therefore,
|
danielebarchiesi@0
|
3848 * JavaScript and CSS code should not rely on any value that was generated by
|
danielebarchiesi@0
|
3849 * this function and instead should rely on manually added CSS classes or
|
danielebarchiesi@0
|
3850 * similarly reliable constructs.
|
danielebarchiesi@0
|
3851 *
|
danielebarchiesi@0
|
3852 * Two consecutive hyphens separate the counter from the original ID. To manage
|
danielebarchiesi@0
|
3853 * uniqueness across multiple Ajax requests on the same page, Ajax requests
|
danielebarchiesi@0
|
3854 * POST an array of all IDs currently present on the page, which are used to
|
danielebarchiesi@0
|
3855 * prime this function's cache upon first invocation.
|
danielebarchiesi@0
|
3856 *
|
danielebarchiesi@0
|
3857 * To allow reverse-parsing of IDs submitted via Ajax, any multiple consecutive
|
danielebarchiesi@0
|
3858 * hyphens in the originally passed $id are replaced with a single hyphen.
|
danielebarchiesi@0
|
3859 *
|
danielebarchiesi@0
|
3860 * @param $id
|
danielebarchiesi@0
|
3861 * The ID to clean.
|
danielebarchiesi@0
|
3862 *
|
danielebarchiesi@0
|
3863 * @return
|
danielebarchiesi@0
|
3864 * The cleaned ID.
|
danielebarchiesi@0
|
3865 */
|
danielebarchiesi@0
|
3866 function drupal_html_id($id) {
|
danielebarchiesi@0
|
3867 // If this is an Ajax request, then content returned by this page request will
|
danielebarchiesi@0
|
3868 // be merged with content already on the base page. The HTML IDs must be
|
danielebarchiesi@0
|
3869 // unique for the fully merged content. Therefore, initialize $seen_ids to
|
danielebarchiesi@0
|
3870 // take into account IDs that are already in use on the base page.
|
danielebarchiesi@0
|
3871 $seen_ids_init = &drupal_static(__FUNCTION__ . ':init');
|
danielebarchiesi@0
|
3872 if (!isset($seen_ids_init)) {
|
danielebarchiesi@0
|
3873 // Ideally, Drupal would provide an API to persist state information about
|
danielebarchiesi@0
|
3874 // prior page requests in the database, and we'd be able to add this
|
danielebarchiesi@0
|
3875 // function's $seen_ids static variable to that state information in order
|
danielebarchiesi@0
|
3876 // to have it properly initialized for this page request. However, no such
|
danielebarchiesi@0
|
3877 // page state API exists, so instead, ajax.js adds all of the in-use HTML
|
danielebarchiesi@0
|
3878 // IDs to the POST data of Ajax submissions. Direct use of $_POST is
|
danielebarchiesi@0
|
3879 // normally not recommended as it could open up security risks, but because
|
danielebarchiesi@0
|
3880 // the raw POST data is cast to a number before being returned by this
|
danielebarchiesi@0
|
3881 // function, this usage is safe.
|
danielebarchiesi@0
|
3882 if (empty($_POST['ajax_html_ids'])) {
|
danielebarchiesi@0
|
3883 $seen_ids_init = array();
|
danielebarchiesi@0
|
3884 }
|
danielebarchiesi@0
|
3885 else {
|
danielebarchiesi@0
|
3886 // This function ensures uniqueness by appending a counter to the base id
|
danielebarchiesi@0
|
3887 // requested by the calling function after the first occurrence of that
|
danielebarchiesi@0
|
3888 // requested id. $_POST['ajax_html_ids'] contains the ids as they were
|
danielebarchiesi@0
|
3889 // returned by this function, potentially with the appended counter, so
|
danielebarchiesi@0
|
3890 // we parse that to reconstruct the $seen_ids array.
|
danielebarchiesi@0
|
3891 if (isset($_POST['ajax_html_ids'][0]) && strpos($_POST['ajax_html_ids'][0], ',') === FALSE) {
|
danielebarchiesi@0
|
3892 $ajax_html_ids = $_POST['ajax_html_ids'];
|
danielebarchiesi@0
|
3893 }
|
danielebarchiesi@0
|
3894 else {
|
danielebarchiesi@0
|
3895 // jquery.form.js may send the server a comma-separated string as the
|
danielebarchiesi@0
|
3896 // first element of an array (see http://drupal.org/node/1575060), so
|
danielebarchiesi@0
|
3897 // we need to convert it to an array in that case.
|
danielebarchiesi@0
|
3898 $ajax_html_ids = explode(',', $_POST['ajax_html_ids'][0]);
|
danielebarchiesi@0
|
3899 }
|
danielebarchiesi@0
|
3900 foreach ($ajax_html_ids as $seen_id) {
|
danielebarchiesi@0
|
3901 // We rely on '--' being used solely for separating a base id from the
|
danielebarchiesi@0
|
3902 // counter, which this function ensures when returning an id.
|
danielebarchiesi@0
|
3903 $parts = explode('--', $seen_id, 2);
|
danielebarchiesi@0
|
3904 if (!empty($parts[1]) && is_numeric($parts[1])) {
|
danielebarchiesi@0
|
3905 list($seen_id, $i) = $parts;
|
danielebarchiesi@0
|
3906 }
|
danielebarchiesi@0
|
3907 else {
|
danielebarchiesi@0
|
3908 $i = 1;
|
danielebarchiesi@0
|
3909 }
|
danielebarchiesi@0
|
3910 if (!isset($seen_ids_init[$seen_id]) || ($i > $seen_ids_init[$seen_id])) {
|
danielebarchiesi@0
|
3911 $seen_ids_init[$seen_id] = $i;
|
danielebarchiesi@0
|
3912 }
|
danielebarchiesi@0
|
3913 }
|
danielebarchiesi@0
|
3914 }
|
danielebarchiesi@0
|
3915 }
|
danielebarchiesi@0
|
3916 $seen_ids = &drupal_static(__FUNCTION__, $seen_ids_init);
|
danielebarchiesi@0
|
3917
|
danielebarchiesi@0
|
3918 $id = strtr(drupal_strtolower($id), array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
|
danielebarchiesi@0
|
3919
|
danielebarchiesi@0
|
3920 // As defined in http://www.w3.org/TR/html4/types.html#type-name, HTML IDs can
|
danielebarchiesi@0
|
3921 // only contain letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
|
danielebarchiesi@0
|
3922 // colons (":"), and periods ("."). We strip out any character not in that
|
danielebarchiesi@0
|
3923 // list. Note that the CSS spec doesn't allow colons or periods in identifiers
|
danielebarchiesi@0
|
3924 // (http://www.w3.org/TR/CSS21/syndata.html#characters), so we strip those two
|
danielebarchiesi@0
|
3925 // characters as well.
|
danielebarchiesi@0
|
3926 $id = preg_replace('/[^A-Za-z0-9\-_]/', '', $id);
|
danielebarchiesi@0
|
3927
|
danielebarchiesi@0
|
3928 // Removing multiple consecutive hyphens.
|
danielebarchiesi@0
|
3929 $id = preg_replace('/\-+/', '-', $id);
|
danielebarchiesi@0
|
3930 // Ensure IDs are unique by appending a counter after the first occurrence.
|
danielebarchiesi@0
|
3931 // The counter needs to be appended with a delimiter that does not exist in
|
danielebarchiesi@0
|
3932 // the base ID. Requiring a unique delimiter helps ensure that we really do
|
danielebarchiesi@0
|
3933 // return unique IDs and also helps us re-create the $seen_ids array during
|
danielebarchiesi@0
|
3934 // Ajax requests.
|
danielebarchiesi@0
|
3935 if (isset($seen_ids[$id])) {
|
danielebarchiesi@0
|
3936 $id = $id . '--' . ++$seen_ids[$id];
|
danielebarchiesi@0
|
3937 }
|
danielebarchiesi@0
|
3938 else {
|
danielebarchiesi@0
|
3939 $seen_ids[$id] = 1;
|
danielebarchiesi@0
|
3940 }
|
danielebarchiesi@0
|
3941
|
danielebarchiesi@0
|
3942 return $id;
|
danielebarchiesi@0
|
3943 }
|
danielebarchiesi@0
|
3944
|
danielebarchiesi@0
|
3945 /**
|
danielebarchiesi@0
|
3946 * Provides a standard HTML class name that identifies a page region.
|
danielebarchiesi@0
|
3947 *
|
danielebarchiesi@0
|
3948 * It is recommended that template preprocess functions apply this class to any
|
danielebarchiesi@0
|
3949 * page region that is output by the theme (Drupal core already handles this in
|
danielebarchiesi@0
|
3950 * the standard template preprocess implementation). Standardizing the class
|
danielebarchiesi@0
|
3951 * names in this way allows modules to implement certain features, such as
|
danielebarchiesi@0
|
3952 * drag-and-drop or dynamic Ajax loading, in a theme-independent way.
|
danielebarchiesi@0
|
3953 *
|
danielebarchiesi@0
|
3954 * @param $region
|
danielebarchiesi@0
|
3955 * The name of the page region (for example, 'page_top' or 'content').
|
danielebarchiesi@0
|
3956 *
|
danielebarchiesi@0
|
3957 * @return
|
danielebarchiesi@0
|
3958 * An HTML class that identifies the region (for example, 'region-page-top'
|
danielebarchiesi@0
|
3959 * or 'region-content').
|
danielebarchiesi@0
|
3960 *
|
danielebarchiesi@0
|
3961 * @see template_preprocess_region()
|
danielebarchiesi@0
|
3962 */
|
danielebarchiesi@0
|
3963 function drupal_region_class($region) {
|
danielebarchiesi@0
|
3964 return drupal_html_class("region-$region");
|
danielebarchiesi@0
|
3965 }
|
danielebarchiesi@0
|
3966
|
danielebarchiesi@0
|
3967 /**
|
danielebarchiesi@0
|
3968 * Adds a JavaScript file, setting, or inline code to the page.
|
danielebarchiesi@0
|
3969 *
|
danielebarchiesi@0
|
3970 * The behavior of this function depends on the parameters it is called with.
|
danielebarchiesi@0
|
3971 * Generally, it handles the addition of JavaScript to the page, either as
|
danielebarchiesi@0
|
3972 * reference to an existing file or as inline code. The following actions can be
|
danielebarchiesi@0
|
3973 * performed using this function:
|
danielebarchiesi@0
|
3974 * - Add a file ('file'): Adds a reference to a JavaScript file to the page.
|
danielebarchiesi@0
|
3975 * - Add inline JavaScript code ('inline'): Executes a piece of JavaScript code
|
danielebarchiesi@0
|
3976 * on the current page by placing the code directly in the page (for example,
|
danielebarchiesi@0
|
3977 * to tell the user that a new message arrived, by opening a pop up, alert
|
danielebarchiesi@0
|
3978 * box, etc.). This should only be used for JavaScript that cannot be executed
|
danielebarchiesi@0
|
3979 * from a file. When adding inline code, make sure that you are not relying on
|
danielebarchiesi@0
|
3980 * $() being the jQuery function. Wrap your code in
|
danielebarchiesi@0
|
3981 * @code (function ($) {... })(jQuery); @endcode
|
danielebarchiesi@0
|
3982 * or use jQuery() instead of $().
|
danielebarchiesi@0
|
3983 * - Add external JavaScript ('external'): Allows the inclusion of external
|
danielebarchiesi@0
|
3984 * JavaScript files that are not hosted on the local server. Note that these
|
danielebarchiesi@0
|
3985 * external JavaScript references do not get aggregated when preprocessing is
|
danielebarchiesi@0
|
3986 * on.
|
danielebarchiesi@0
|
3987 * - Add settings ('setting'): Adds settings to Drupal's global storage of
|
danielebarchiesi@0
|
3988 * JavaScript settings. Per-page settings are required by some modules to
|
danielebarchiesi@0
|
3989 * function properly. All settings will be accessible at Drupal.settings.
|
danielebarchiesi@0
|
3990 *
|
danielebarchiesi@0
|
3991 * Examples:
|
danielebarchiesi@0
|
3992 * @code
|
danielebarchiesi@0
|
3993 * drupal_add_js('misc/collapse.js');
|
danielebarchiesi@0
|
3994 * drupal_add_js('misc/collapse.js', 'file');
|
danielebarchiesi@0
|
3995 * drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
|
danielebarchiesi@0
|
3996 * drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });',
|
danielebarchiesi@0
|
3997 * array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
|
danielebarchiesi@0
|
3998 * );
|
danielebarchiesi@0
|
3999 * drupal_add_js('http://example.com/example.js', 'external');
|
danielebarchiesi@0
|
4000 * drupal_add_js(array('myModule' => array('key' => 'value')), 'setting');
|
danielebarchiesi@0
|
4001 * @endcode
|
danielebarchiesi@0
|
4002 *
|
danielebarchiesi@0
|
4003 * Calling drupal_static_reset('drupal_add_js') will clear all JavaScript added
|
danielebarchiesi@0
|
4004 * so far.
|
danielebarchiesi@0
|
4005 *
|
danielebarchiesi@0
|
4006 * If JavaScript aggregation is enabled, all JavaScript files added with
|
danielebarchiesi@0
|
4007 * $options['preprocess'] set to TRUE will be merged into one aggregate file.
|
danielebarchiesi@0
|
4008 * Preprocessed inline JavaScript will not be aggregated into this single file.
|
danielebarchiesi@0
|
4009 * Externally hosted JavaScripts are never aggregated.
|
danielebarchiesi@0
|
4010 *
|
danielebarchiesi@0
|
4011 * The reason for aggregating the files is outlined quite thoroughly here:
|
danielebarchiesi@0
|
4012 * http://www.die.net/musings/page_load_time/ "Load fewer external objects. Due
|
danielebarchiesi@0
|
4013 * to request overhead, one bigger file just loads faster than two smaller ones
|
danielebarchiesi@0
|
4014 * half its size."
|
danielebarchiesi@0
|
4015 *
|
danielebarchiesi@0
|
4016 * $options['preprocess'] should be only set to TRUE when a file is required for
|
danielebarchiesi@0
|
4017 * all typical visitors and most pages of a site. It is critical that all
|
danielebarchiesi@0
|
4018 * preprocessed files are added unconditionally on every page, even if the
|
danielebarchiesi@0
|
4019 * files are not needed on a page. This is normally done by calling
|
danielebarchiesi@0
|
4020 * drupal_add_js() in a hook_init() implementation.
|
danielebarchiesi@0
|
4021 *
|
danielebarchiesi@0
|
4022 * Non-preprocessed files should only be added to the page when they are
|
danielebarchiesi@0
|
4023 * actually needed.
|
danielebarchiesi@0
|
4024 *
|
danielebarchiesi@0
|
4025 * @param $data
|
danielebarchiesi@0
|
4026 * (optional) If given, the value depends on the $options parameter, or
|
danielebarchiesi@0
|
4027 * $options['type'] if $options is passed as an associative array:
|
danielebarchiesi@0
|
4028 * - 'file': Path to the file relative to base_path().
|
danielebarchiesi@0
|
4029 * - 'inline': The JavaScript code that should be placed in the given scope.
|
danielebarchiesi@0
|
4030 * - 'external': The absolute path to an external JavaScript file that is not
|
danielebarchiesi@0
|
4031 * hosted on the local server. These files will not be aggregated if
|
danielebarchiesi@0
|
4032 * JavaScript aggregation is enabled.
|
danielebarchiesi@0
|
4033 * - 'setting': An associative array with configuration options. The array is
|
danielebarchiesi@0
|
4034 * merged directly into Drupal.settings. All modules should wrap their
|
danielebarchiesi@0
|
4035 * actual configuration settings in another variable to prevent conflicts in
|
danielebarchiesi@0
|
4036 * the Drupal.settings namespace. Items added with a string key will replace
|
danielebarchiesi@0
|
4037 * existing settings with that key; items with numeric array keys will be
|
danielebarchiesi@0
|
4038 * added to the existing settings array.
|
danielebarchiesi@0
|
4039 * @param $options
|
danielebarchiesi@0
|
4040 * (optional) A string defining the type of JavaScript that is being added in
|
danielebarchiesi@0
|
4041 * the $data parameter ('file'/'setting'/'inline'/'external'), or an
|
danielebarchiesi@0
|
4042 * associative array. JavaScript settings should always pass the string
|
danielebarchiesi@0
|
4043 * 'setting' only. Other types can have the following elements in the array:
|
danielebarchiesi@0
|
4044 * - type: The type of JavaScript that is to be added to the page. Allowed
|
danielebarchiesi@0
|
4045 * values are 'file', 'inline', 'external' or 'setting'. Defaults
|
danielebarchiesi@0
|
4046 * to 'file'.
|
danielebarchiesi@0
|
4047 * - scope: The location in which you want to place the script. Possible
|
danielebarchiesi@0
|
4048 * values are 'header' or 'footer'. If your theme implements different
|
danielebarchiesi@0
|
4049 * regions, you can also use these. Defaults to 'header'.
|
danielebarchiesi@0
|
4050 * - group: A number identifying the group in which to add the JavaScript.
|
danielebarchiesi@0
|
4051 * Available constants are:
|
danielebarchiesi@0
|
4052 * - JS_LIBRARY: Any libraries, settings, or jQuery plugins.
|
danielebarchiesi@0
|
4053 * - JS_DEFAULT: Any module-layer JavaScript.
|
danielebarchiesi@0
|
4054 * - JS_THEME: Any theme-layer JavaScript.
|
danielebarchiesi@0
|
4055 * The group number serves as a weight: JavaScript within a lower weight
|
danielebarchiesi@0
|
4056 * group is presented on the page before JavaScript within a higher weight
|
danielebarchiesi@0
|
4057 * group.
|
danielebarchiesi@0
|
4058 * - every_page: For optimal front-end performance when aggregation is
|
danielebarchiesi@0
|
4059 * enabled, this should be set to TRUE if the JavaScript is present on every
|
danielebarchiesi@0
|
4060 * page of the website for users for whom it is present at all. This
|
danielebarchiesi@0
|
4061 * defaults to FALSE. It is set to TRUE for JavaScript files that are added
|
danielebarchiesi@0
|
4062 * via module and theme .info files. Modules that add JavaScript within
|
danielebarchiesi@0
|
4063 * hook_init() implementations, or from other code that ensures that the
|
danielebarchiesi@0
|
4064 * JavaScript is added to all website pages, should also set this flag to
|
danielebarchiesi@0
|
4065 * TRUE. All JavaScript files within the same group and that have the
|
danielebarchiesi@0
|
4066 * 'every_page' flag set to TRUE and do not have 'preprocess' set to FALSE
|
danielebarchiesi@0
|
4067 * are aggregated together into a single aggregate file, and that aggregate
|
danielebarchiesi@0
|
4068 * file can be reused across a user's entire site visit, leading to faster
|
danielebarchiesi@0
|
4069 * navigation between pages. However, JavaScript that is only needed on
|
danielebarchiesi@0
|
4070 * pages less frequently visited, can be added by code that only runs for
|
danielebarchiesi@0
|
4071 * those particular pages, and that code should not set the 'every_page'
|
danielebarchiesi@0
|
4072 * flag. This minimizes the size of the aggregate file that the user needs
|
danielebarchiesi@0
|
4073 * to download when first visiting the website. JavaScript without the
|
danielebarchiesi@0
|
4074 * 'every_page' flag is aggregated into a separate aggregate file. This
|
danielebarchiesi@0
|
4075 * other aggregate file is likely to change from page to page, and each new
|
danielebarchiesi@0
|
4076 * aggregate file needs to be downloaded when first encountered, so it
|
danielebarchiesi@0
|
4077 * should be kept relatively small by ensuring that most commonly needed
|
danielebarchiesi@0
|
4078 * JavaScript is added to every page.
|
danielebarchiesi@0
|
4079 * - weight: A number defining the order in which the JavaScript is added to
|
danielebarchiesi@0
|
4080 * the page relative to other JavaScript with the same 'scope', 'group',
|
danielebarchiesi@0
|
4081 * and 'every_page' value. In some cases, the order in which the JavaScript
|
danielebarchiesi@0
|
4082 * is presented on the page is very important. jQuery, for example, must be
|
danielebarchiesi@0
|
4083 * added to the page before any jQuery code is run, so jquery.js uses the
|
danielebarchiesi@0
|
4084 * JS_LIBRARY group and a weight of -20, jquery.once.js (a library drupal.js
|
danielebarchiesi@0
|
4085 * depends on) uses the JS_LIBRARY group and a weight of -19, drupal.js uses
|
danielebarchiesi@0
|
4086 * the JS_LIBRARY group and a weight of -1, other libraries use the
|
danielebarchiesi@0
|
4087 * JS_LIBRARY group and a weight of 0 or higher, and all other scripts use
|
danielebarchiesi@0
|
4088 * one of the other group constants. The exact ordering of JavaScript is as
|
danielebarchiesi@0
|
4089 * follows:
|
danielebarchiesi@0
|
4090 * - First by scope, with 'header' first, 'footer' last, and any other
|
danielebarchiesi@0
|
4091 * scopes provided by a custom theme coming in between, as determined by
|
danielebarchiesi@0
|
4092 * the theme.
|
danielebarchiesi@0
|
4093 * - Then by group.
|
danielebarchiesi@0
|
4094 * - Then by the 'every_page' flag, with TRUE coming before FALSE.
|
danielebarchiesi@0
|
4095 * - Then by weight.
|
danielebarchiesi@0
|
4096 * - Then by the order in which the JavaScript was added. For example, all
|
danielebarchiesi@0
|
4097 * else being the same, JavaScript added by a call to drupal_add_js() that
|
danielebarchiesi@0
|
4098 * happened later in the page request gets added to the page after one for
|
danielebarchiesi@0
|
4099 * which drupal_add_js() happened earlier in the page request.
|
danielebarchiesi@0
|
4100 * - defer: If set to TRUE, the defer attribute is set on the <script>
|
danielebarchiesi@0
|
4101 * tag. Defaults to FALSE.
|
danielebarchiesi@0
|
4102 * - cache: If set to FALSE, the JavaScript file is loaded anew on every page
|
danielebarchiesi@0
|
4103 * call; in other words, it is not cached. Used only when 'type' references
|
danielebarchiesi@0
|
4104 * a JavaScript file. Defaults to TRUE.
|
danielebarchiesi@0
|
4105 * - preprocess: If TRUE and JavaScript aggregation is enabled, the script
|
danielebarchiesi@0
|
4106 * file will be aggregated. Defaults to TRUE.
|
danielebarchiesi@0
|
4107 *
|
danielebarchiesi@0
|
4108 * @return
|
danielebarchiesi@0
|
4109 * The current array of JavaScript files, settings, and in-line code,
|
danielebarchiesi@0
|
4110 * including Drupal defaults, anything previously added with calls to
|
danielebarchiesi@0
|
4111 * drupal_add_js(), and this function call's additions.
|
danielebarchiesi@0
|
4112 *
|
danielebarchiesi@0
|
4113 * @see drupal_get_js()
|
danielebarchiesi@0
|
4114 */
|
danielebarchiesi@0
|
4115 function drupal_add_js($data = NULL, $options = NULL) {
|
danielebarchiesi@0
|
4116 $javascript = &drupal_static(__FUNCTION__, array());
|
danielebarchiesi@0
|
4117
|
danielebarchiesi@0
|
4118 // Construct the options, taking the defaults into consideration.
|
danielebarchiesi@0
|
4119 if (isset($options)) {
|
danielebarchiesi@0
|
4120 if (!is_array($options)) {
|
danielebarchiesi@0
|
4121 $options = array('type' => $options);
|
danielebarchiesi@0
|
4122 }
|
danielebarchiesi@0
|
4123 }
|
danielebarchiesi@0
|
4124 else {
|
danielebarchiesi@0
|
4125 $options = array();
|
danielebarchiesi@0
|
4126 }
|
danielebarchiesi@0
|
4127 $options += drupal_js_defaults($data);
|
danielebarchiesi@0
|
4128
|
danielebarchiesi@0
|
4129 // Preprocess can only be set if caching is enabled.
|
danielebarchiesi@0
|
4130 $options['preprocess'] = $options['cache'] ? $options['preprocess'] : FALSE;
|
danielebarchiesi@0
|
4131
|
danielebarchiesi@0
|
4132 // Tweak the weight so that files of the same weight are included in the
|
danielebarchiesi@0
|
4133 // order of the calls to drupal_add_js().
|
danielebarchiesi@0
|
4134 $options['weight'] += count($javascript) / 1000;
|
danielebarchiesi@0
|
4135
|
danielebarchiesi@0
|
4136 if (isset($data)) {
|
danielebarchiesi@0
|
4137 // Add jquery.js and drupal.js, as well as the basePath setting, the
|
danielebarchiesi@0
|
4138 // first time a JavaScript file is added.
|
danielebarchiesi@0
|
4139 if (empty($javascript)) {
|
danielebarchiesi@0
|
4140 // url() generates the prefix using hook_url_outbound_alter(). Instead of
|
danielebarchiesi@0
|
4141 // running the hook_url_outbound_alter() again here, extract the prefix
|
danielebarchiesi@0
|
4142 // from url().
|
danielebarchiesi@0
|
4143 url('', array('prefix' => &$prefix));
|
danielebarchiesi@0
|
4144 $javascript = array(
|
danielebarchiesi@0
|
4145 'settings' => array(
|
danielebarchiesi@0
|
4146 'data' => array(
|
danielebarchiesi@0
|
4147 array('basePath' => base_path()),
|
danielebarchiesi@0
|
4148 array('pathPrefix' => empty($prefix) ? '' : $prefix),
|
danielebarchiesi@0
|
4149 ),
|
danielebarchiesi@0
|
4150 'type' => 'setting',
|
danielebarchiesi@0
|
4151 'scope' => 'header',
|
danielebarchiesi@0
|
4152 'group' => JS_LIBRARY,
|
danielebarchiesi@0
|
4153 'every_page' => TRUE,
|
danielebarchiesi@0
|
4154 'weight' => 0,
|
danielebarchiesi@0
|
4155 ),
|
danielebarchiesi@0
|
4156 'misc/drupal.js' => array(
|
danielebarchiesi@0
|
4157 'data' => 'misc/drupal.js',
|
danielebarchiesi@0
|
4158 'type' => 'file',
|
danielebarchiesi@0
|
4159 'scope' => 'header',
|
danielebarchiesi@0
|
4160 'group' => JS_LIBRARY,
|
danielebarchiesi@0
|
4161 'every_page' => TRUE,
|
danielebarchiesi@0
|
4162 'weight' => -1,
|
danielebarchiesi@0
|
4163 'preprocess' => TRUE,
|
danielebarchiesi@0
|
4164 'cache' => TRUE,
|
danielebarchiesi@0
|
4165 'defer' => FALSE,
|
danielebarchiesi@0
|
4166 ),
|
danielebarchiesi@0
|
4167 );
|
danielebarchiesi@0
|
4168 // Register all required libraries.
|
danielebarchiesi@0
|
4169 drupal_add_library('system', 'jquery', TRUE);
|
danielebarchiesi@0
|
4170 drupal_add_library('system', 'jquery.once', TRUE);
|
danielebarchiesi@0
|
4171 }
|
danielebarchiesi@0
|
4172
|
danielebarchiesi@0
|
4173 switch ($options['type']) {
|
danielebarchiesi@0
|
4174 case 'setting':
|
danielebarchiesi@0
|
4175 // All JavaScript settings are placed in the header of the page with
|
danielebarchiesi@0
|
4176 // the library weight so that inline scripts appear afterwards.
|
danielebarchiesi@0
|
4177 $javascript['settings']['data'][] = $data;
|
danielebarchiesi@0
|
4178 break;
|
danielebarchiesi@0
|
4179
|
danielebarchiesi@0
|
4180 case 'inline':
|
danielebarchiesi@0
|
4181 $javascript[] = $options;
|
danielebarchiesi@0
|
4182 break;
|
danielebarchiesi@0
|
4183
|
danielebarchiesi@0
|
4184 default: // 'file' and 'external'
|
danielebarchiesi@0
|
4185 // Local and external files must keep their name as the associative key
|
danielebarchiesi@0
|
4186 // so the same JavaScript file is not added twice.
|
danielebarchiesi@0
|
4187 $javascript[$options['data']] = $options;
|
danielebarchiesi@0
|
4188 }
|
danielebarchiesi@0
|
4189 }
|
danielebarchiesi@0
|
4190 return $javascript;
|
danielebarchiesi@0
|
4191 }
|
danielebarchiesi@0
|
4192
|
danielebarchiesi@0
|
4193 /**
|
danielebarchiesi@0
|
4194 * Constructs an array of the defaults that are used for JavaScript items.
|
danielebarchiesi@0
|
4195 *
|
danielebarchiesi@0
|
4196 * @param $data
|
danielebarchiesi@0
|
4197 * (optional) The default data parameter for the JavaScript item array.
|
danielebarchiesi@0
|
4198 *
|
danielebarchiesi@0
|
4199 * @see drupal_get_js()
|
danielebarchiesi@0
|
4200 * @see drupal_add_js()
|
danielebarchiesi@0
|
4201 */
|
danielebarchiesi@0
|
4202 function drupal_js_defaults($data = NULL) {
|
danielebarchiesi@0
|
4203 return array(
|
danielebarchiesi@0
|
4204 'type' => 'file',
|
danielebarchiesi@0
|
4205 'group' => JS_DEFAULT,
|
danielebarchiesi@0
|
4206 'every_page' => FALSE,
|
danielebarchiesi@0
|
4207 'weight' => 0,
|
danielebarchiesi@0
|
4208 'scope' => 'header',
|
danielebarchiesi@0
|
4209 'cache' => TRUE,
|
danielebarchiesi@0
|
4210 'defer' => FALSE,
|
danielebarchiesi@0
|
4211 'preprocess' => TRUE,
|
danielebarchiesi@0
|
4212 'version' => NULL,
|
danielebarchiesi@0
|
4213 'data' => $data,
|
danielebarchiesi@0
|
4214 );
|
danielebarchiesi@0
|
4215 }
|
danielebarchiesi@0
|
4216
|
danielebarchiesi@0
|
4217 /**
|
danielebarchiesi@0
|
4218 * Returns a themed presentation of all JavaScript code for the current page.
|
danielebarchiesi@0
|
4219 *
|
danielebarchiesi@0
|
4220 * References to JavaScript files are placed in a certain order: first, all
|
danielebarchiesi@0
|
4221 * 'core' files, then all 'module' and finally all 'theme' JavaScript files
|
danielebarchiesi@0
|
4222 * are added to the page. Then, all settings are output, followed by 'inline'
|
danielebarchiesi@0
|
4223 * JavaScript code. If running update.php, all preprocessing is disabled.
|
danielebarchiesi@0
|
4224 *
|
danielebarchiesi@0
|
4225 * Note that hook_js_alter(&$javascript) is called during this function call
|
danielebarchiesi@0
|
4226 * to allow alterations of the JavaScript during its presentation. Calls to
|
danielebarchiesi@0
|
4227 * drupal_add_js() from hook_js_alter() will not be added to the output
|
danielebarchiesi@0
|
4228 * presentation. The correct way to add JavaScript during hook_js_alter()
|
danielebarchiesi@0
|
4229 * is to add another element to the $javascript array, deriving from
|
danielebarchiesi@0
|
4230 * drupal_js_defaults(). See locale_js_alter() for an example of this.
|
danielebarchiesi@0
|
4231 *
|
danielebarchiesi@0
|
4232 * @param $scope
|
danielebarchiesi@0
|
4233 * (optional) The scope for which the JavaScript rules should be returned.
|
danielebarchiesi@0
|
4234 * Defaults to 'header'.
|
danielebarchiesi@0
|
4235 * @param $javascript
|
danielebarchiesi@0
|
4236 * (optional) An array with all JavaScript code. Defaults to the default
|
danielebarchiesi@0
|
4237 * JavaScript array for the given scope.
|
danielebarchiesi@0
|
4238 * @param $skip_alter
|
danielebarchiesi@0
|
4239 * (optional) If set to TRUE, this function skips calling drupal_alter() on
|
danielebarchiesi@0
|
4240 * $javascript, useful when the calling function passes a $javascript array
|
danielebarchiesi@0
|
4241 * that has already been altered.
|
danielebarchiesi@0
|
4242 *
|
danielebarchiesi@0
|
4243 * @return
|
danielebarchiesi@0
|
4244 * All JavaScript code segments and includes for the scope as HTML tags.
|
danielebarchiesi@0
|
4245 *
|
danielebarchiesi@0
|
4246 * @see drupal_add_js()
|
danielebarchiesi@0
|
4247 * @see locale_js_alter()
|
danielebarchiesi@0
|
4248 * @see drupal_js_defaults()
|
danielebarchiesi@0
|
4249 */
|
danielebarchiesi@0
|
4250 function drupal_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALSE) {
|
danielebarchiesi@0
|
4251 if (!isset($javascript)) {
|
danielebarchiesi@0
|
4252 $javascript = drupal_add_js();
|
danielebarchiesi@0
|
4253 }
|
danielebarchiesi@0
|
4254 if (empty($javascript)) {
|
danielebarchiesi@0
|
4255 return '';
|
danielebarchiesi@0
|
4256 }
|
danielebarchiesi@0
|
4257
|
danielebarchiesi@0
|
4258 // Allow modules to alter the JavaScript.
|
danielebarchiesi@0
|
4259 if (!$skip_alter) {
|
danielebarchiesi@0
|
4260 drupal_alter('js', $javascript);
|
danielebarchiesi@0
|
4261 }
|
danielebarchiesi@0
|
4262
|
danielebarchiesi@0
|
4263 // Filter out elements of the given scope.
|
danielebarchiesi@0
|
4264 $items = array();
|
danielebarchiesi@0
|
4265 foreach ($javascript as $key => $item) {
|
danielebarchiesi@0
|
4266 if ($item['scope'] == $scope) {
|
danielebarchiesi@0
|
4267 $items[$key] = $item;
|
danielebarchiesi@0
|
4268 }
|
danielebarchiesi@0
|
4269 }
|
danielebarchiesi@0
|
4270
|
danielebarchiesi@0
|
4271 $output = '';
|
danielebarchiesi@0
|
4272 // The index counter is used to keep aggregated and non-aggregated files in
|
danielebarchiesi@0
|
4273 // order by weight.
|
danielebarchiesi@0
|
4274 $index = 1;
|
danielebarchiesi@0
|
4275 $processed = array();
|
danielebarchiesi@0
|
4276 $files = array();
|
danielebarchiesi@0
|
4277 $preprocess_js = (variable_get('preprocess_js', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
|
danielebarchiesi@0
|
4278
|
danielebarchiesi@0
|
4279 // A dummy query-string is added to filenames, to gain control over
|
danielebarchiesi@0
|
4280 // browser-caching. The string changes on every update or full cache
|
danielebarchiesi@0
|
4281 // flush, forcing browsers to load a new copy of the files, as the
|
danielebarchiesi@0
|
4282 // URL changed. Files that should not be cached (see drupal_add_js())
|
danielebarchiesi@0
|
4283 // get REQUEST_TIME as query-string instead, to enforce reload on every
|
danielebarchiesi@0
|
4284 // page request.
|
danielebarchiesi@0
|
4285 $default_query_string = variable_get('css_js_query_string', '0');
|
danielebarchiesi@0
|
4286
|
danielebarchiesi@0
|
4287 // For inline JavaScript to validate as XHTML, all JavaScript containing
|
danielebarchiesi@0
|
4288 // XHTML needs to be wrapped in CDATA. To make that backwards compatible
|
danielebarchiesi@0
|
4289 // with HTML 4, we need to comment out the CDATA-tag.
|
danielebarchiesi@0
|
4290 $embed_prefix = "\n<!--//--><![CDATA[//><!--\n";
|
danielebarchiesi@0
|
4291 $embed_suffix = "\n//--><!]]>\n";
|
danielebarchiesi@0
|
4292
|
danielebarchiesi@0
|
4293 // Since JavaScript may look for arguments in the URL and act on them, some
|
danielebarchiesi@0
|
4294 // third-party code might require the use of a different query string.
|
danielebarchiesi@0
|
4295 $js_version_string = variable_get('drupal_js_version_query_string', 'v=');
|
danielebarchiesi@0
|
4296
|
danielebarchiesi@0
|
4297 // Sort the JavaScript so that it appears in the correct order.
|
danielebarchiesi@0
|
4298 uasort($items, 'drupal_sort_css_js');
|
danielebarchiesi@0
|
4299
|
danielebarchiesi@0
|
4300 // Provide the page with information about the individual JavaScript files
|
danielebarchiesi@0
|
4301 // used, information not otherwise available when aggregation is enabled.
|
danielebarchiesi@0
|
4302 $setting['ajaxPageState']['js'] = array_fill_keys(array_keys($items), 1);
|
danielebarchiesi@0
|
4303 unset($setting['ajaxPageState']['js']['settings']);
|
danielebarchiesi@0
|
4304 drupal_add_js($setting, 'setting');
|
danielebarchiesi@0
|
4305
|
danielebarchiesi@0
|
4306 // If we're outputting the header scope, then this might be the final time
|
danielebarchiesi@0
|
4307 // that drupal_get_js() is running, so add the setting to this output as well
|
danielebarchiesi@0
|
4308 // as to the drupal_add_js() cache. If $items['settings'] doesn't exist, it's
|
danielebarchiesi@0
|
4309 // because drupal_get_js() was intentionally passed a $javascript argument
|
danielebarchiesi@0
|
4310 // stripped off settings, potentially in order to override how settings get
|
danielebarchiesi@0
|
4311 // output, so in this case, do not add the setting to this output.
|
danielebarchiesi@0
|
4312 if ($scope == 'header' && isset($items['settings'])) {
|
danielebarchiesi@0
|
4313 $items['settings']['data'][] = $setting;
|
danielebarchiesi@0
|
4314 }
|
danielebarchiesi@0
|
4315
|
danielebarchiesi@0
|
4316 // Loop through the JavaScript to construct the rendered output.
|
danielebarchiesi@0
|
4317 $element = array(
|
danielebarchiesi@0
|
4318 '#tag' => 'script',
|
danielebarchiesi@0
|
4319 '#value' => '',
|
danielebarchiesi@0
|
4320 '#attributes' => array(
|
danielebarchiesi@0
|
4321 'type' => 'text/javascript',
|
danielebarchiesi@0
|
4322 ),
|
danielebarchiesi@0
|
4323 );
|
danielebarchiesi@0
|
4324 foreach ($items as $item) {
|
danielebarchiesi@0
|
4325 $query_string = empty($item['version']) ? $default_query_string : $js_version_string . $item['version'];
|
danielebarchiesi@0
|
4326
|
danielebarchiesi@0
|
4327 switch ($item['type']) {
|
danielebarchiesi@0
|
4328 case 'setting':
|
danielebarchiesi@0
|
4329 $js_element = $element;
|
danielebarchiesi@0
|
4330 $js_element['#value_prefix'] = $embed_prefix;
|
danielebarchiesi@0
|
4331 $js_element['#value'] = 'jQuery.extend(Drupal.settings, ' . drupal_json_encode(drupal_array_merge_deep_array($item['data'])) . ");";
|
danielebarchiesi@0
|
4332 $js_element['#value_suffix'] = $embed_suffix;
|
danielebarchiesi@0
|
4333 $output .= theme('html_tag', array('element' => $js_element));
|
danielebarchiesi@0
|
4334 break;
|
danielebarchiesi@0
|
4335
|
danielebarchiesi@0
|
4336 case 'inline':
|
danielebarchiesi@0
|
4337 $js_element = $element;
|
danielebarchiesi@0
|
4338 if ($item['defer']) {
|
danielebarchiesi@0
|
4339 $js_element['#attributes']['defer'] = 'defer';
|
danielebarchiesi@0
|
4340 }
|
danielebarchiesi@0
|
4341 $js_element['#value_prefix'] = $embed_prefix;
|
danielebarchiesi@0
|
4342 $js_element['#value'] = $item['data'];
|
danielebarchiesi@0
|
4343 $js_element['#value_suffix'] = $embed_suffix;
|
danielebarchiesi@0
|
4344 $processed[$index++] = theme('html_tag', array('element' => $js_element));
|
danielebarchiesi@0
|
4345 break;
|
danielebarchiesi@0
|
4346
|
danielebarchiesi@0
|
4347 case 'file':
|
danielebarchiesi@0
|
4348 $js_element = $element;
|
danielebarchiesi@0
|
4349 if (!$item['preprocess'] || !$preprocess_js) {
|
danielebarchiesi@0
|
4350 if ($item['defer']) {
|
danielebarchiesi@0
|
4351 $js_element['#attributes']['defer'] = 'defer';
|
danielebarchiesi@0
|
4352 }
|
danielebarchiesi@0
|
4353 $query_string_separator = (strpos($item['data'], '?') !== FALSE) ? '&' : '?';
|
danielebarchiesi@0
|
4354 $js_element['#attributes']['src'] = file_create_url($item['data']) . $query_string_separator . ($item['cache'] ? $query_string : REQUEST_TIME);
|
danielebarchiesi@0
|
4355 $processed[$index++] = theme('html_tag', array('element' => $js_element));
|
danielebarchiesi@0
|
4356 }
|
danielebarchiesi@0
|
4357 else {
|
danielebarchiesi@0
|
4358 // By increasing the index for each aggregated file, we maintain
|
danielebarchiesi@0
|
4359 // the relative ordering of JS by weight. We also set the key such
|
danielebarchiesi@0
|
4360 // that groups are split by items sharing the same 'group' value and
|
danielebarchiesi@0
|
4361 // 'every_page' flag. While this potentially results in more aggregate
|
danielebarchiesi@0
|
4362 // files, it helps make each one more reusable across a site visit,
|
danielebarchiesi@0
|
4363 // leading to better front-end performance of a website as a whole.
|
danielebarchiesi@0
|
4364 // See drupal_add_js() for details.
|
danielebarchiesi@0
|
4365 $key = 'aggregate_' . $item['group'] . '_' . $item['every_page'] . '_' . $index;
|
danielebarchiesi@0
|
4366 $processed[$key] = '';
|
danielebarchiesi@0
|
4367 $files[$key][$item['data']] = $item;
|
danielebarchiesi@0
|
4368 }
|
danielebarchiesi@0
|
4369 break;
|
danielebarchiesi@0
|
4370
|
danielebarchiesi@0
|
4371 case 'external':
|
danielebarchiesi@0
|
4372 $js_element = $element;
|
danielebarchiesi@0
|
4373 // Preprocessing for external JavaScript files is ignored.
|
danielebarchiesi@0
|
4374 if ($item['defer']) {
|
danielebarchiesi@0
|
4375 $js_element['#attributes']['defer'] = 'defer';
|
danielebarchiesi@0
|
4376 }
|
danielebarchiesi@0
|
4377 $js_element['#attributes']['src'] = $item['data'];
|
danielebarchiesi@0
|
4378 $processed[$index++] = theme('html_tag', array('element' => $js_element));
|
danielebarchiesi@0
|
4379 break;
|
danielebarchiesi@0
|
4380 }
|
danielebarchiesi@0
|
4381 }
|
danielebarchiesi@0
|
4382
|
danielebarchiesi@0
|
4383 // Aggregate any remaining JS files that haven't already been output.
|
danielebarchiesi@0
|
4384 if ($preprocess_js && count($files) > 0) {
|
danielebarchiesi@0
|
4385 foreach ($files as $key => $file_set) {
|
danielebarchiesi@0
|
4386 $uri = drupal_build_js_cache($file_set);
|
danielebarchiesi@0
|
4387 // Only include the file if was written successfully. Errors are logged
|
danielebarchiesi@0
|
4388 // using watchdog.
|
danielebarchiesi@0
|
4389 if ($uri) {
|
danielebarchiesi@0
|
4390 $preprocess_file = file_create_url($uri);
|
danielebarchiesi@0
|
4391 $js_element = $element;
|
danielebarchiesi@0
|
4392 $js_element['#attributes']['src'] = $preprocess_file;
|
danielebarchiesi@0
|
4393 $processed[$key] = theme('html_tag', array('element' => $js_element));
|
danielebarchiesi@0
|
4394 }
|
danielebarchiesi@0
|
4395 }
|
danielebarchiesi@0
|
4396 }
|
danielebarchiesi@0
|
4397
|
danielebarchiesi@0
|
4398 // Keep the order of JS files consistent as some are preprocessed and others are not.
|
danielebarchiesi@0
|
4399 // Make sure any inline or JS setting variables appear last after libraries have loaded.
|
danielebarchiesi@0
|
4400 return implode('', $processed) . $output;
|
danielebarchiesi@0
|
4401 }
|
danielebarchiesi@0
|
4402
|
danielebarchiesi@0
|
4403 /**
|
danielebarchiesi@0
|
4404 * Adds attachments to a render() structure.
|
danielebarchiesi@0
|
4405 *
|
danielebarchiesi@0
|
4406 * Libraries, JavaScript, CSS and other types of custom structures are attached
|
danielebarchiesi@0
|
4407 * to elements using the #attached property. The #attached property is an
|
danielebarchiesi@0
|
4408 * associative array, where the keys are the the attachment types and the values
|
danielebarchiesi@0
|
4409 * are the attached data. For example:
|
danielebarchiesi@0
|
4410 * @code
|
danielebarchiesi@0
|
4411 * $build['#attached'] = array(
|
danielebarchiesi@0
|
4412 * 'js' => array(drupal_get_path('module', 'taxonomy') . '/taxonomy.js'),
|
danielebarchiesi@0
|
4413 * 'css' => array(drupal_get_path('module', 'taxonomy') . '/taxonomy.css'),
|
danielebarchiesi@0
|
4414 * );
|
danielebarchiesi@0
|
4415 * @endcode
|
danielebarchiesi@0
|
4416 *
|
danielebarchiesi@0
|
4417 * 'js', 'css', and 'library' are types that get special handling. For any
|
danielebarchiesi@0
|
4418 * other kind of attached data, the array key must be the full name of the
|
danielebarchiesi@0
|
4419 * callback function and each value an array of arguments. For example:
|
danielebarchiesi@0
|
4420 * @code
|
danielebarchiesi@0
|
4421 * $build['#attached']['drupal_add_http_header'] = array(
|
danielebarchiesi@0
|
4422 * array('Content-Type', 'application/rss+xml; charset=utf-8'),
|
danielebarchiesi@0
|
4423 * );
|
danielebarchiesi@0
|
4424 * @endcode
|
danielebarchiesi@0
|
4425 *
|
danielebarchiesi@0
|
4426 * External 'js' and 'css' files can also be loaded. For example:
|
danielebarchiesi@0
|
4427 * @code
|
danielebarchiesi@0
|
4428 * $build['#attached']['js'] = array(
|
danielebarchiesi@0
|
4429 * 'http://code.jquery.com/jquery-1.4.2.min.js' => array(
|
danielebarchiesi@0
|
4430 * 'type' => 'external',
|
danielebarchiesi@0
|
4431 * ),
|
danielebarchiesi@0
|
4432 * );
|
danielebarchiesi@0
|
4433 * @endcode
|
danielebarchiesi@0
|
4434 *
|
danielebarchiesi@0
|
4435 * @param $elements
|
danielebarchiesi@0
|
4436 * The structured array describing the data being rendered.
|
danielebarchiesi@0
|
4437 * @param $group
|
danielebarchiesi@0
|
4438 * The default group of JavaScript and CSS being added. This is only applied
|
danielebarchiesi@0
|
4439 * to the stylesheets and JavaScript items that don't have an explicit group
|
danielebarchiesi@0
|
4440 * assigned to them.
|
danielebarchiesi@0
|
4441 * @param $dependency_check
|
danielebarchiesi@0
|
4442 * When TRUE, will exit if a given library's dependencies are missing. When
|
danielebarchiesi@0
|
4443 * set to FALSE, will continue to add the libraries, even though one or more
|
danielebarchiesi@0
|
4444 * dependencies are missing. Defaults to FALSE.
|
danielebarchiesi@0
|
4445 * @param $every_page
|
danielebarchiesi@0
|
4446 * Set to TRUE to indicate that the attachments are added to every page on the
|
danielebarchiesi@0
|
4447 * site. Only attachments with the every_page flag set to TRUE can participate
|
danielebarchiesi@0
|
4448 * in JavaScript/CSS aggregation.
|
danielebarchiesi@0
|
4449 *
|
danielebarchiesi@0
|
4450 * @return
|
danielebarchiesi@0
|
4451 * FALSE if there were any missing library dependencies; TRUE if all library
|
danielebarchiesi@0
|
4452 * dependencies were met.
|
danielebarchiesi@0
|
4453 *
|
danielebarchiesi@0
|
4454 * @see drupal_add_library()
|
danielebarchiesi@0
|
4455 * @see drupal_add_js()
|
danielebarchiesi@0
|
4456 * @see drupal_add_css()
|
danielebarchiesi@0
|
4457 * @see drupal_render()
|
danielebarchiesi@0
|
4458 */
|
danielebarchiesi@0
|
4459 function drupal_process_attached($elements, $group = JS_DEFAULT, $dependency_check = FALSE, $every_page = NULL) {
|
danielebarchiesi@0
|
4460 // Add defaults to the special attached structures that should be processed differently.
|
danielebarchiesi@0
|
4461 $elements['#attached'] += array(
|
danielebarchiesi@0
|
4462 'library' => array(),
|
danielebarchiesi@0
|
4463 'js' => array(),
|
danielebarchiesi@0
|
4464 'css' => array(),
|
danielebarchiesi@0
|
4465 );
|
danielebarchiesi@0
|
4466
|
danielebarchiesi@0
|
4467 // Add the libraries first.
|
danielebarchiesi@0
|
4468 $success = TRUE;
|
danielebarchiesi@0
|
4469 foreach ($elements['#attached']['library'] as $library) {
|
danielebarchiesi@0
|
4470 if (drupal_add_library($library[0], $library[1], $every_page) === FALSE) {
|
danielebarchiesi@0
|
4471 $success = FALSE;
|
danielebarchiesi@0
|
4472 // Exit if the dependency is missing.
|
danielebarchiesi@0
|
4473 if ($dependency_check) {
|
danielebarchiesi@0
|
4474 return $success;
|
danielebarchiesi@0
|
4475 }
|
danielebarchiesi@0
|
4476 }
|
danielebarchiesi@0
|
4477 }
|
danielebarchiesi@0
|
4478 unset($elements['#attached']['library']);
|
danielebarchiesi@0
|
4479
|
danielebarchiesi@0
|
4480 // Add both the JavaScript and the CSS.
|
danielebarchiesi@0
|
4481 // The parameters for drupal_add_js() and drupal_add_css() require special
|
danielebarchiesi@0
|
4482 // handling.
|
danielebarchiesi@0
|
4483 foreach (array('js', 'css') as $type) {
|
danielebarchiesi@0
|
4484 foreach ($elements['#attached'][$type] as $data => $options) {
|
danielebarchiesi@0
|
4485 // If the value is not an array, it's a filename and passed as first
|
danielebarchiesi@0
|
4486 // (and only) argument.
|
danielebarchiesi@0
|
4487 if (!is_array($options)) {
|
danielebarchiesi@0
|
4488 $data = $options;
|
danielebarchiesi@0
|
4489 $options = NULL;
|
danielebarchiesi@0
|
4490 }
|
danielebarchiesi@0
|
4491 // In some cases, the first parameter ($data) is an array. Arrays can't be
|
danielebarchiesi@0
|
4492 // passed as keys in PHP, so we have to get $data from the value array.
|
danielebarchiesi@0
|
4493 if (is_numeric($data)) {
|
danielebarchiesi@0
|
4494 $data = $options['data'];
|
danielebarchiesi@0
|
4495 unset($options['data']);
|
danielebarchiesi@0
|
4496 }
|
danielebarchiesi@0
|
4497 // Apply the default group if it isn't explicitly given.
|
danielebarchiesi@0
|
4498 if (!isset($options['group'])) {
|
danielebarchiesi@0
|
4499 $options['group'] = $group;
|
danielebarchiesi@0
|
4500 }
|
danielebarchiesi@0
|
4501 // Set the every_page flag if one was passed.
|
danielebarchiesi@0
|
4502 if (isset($every_page)) {
|
danielebarchiesi@0
|
4503 $options['every_page'] = $every_page;
|
danielebarchiesi@0
|
4504 }
|
danielebarchiesi@0
|
4505 call_user_func('drupal_add_' . $type, $data, $options);
|
danielebarchiesi@0
|
4506 }
|
danielebarchiesi@0
|
4507 unset($elements['#attached'][$type]);
|
danielebarchiesi@0
|
4508 }
|
danielebarchiesi@0
|
4509
|
danielebarchiesi@0
|
4510 // Add additional types of attachments specified in the render() structure.
|
danielebarchiesi@0
|
4511 // Libraries, JavaScript and CSS have been added already, as they require
|
danielebarchiesi@0
|
4512 // special handling.
|
danielebarchiesi@0
|
4513 foreach ($elements['#attached'] as $callback => $options) {
|
danielebarchiesi@0
|
4514 if (function_exists($callback)) {
|
danielebarchiesi@0
|
4515 foreach ($elements['#attached'][$callback] as $args) {
|
danielebarchiesi@0
|
4516 call_user_func_array($callback, $args);
|
danielebarchiesi@0
|
4517 }
|
danielebarchiesi@0
|
4518 }
|
danielebarchiesi@0
|
4519 }
|
danielebarchiesi@0
|
4520
|
danielebarchiesi@0
|
4521 return $success;
|
danielebarchiesi@0
|
4522 }
|
danielebarchiesi@0
|
4523
|
danielebarchiesi@0
|
4524 /**
|
danielebarchiesi@0
|
4525 * Adds JavaScript to change the state of an element based on another element.
|
danielebarchiesi@0
|
4526 *
|
danielebarchiesi@0
|
4527 * A "state" means a certain property on a DOM element, such as "visible" or
|
danielebarchiesi@0
|
4528 * "checked". A state can be applied to an element, depending on the state of
|
danielebarchiesi@0
|
4529 * another element on the page. In general, states depend on HTML attributes and
|
danielebarchiesi@0
|
4530 * DOM element properties, which change due to user interaction.
|
danielebarchiesi@0
|
4531 *
|
danielebarchiesi@0
|
4532 * Since states are driven by JavaScript only, it is important to understand
|
danielebarchiesi@0
|
4533 * that all states are applied on presentation only, none of the states force
|
danielebarchiesi@0
|
4534 * any server-side logic, and that they will not be applied for site visitors
|
danielebarchiesi@0
|
4535 * without JavaScript support. All modules implementing states have to make
|
danielebarchiesi@0
|
4536 * sure that the intended logic also works without JavaScript being enabled.
|
danielebarchiesi@0
|
4537 *
|
danielebarchiesi@0
|
4538 * #states is an associative array in the form of:
|
danielebarchiesi@0
|
4539 * @code
|
danielebarchiesi@0
|
4540 * array(
|
danielebarchiesi@0
|
4541 * STATE1 => CONDITIONS_ARRAY1,
|
danielebarchiesi@0
|
4542 * STATE2 => CONDITIONS_ARRAY2,
|
danielebarchiesi@0
|
4543 * ...
|
danielebarchiesi@0
|
4544 * )
|
danielebarchiesi@0
|
4545 * @endcode
|
danielebarchiesi@0
|
4546 * Each key is the name of a state to apply to the element, such as 'visible'.
|
danielebarchiesi@0
|
4547 * Each value is a list of conditions that denote when the state should be
|
danielebarchiesi@0
|
4548 * applied.
|
danielebarchiesi@0
|
4549 *
|
danielebarchiesi@0
|
4550 * Multiple different states may be specified to act on complex conditions:
|
danielebarchiesi@0
|
4551 * @code
|
danielebarchiesi@0
|
4552 * array(
|
danielebarchiesi@0
|
4553 * 'visible' => CONDITIONS,
|
danielebarchiesi@0
|
4554 * 'checked' => OTHER_CONDITIONS,
|
danielebarchiesi@0
|
4555 * )
|
danielebarchiesi@0
|
4556 * @endcode
|
danielebarchiesi@0
|
4557 *
|
danielebarchiesi@0
|
4558 * Every condition is a key/value pair, whose key is a jQuery selector that
|
danielebarchiesi@0
|
4559 * denotes another element on the page, and whose value is an array of
|
danielebarchiesi@0
|
4560 * conditions, which must bet met on that element:
|
danielebarchiesi@0
|
4561 * @code
|
danielebarchiesi@0
|
4562 * array(
|
danielebarchiesi@0
|
4563 * 'visible' => array(
|
danielebarchiesi@0
|
4564 * JQUERY_SELECTOR => REMOTE_CONDITIONS,
|
danielebarchiesi@0
|
4565 * JQUERY_SELECTOR => REMOTE_CONDITIONS,
|
danielebarchiesi@0
|
4566 * ...
|
danielebarchiesi@0
|
4567 * ),
|
danielebarchiesi@0
|
4568 * )
|
danielebarchiesi@0
|
4569 * @endcode
|
danielebarchiesi@0
|
4570 * All conditions must be met for the state to be applied.
|
danielebarchiesi@0
|
4571 *
|
danielebarchiesi@0
|
4572 * Each remote condition is a key/value pair specifying conditions on the other
|
danielebarchiesi@0
|
4573 * element that need to be met to apply the state to the element:
|
danielebarchiesi@0
|
4574 * @code
|
danielebarchiesi@0
|
4575 * array(
|
danielebarchiesi@0
|
4576 * 'visible' => array(
|
danielebarchiesi@0
|
4577 * ':input[name="remote_checkbox"]' => array('checked' => TRUE),
|
danielebarchiesi@0
|
4578 * ),
|
danielebarchiesi@0
|
4579 * )
|
danielebarchiesi@0
|
4580 * @endcode
|
danielebarchiesi@0
|
4581 *
|
danielebarchiesi@0
|
4582 * For example, to show a textfield only when a checkbox is checked:
|
danielebarchiesi@0
|
4583 * @code
|
danielebarchiesi@0
|
4584 * $form['toggle_me'] = array(
|
danielebarchiesi@0
|
4585 * '#type' => 'checkbox',
|
danielebarchiesi@0
|
4586 * '#title' => t('Tick this box to type'),
|
danielebarchiesi@0
|
4587 * );
|
danielebarchiesi@0
|
4588 * $form['settings'] = array(
|
danielebarchiesi@0
|
4589 * '#type' => 'textfield',
|
danielebarchiesi@0
|
4590 * '#states' => array(
|
danielebarchiesi@0
|
4591 * // Only show this field when the 'toggle_me' checkbox is enabled.
|
danielebarchiesi@0
|
4592 * 'visible' => array(
|
danielebarchiesi@0
|
4593 * ':input[name="toggle_me"]' => array('checked' => TRUE),
|
danielebarchiesi@0
|
4594 * ),
|
danielebarchiesi@0
|
4595 * ),
|
danielebarchiesi@0
|
4596 * );
|
danielebarchiesi@0
|
4597 * @endcode
|
danielebarchiesi@0
|
4598 *
|
danielebarchiesi@0
|
4599 * The following states may be applied to an element:
|
danielebarchiesi@0
|
4600 * - enabled
|
danielebarchiesi@0
|
4601 * - disabled
|
danielebarchiesi@0
|
4602 * - required
|
danielebarchiesi@0
|
4603 * - optional
|
danielebarchiesi@0
|
4604 * - visible
|
danielebarchiesi@0
|
4605 * - invisible
|
danielebarchiesi@0
|
4606 * - checked
|
danielebarchiesi@0
|
4607 * - unchecked
|
danielebarchiesi@0
|
4608 * - expanded
|
danielebarchiesi@0
|
4609 * - collapsed
|
danielebarchiesi@0
|
4610 *
|
danielebarchiesi@0
|
4611 * The following states may be used in remote conditions:
|
danielebarchiesi@0
|
4612 * - empty
|
danielebarchiesi@0
|
4613 * - filled
|
danielebarchiesi@0
|
4614 * - checked
|
danielebarchiesi@0
|
4615 * - unchecked
|
danielebarchiesi@0
|
4616 * - expanded
|
danielebarchiesi@0
|
4617 * - collapsed
|
danielebarchiesi@0
|
4618 * - value
|
danielebarchiesi@0
|
4619 *
|
danielebarchiesi@0
|
4620 * The following states exist for both elements and remote conditions, but are
|
danielebarchiesi@0
|
4621 * not fully implemented and may not change anything on the element:
|
danielebarchiesi@0
|
4622 * - relevant
|
danielebarchiesi@0
|
4623 * - irrelevant
|
danielebarchiesi@0
|
4624 * - valid
|
danielebarchiesi@0
|
4625 * - invalid
|
danielebarchiesi@0
|
4626 * - touched
|
danielebarchiesi@0
|
4627 * - untouched
|
danielebarchiesi@0
|
4628 * - readwrite
|
danielebarchiesi@0
|
4629 * - readonly
|
danielebarchiesi@0
|
4630 *
|
danielebarchiesi@0
|
4631 * When referencing select lists and radio buttons in remote conditions, a
|
danielebarchiesi@0
|
4632 * 'value' condition must be used:
|
danielebarchiesi@0
|
4633 * @code
|
danielebarchiesi@0
|
4634 * '#states' => array(
|
danielebarchiesi@0
|
4635 * // Show the settings if 'bar' has been selected for 'foo'.
|
danielebarchiesi@0
|
4636 * 'visible' => array(
|
danielebarchiesi@0
|
4637 * ':input[name="foo"]' => array('value' => 'bar'),
|
danielebarchiesi@0
|
4638 * ),
|
danielebarchiesi@0
|
4639 * ),
|
danielebarchiesi@0
|
4640 * @endcode
|
danielebarchiesi@0
|
4641 *
|
danielebarchiesi@0
|
4642 * @param $elements
|
danielebarchiesi@0
|
4643 * A renderable array element having a #states property as described above.
|
danielebarchiesi@0
|
4644 *
|
danielebarchiesi@0
|
4645 * @see form_example_states_form()
|
danielebarchiesi@0
|
4646 */
|
danielebarchiesi@0
|
4647 function drupal_process_states(&$elements) {
|
danielebarchiesi@0
|
4648 $elements['#attached']['library'][] = array('system', 'drupal.states');
|
danielebarchiesi@0
|
4649 $elements['#attached']['js'][] = array(
|
danielebarchiesi@0
|
4650 'type' => 'setting',
|
danielebarchiesi@0
|
4651 'data' => array('states' => array('#' . $elements['#id'] => $elements['#states'])),
|
danielebarchiesi@0
|
4652 );
|
danielebarchiesi@0
|
4653 }
|
danielebarchiesi@0
|
4654
|
danielebarchiesi@0
|
4655 /**
|
danielebarchiesi@0
|
4656 * Adds multiple JavaScript or CSS files at the same time.
|
danielebarchiesi@0
|
4657 *
|
danielebarchiesi@0
|
4658 * A library defines a set of JavaScript and/or CSS files, optionally using
|
danielebarchiesi@0
|
4659 * settings, and optionally requiring another library. For example, a library
|
danielebarchiesi@0
|
4660 * can be a jQuery plugin, a JavaScript framework, or a CSS framework. This
|
danielebarchiesi@0
|
4661 * function allows modules to load a library defined/shipped by itself or a
|
danielebarchiesi@0
|
4662 * depending module, without having to add all files of the library separately.
|
danielebarchiesi@0
|
4663 * Each library is only loaded once.
|
danielebarchiesi@0
|
4664 *
|
danielebarchiesi@0
|
4665 * @param $module
|
danielebarchiesi@0
|
4666 * The name of the module that registered the library.
|
danielebarchiesi@0
|
4667 * @param $name
|
danielebarchiesi@0
|
4668 * The name of the library to add.
|
danielebarchiesi@0
|
4669 * @param $every_page
|
danielebarchiesi@0
|
4670 * Set to TRUE if this library is added to every page on the site. Only items
|
danielebarchiesi@0
|
4671 * with the every_page flag set to TRUE can participate in aggregation.
|
danielebarchiesi@0
|
4672 *
|
danielebarchiesi@0
|
4673 * @return
|
danielebarchiesi@0
|
4674 * TRUE if the library was successfully added; FALSE if the library or one of
|
danielebarchiesi@0
|
4675 * its dependencies could not be added.
|
danielebarchiesi@0
|
4676 *
|
danielebarchiesi@0
|
4677 * @see drupal_get_library()
|
danielebarchiesi@0
|
4678 * @see hook_library()
|
danielebarchiesi@0
|
4679 * @see hook_library_alter()
|
danielebarchiesi@0
|
4680 */
|
danielebarchiesi@0
|
4681 function drupal_add_library($module, $name, $every_page = NULL) {
|
danielebarchiesi@0
|
4682 $added = &drupal_static(__FUNCTION__, array());
|
danielebarchiesi@0
|
4683
|
danielebarchiesi@0
|
4684 // Only process the library if it exists and it was not added already.
|
danielebarchiesi@0
|
4685 if (!isset($added[$module][$name])) {
|
danielebarchiesi@0
|
4686 if ($library = drupal_get_library($module, $name)) {
|
danielebarchiesi@0
|
4687 // Add all components within the library.
|
danielebarchiesi@0
|
4688 $elements['#attached'] = array(
|
danielebarchiesi@0
|
4689 'library' => $library['dependencies'],
|
danielebarchiesi@0
|
4690 'js' => $library['js'],
|
danielebarchiesi@0
|
4691 'css' => $library['css'],
|
danielebarchiesi@0
|
4692 );
|
danielebarchiesi@0
|
4693 $added[$module][$name] = drupal_process_attached($elements, JS_LIBRARY, TRUE, $every_page);
|
danielebarchiesi@0
|
4694 }
|
danielebarchiesi@0
|
4695 else {
|
danielebarchiesi@0
|
4696 // Requested library does not exist.
|
danielebarchiesi@0
|
4697 $added[$module][$name] = FALSE;
|
danielebarchiesi@0
|
4698 }
|
danielebarchiesi@0
|
4699 }
|
danielebarchiesi@0
|
4700
|
danielebarchiesi@0
|
4701 return $added[$module][$name];
|
danielebarchiesi@0
|
4702 }
|
danielebarchiesi@0
|
4703
|
danielebarchiesi@0
|
4704 /**
|
danielebarchiesi@0
|
4705 * Retrieves information for a JavaScript/CSS library.
|
danielebarchiesi@0
|
4706 *
|
danielebarchiesi@0
|
4707 * Library information is statically cached. Libraries are keyed by module for
|
danielebarchiesi@0
|
4708 * several reasons:
|
danielebarchiesi@0
|
4709 * - Libraries are not unique. Multiple modules might ship with the same library
|
danielebarchiesi@0
|
4710 * in a different version or variant. This registry cannot (and does not
|
danielebarchiesi@0
|
4711 * attempt to) prevent library conflicts.
|
danielebarchiesi@0
|
4712 * - Modules implementing and thereby depending on a library that is registered
|
danielebarchiesi@0
|
4713 * by another module can only rely on that module's library.
|
danielebarchiesi@0
|
4714 * - Two (or more) modules can still register the same library and use it
|
danielebarchiesi@0
|
4715 * without conflicts in case the libraries are loaded on certain pages only.
|
danielebarchiesi@0
|
4716 *
|
danielebarchiesi@0
|
4717 * @param $module
|
danielebarchiesi@0
|
4718 * The name of a module that registered a library.
|
danielebarchiesi@0
|
4719 * @param $name
|
danielebarchiesi@0
|
4720 * (optional) The name of a registered library to retrieve. By default, all
|
danielebarchiesi@0
|
4721 * libraries registered by $module are returned.
|
danielebarchiesi@0
|
4722 *
|
danielebarchiesi@0
|
4723 * @return
|
danielebarchiesi@0
|
4724 * The definition of the requested library, if $name was passed and it exists,
|
danielebarchiesi@0
|
4725 * or FALSE if it does not exist. If no $name was passed, an associative array
|
danielebarchiesi@0
|
4726 * of libraries registered by $module is returned (which may be empty).
|
danielebarchiesi@0
|
4727 *
|
danielebarchiesi@0
|
4728 * @see drupal_add_library()
|
danielebarchiesi@0
|
4729 * @see hook_library()
|
danielebarchiesi@0
|
4730 * @see hook_library_alter()
|
danielebarchiesi@0
|
4731 *
|
danielebarchiesi@0
|
4732 * @todo The purpose of drupal_get_*() is completely different to other page
|
danielebarchiesi@0
|
4733 * requisite API functions; find and use a different name.
|
danielebarchiesi@0
|
4734 */
|
danielebarchiesi@0
|
4735 function drupal_get_library($module, $name = NULL) {
|
danielebarchiesi@0
|
4736 $libraries = &drupal_static(__FUNCTION__, array());
|
danielebarchiesi@0
|
4737
|
danielebarchiesi@0
|
4738 if (!isset($libraries[$module])) {
|
danielebarchiesi@0
|
4739 // Retrieve all libraries associated with the module.
|
danielebarchiesi@0
|
4740 $module_libraries = module_invoke($module, 'library');
|
danielebarchiesi@0
|
4741 if (empty($module_libraries)) {
|
danielebarchiesi@0
|
4742 $module_libraries = array();
|
danielebarchiesi@0
|
4743 }
|
danielebarchiesi@0
|
4744 // Allow modules to alter the module's registered libraries.
|
danielebarchiesi@0
|
4745 drupal_alter('library', $module_libraries, $module);
|
danielebarchiesi@0
|
4746
|
danielebarchiesi@0
|
4747 foreach ($module_libraries as $key => $data) {
|
danielebarchiesi@0
|
4748 if (is_array($data)) {
|
danielebarchiesi@0
|
4749 // Add default elements to allow for easier processing.
|
danielebarchiesi@0
|
4750 $module_libraries[$key] += array('dependencies' => array(), 'js' => array(), 'css' => array());
|
danielebarchiesi@0
|
4751 foreach ($module_libraries[$key]['js'] as $file => $options) {
|
danielebarchiesi@0
|
4752 $module_libraries[$key]['js'][$file]['version'] = $module_libraries[$key]['version'];
|
danielebarchiesi@0
|
4753 }
|
danielebarchiesi@0
|
4754 }
|
danielebarchiesi@0
|
4755 }
|
danielebarchiesi@0
|
4756 $libraries[$module] = $module_libraries;
|
danielebarchiesi@0
|
4757 }
|
danielebarchiesi@0
|
4758 if (isset($name)) {
|
danielebarchiesi@0
|
4759 if (!isset($libraries[$module][$name])) {
|
danielebarchiesi@0
|
4760 $libraries[$module][$name] = FALSE;
|
danielebarchiesi@0
|
4761 }
|
danielebarchiesi@0
|
4762 return $libraries[$module][$name];
|
danielebarchiesi@0
|
4763 }
|
danielebarchiesi@0
|
4764 return $libraries[$module];
|
danielebarchiesi@0
|
4765 }
|
danielebarchiesi@0
|
4766
|
danielebarchiesi@0
|
4767 /**
|
danielebarchiesi@0
|
4768 * Assists in adding the tableDrag JavaScript behavior to a themed table.
|
danielebarchiesi@0
|
4769 *
|
danielebarchiesi@0
|
4770 * Draggable tables should be used wherever an outline or list of sortable items
|
danielebarchiesi@0
|
4771 * needs to be arranged by an end-user. Draggable tables are very flexible and
|
danielebarchiesi@0
|
4772 * can manipulate the value of form elements placed within individual columns.
|
danielebarchiesi@0
|
4773 *
|
danielebarchiesi@0
|
4774 * To set up a table to use drag and drop in place of weight select-lists or in
|
danielebarchiesi@0
|
4775 * place of a form that contains parent relationships, the form must be themed
|
danielebarchiesi@0
|
4776 * into a table. The table must have an ID attribute set. If using
|
danielebarchiesi@0
|
4777 * theme_table(), the ID may be set as follows:
|
danielebarchiesi@0
|
4778 * @code
|
danielebarchiesi@0
|
4779 * $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'my-module-table')));
|
danielebarchiesi@0
|
4780 * return $output;
|
danielebarchiesi@0
|
4781 * @endcode
|
danielebarchiesi@0
|
4782 *
|
danielebarchiesi@0
|
4783 * In the theme function for the form, a special class must be added to each
|
danielebarchiesi@0
|
4784 * form element within the same column, "grouping" them together.
|
danielebarchiesi@0
|
4785 *
|
danielebarchiesi@0
|
4786 * In a situation where a single weight column is being sorted in the table, the
|
danielebarchiesi@0
|
4787 * classes could be added like this (in the theme function):
|
danielebarchiesi@0
|
4788 * @code
|
danielebarchiesi@0
|
4789 * $form['my_elements'][$delta]['weight']['#attributes']['class'] = array('my-elements-weight');
|
danielebarchiesi@0
|
4790 * @endcode
|
danielebarchiesi@0
|
4791 *
|
danielebarchiesi@0
|
4792 * Each row of the table must also have a class of "draggable" in order to
|
danielebarchiesi@0
|
4793 * enable the drag handles:
|
danielebarchiesi@0
|
4794 * @code
|
danielebarchiesi@0
|
4795 * $row = array(...);
|
danielebarchiesi@0
|
4796 * $rows[] = array(
|
danielebarchiesi@0
|
4797 * 'data' => $row,
|
danielebarchiesi@0
|
4798 * 'class' => array('draggable'),
|
danielebarchiesi@0
|
4799 * );
|
danielebarchiesi@0
|
4800 * @endcode
|
danielebarchiesi@0
|
4801 *
|
danielebarchiesi@0
|
4802 * When tree relationships are present, the two additional classes
|
danielebarchiesi@0
|
4803 * 'tabledrag-leaf' and 'tabledrag-root' can be used to refine the behavior:
|
danielebarchiesi@0
|
4804 * - Rows with the 'tabledrag-leaf' class cannot have child rows.
|
danielebarchiesi@0
|
4805 * - Rows with the 'tabledrag-root' class cannot be nested under a parent row.
|
danielebarchiesi@0
|
4806 *
|
danielebarchiesi@0
|
4807 * Calling drupal_add_tabledrag() would then be written as such:
|
danielebarchiesi@0
|
4808 * @code
|
danielebarchiesi@0
|
4809 * drupal_add_tabledrag('my-module-table', 'order', 'sibling', 'my-elements-weight');
|
danielebarchiesi@0
|
4810 * @endcode
|
danielebarchiesi@0
|
4811 *
|
danielebarchiesi@0
|
4812 * In a more complex case where there are several groups in one column (such as
|
danielebarchiesi@0
|
4813 * the block regions on the admin/structure/block page), a separate subgroup
|
danielebarchiesi@0
|
4814 * class must also be added to differentiate the groups.
|
danielebarchiesi@0
|
4815 * @code
|
danielebarchiesi@0
|
4816 * $form['my_elements'][$region][$delta]['weight']['#attributes']['class'] = array('my-elements-weight', 'my-elements-weight-' . $region);
|
danielebarchiesi@0
|
4817 * @endcode
|
danielebarchiesi@0
|
4818 *
|
danielebarchiesi@0
|
4819 * $group is still 'my-element-weight', and the additional $subgroup variable
|
danielebarchiesi@0
|
4820 * will be passed in as 'my-elements-weight-' . $region. This also means that
|
danielebarchiesi@0
|
4821 * you'll need to call drupal_add_tabledrag() once for every region added.
|
danielebarchiesi@0
|
4822 *
|
danielebarchiesi@0
|
4823 * @code
|
danielebarchiesi@0
|
4824 * foreach ($regions as $region) {
|
danielebarchiesi@0
|
4825 * drupal_add_tabledrag('my-module-table', 'order', 'sibling', 'my-elements-weight', 'my-elements-weight-' . $region);
|
danielebarchiesi@0
|
4826 * }
|
danielebarchiesi@0
|
4827 * @endcode
|
danielebarchiesi@0
|
4828 *
|
danielebarchiesi@0
|
4829 * In a situation where tree relationships are present, adding multiple
|
danielebarchiesi@0
|
4830 * subgroups is not necessary, because the table will contain indentations that
|
danielebarchiesi@0
|
4831 * provide enough information about the sibling and parent relationships. See
|
danielebarchiesi@0
|
4832 * theme_menu_overview_form() for an example creating a table containing parent
|
danielebarchiesi@0
|
4833 * relationships.
|
danielebarchiesi@0
|
4834 *
|
danielebarchiesi@0
|
4835 * Note that this function should be called from the theme layer, such as in a
|
danielebarchiesi@0
|
4836 * .tpl.php file, theme_ function, or in a template_preprocess function, not in
|
danielebarchiesi@0
|
4837 * a form declaration. Though the same JavaScript could be added to the page
|
danielebarchiesi@0
|
4838 * using drupal_add_js() directly, this function helps keep template files
|
danielebarchiesi@0
|
4839 * clean and readable. It also prevents tabledrag.js from being added twice
|
danielebarchiesi@0
|
4840 * accidentally.
|
danielebarchiesi@0
|
4841 *
|
danielebarchiesi@0
|
4842 * @param $table_id
|
danielebarchiesi@0
|
4843 * String containing the target table's id attribute. If the table does not
|
danielebarchiesi@0
|
4844 * have an id, one will need to be set, such as <table id="my-module-table">.
|
danielebarchiesi@0
|
4845 * @param $action
|
danielebarchiesi@0
|
4846 * String describing the action to be done on the form item. Either 'match'
|
danielebarchiesi@0
|
4847 * 'depth', or 'order'. Match is typically used for parent relationships.
|
danielebarchiesi@0
|
4848 * Order is typically used to set weights on other form elements with the same
|
danielebarchiesi@0
|
4849 * group. Depth updates the target element with the current indentation.
|
danielebarchiesi@0
|
4850 * @param $relationship
|
danielebarchiesi@0
|
4851 * String describing where the $action variable should be performed. Either
|
danielebarchiesi@0
|
4852 * 'parent', 'sibling', 'group', or 'self'. Parent will only look for fields
|
danielebarchiesi@0
|
4853 * up the tree. Sibling will look for fields in the same group in rows above
|
danielebarchiesi@0
|
4854 * and below it. Self affects the dragged row itself. Group affects the
|
danielebarchiesi@0
|
4855 * dragged row, plus any children below it (the entire dragged group).
|
danielebarchiesi@0
|
4856 * @param $group
|
danielebarchiesi@0
|
4857 * A class name applied on all related form elements for this action.
|
danielebarchiesi@0
|
4858 * @param $subgroup
|
danielebarchiesi@0
|
4859 * (optional) If the group has several subgroups within it, this string should
|
danielebarchiesi@0
|
4860 * contain the class name identifying fields in the same subgroup.
|
danielebarchiesi@0
|
4861 * @param $source
|
danielebarchiesi@0
|
4862 * (optional) If the $action is 'match', this string should contain the class
|
danielebarchiesi@0
|
4863 * name identifying what field will be used as the source value when matching
|
danielebarchiesi@0
|
4864 * the value in $subgroup.
|
danielebarchiesi@0
|
4865 * @param $hidden
|
danielebarchiesi@0
|
4866 * (optional) The column containing the field elements may be entirely hidden
|
danielebarchiesi@0
|
4867 * from view dynamically when the JavaScript is loaded. Set to FALSE if the
|
danielebarchiesi@0
|
4868 * column should not be hidden.
|
danielebarchiesi@0
|
4869 * @param $limit
|
danielebarchiesi@0
|
4870 * (optional) Limit the maximum amount of parenting in this table.
|
danielebarchiesi@0
|
4871 * @see block-admin-display-form.tpl.php
|
danielebarchiesi@0
|
4872 * @see theme_menu_overview_form()
|
danielebarchiesi@0
|
4873 */
|
danielebarchiesi@0
|
4874 function drupal_add_tabledrag($table_id, $action, $relationship, $group, $subgroup = NULL, $source = NULL, $hidden = TRUE, $limit = 0) {
|
danielebarchiesi@0
|
4875 $js_added = &drupal_static(__FUNCTION__, FALSE);
|
danielebarchiesi@0
|
4876 if (!$js_added) {
|
danielebarchiesi@0
|
4877 // Add the table drag JavaScript to the page before the module JavaScript
|
danielebarchiesi@0
|
4878 // to ensure that table drag behaviors are registered before any module
|
danielebarchiesi@0
|
4879 // uses it.
|
danielebarchiesi@0
|
4880 drupal_add_library('system', 'jquery.cookie');
|
danielebarchiesi@0
|
4881 drupal_add_js('misc/tabledrag.js', array('weight' => -1));
|
danielebarchiesi@0
|
4882 $js_added = TRUE;
|
danielebarchiesi@0
|
4883 }
|
danielebarchiesi@0
|
4884
|
danielebarchiesi@0
|
4885 // If a subgroup or source isn't set, assume it is the same as the group.
|
danielebarchiesi@0
|
4886 $target = isset($subgroup) ? $subgroup : $group;
|
danielebarchiesi@0
|
4887 $source = isset($source) ? $source : $target;
|
danielebarchiesi@0
|
4888 $settings['tableDrag'][$table_id][$group][] = array(
|
danielebarchiesi@0
|
4889 'target' => $target,
|
danielebarchiesi@0
|
4890 'source' => $source,
|
danielebarchiesi@0
|
4891 'relationship' => $relationship,
|
danielebarchiesi@0
|
4892 'action' => $action,
|
danielebarchiesi@0
|
4893 'hidden' => $hidden,
|
danielebarchiesi@0
|
4894 'limit' => $limit,
|
danielebarchiesi@0
|
4895 );
|
danielebarchiesi@0
|
4896 drupal_add_js($settings, 'setting');
|
danielebarchiesi@0
|
4897 }
|
danielebarchiesi@0
|
4898
|
danielebarchiesi@0
|
4899 /**
|
danielebarchiesi@0
|
4900 * Aggregates JavaScript files into a cache file in the files directory.
|
danielebarchiesi@0
|
4901 *
|
danielebarchiesi@0
|
4902 * The file name for the JavaScript cache file is generated from the hash of
|
danielebarchiesi@0
|
4903 * the aggregated contents of the files in $files. This forces proxies and
|
danielebarchiesi@0
|
4904 * browsers to download new JavaScript when the JavaScript changes.
|
danielebarchiesi@0
|
4905 *
|
danielebarchiesi@0
|
4906 * The cache file name is retrieved on a page load via a lookup variable that
|
danielebarchiesi@0
|
4907 * contains an associative array. The array key is the hash of the names in
|
danielebarchiesi@0
|
4908 * $files while the value is the cache file name. The cache file is generated
|
danielebarchiesi@0
|
4909 * in two cases. First, if there is no file name value for the key, which will
|
danielebarchiesi@0
|
4910 * happen if a new file name has been added to $files or after the lookup
|
danielebarchiesi@0
|
4911 * variable is emptied to force a rebuild of the cache. Second, the cache file
|
danielebarchiesi@0
|
4912 * is generated if it is missing on disk. Old cache files are not deleted
|
danielebarchiesi@0
|
4913 * immediately when the lookup variable is emptied, but are deleted after a set
|
danielebarchiesi@0
|
4914 * period by drupal_delete_file_if_stale(). This ensures that files referenced
|
danielebarchiesi@0
|
4915 * by a cached page will still be available.
|
danielebarchiesi@0
|
4916 *
|
danielebarchiesi@0
|
4917 * @param $files
|
danielebarchiesi@0
|
4918 * An array of JavaScript files to aggregate and compress into one file.
|
danielebarchiesi@0
|
4919 *
|
danielebarchiesi@0
|
4920 * @return
|
danielebarchiesi@0
|
4921 * The URI of the cache file, or FALSE if the file could not be saved.
|
danielebarchiesi@0
|
4922 */
|
danielebarchiesi@0
|
4923 function drupal_build_js_cache($files) {
|
danielebarchiesi@0
|
4924 $contents = '';
|
danielebarchiesi@0
|
4925 $uri = '';
|
danielebarchiesi@0
|
4926 $map = variable_get('drupal_js_cache_files', array());
|
danielebarchiesi@0
|
4927 // Create a new array so that only the file names are used to create the hash.
|
danielebarchiesi@0
|
4928 // This prevents new aggregates from being created unnecessarily.
|
danielebarchiesi@0
|
4929 $js_data = array();
|
danielebarchiesi@0
|
4930 foreach ($files as $file) {
|
danielebarchiesi@0
|
4931 $js_data[] = $file['data'];
|
danielebarchiesi@0
|
4932 }
|
danielebarchiesi@0
|
4933 $key = hash('sha256', serialize($js_data));
|
danielebarchiesi@0
|
4934 if (isset($map[$key])) {
|
danielebarchiesi@0
|
4935 $uri = $map[$key];
|
danielebarchiesi@0
|
4936 }
|
danielebarchiesi@0
|
4937
|
danielebarchiesi@0
|
4938 if (empty($uri) || !file_exists($uri)) {
|
danielebarchiesi@0
|
4939 // Build aggregate JS file.
|
danielebarchiesi@0
|
4940 foreach ($files as $path => $info) {
|
danielebarchiesi@0
|
4941 if ($info['preprocess']) {
|
danielebarchiesi@0
|
4942 // Append a ';' and a newline after each JS file to prevent them from running together.
|
danielebarchiesi@0
|
4943 $contents .= file_get_contents($path) . ";\n";
|
danielebarchiesi@0
|
4944 }
|
danielebarchiesi@0
|
4945 }
|
danielebarchiesi@0
|
4946 // Prefix filename to prevent blocking by firewalls which reject files
|
danielebarchiesi@0
|
4947 // starting with "ad*".
|
danielebarchiesi@0
|
4948 $filename = 'js_' . drupal_hash_base64($contents) . '.js';
|
danielebarchiesi@0
|
4949 // Create the js/ within the files folder.
|
danielebarchiesi@0
|
4950 $jspath = 'public://js';
|
danielebarchiesi@0
|
4951 $uri = $jspath . '/' . $filename;
|
danielebarchiesi@0
|
4952 // Create the JS file.
|
danielebarchiesi@0
|
4953 file_prepare_directory($jspath, FILE_CREATE_DIRECTORY);
|
danielebarchiesi@0
|
4954 if (!file_exists($uri) && !file_unmanaged_save_data($contents, $uri, FILE_EXISTS_REPLACE)) {
|
danielebarchiesi@0
|
4955 return FALSE;
|
danielebarchiesi@0
|
4956 }
|
danielebarchiesi@0
|
4957 // If JS gzip compression is enabled, clean URLs are enabled (which means
|
danielebarchiesi@0
|
4958 // that rewrite rules are working) and the zlib extension is available then
|
danielebarchiesi@0
|
4959 // create a gzipped version of this file. This file is served conditionally
|
danielebarchiesi@0
|
4960 // to browsers that accept gzip using .htaccess rules.
|
danielebarchiesi@0
|
4961 if (variable_get('js_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) {
|
danielebarchiesi@0
|
4962 if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($contents, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {
|
danielebarchiesi@0
|
4963 return FALSE;
|
danielebarchiesi@0
|
4964 }
|
danielebarchiesi@0
|
4965 }
|
danielebarchiesi@0
|
4966 $map[$key] = $uri;
|
danielebarchiesi@0
|
4967 variable_set('drupal_js_cache_files', $map);
|
danielebarchiesi@0
|
4968 }
|
danielebarchiesi@0
|
4969 return $uri;
|
danielebarchiesi@0
|
4970 }
|
danielebarchiesi@0
|
4971
|
danielebarchiesi@0
|
4972 /**
|
danielebarchiesi@0
|
4973 * Deletes old cached JavaScript files and variables.
|
danielebarchiesi@0
|
4974 */
|
danielebarchiesi@0
|
4975 function drupal_clear_js_cache() {
|
danielebarchiesi@0
|
4976 variable_del('javascript_parsed');
|
danielebarchiesi@0
|
4977 variable_del('drupal_js_cache_files');
|
danielebarchiesi@0
|
4978 file_scan_directory('public://js', '/.*/', array('callback' => 'drupal_delete_file_if_stale'));
|
danielebarchiesi@0
|
4979 }
|
danielebarchiesi@0
|
4980
|
danielebarchiesi@0
|
4981 /**
|
danielebarchiesi@0
|
4982 * Converts a PHP variable into its JavaScript equivalent.
|
danielebarchiesi@0
|
4983 *
|
danielebarchiesi@0
|
4984 * We use HTML-safe strings, with several characters escaped.
|
danielebarchiesi@0
|
4985 *
|
danielebarchiesi@0
|
4986 * @see drupal_json_decode()
|
danielebarchiesi@0
|
4987 * @see drupal_json_encode_helper()
|
danielebarchiesi@0
|
4988 * @ingroup php_wrappers
|
danielebarchiesi@0
|
4989 */
|
danielebarchiesi@0
|
4990 function drupal_json_encode($var) {
|
danielebarchiesi@0
|
4991 // The PHP version cannot change within a request.
|
danielebarchiesi@0
|
4992 static $php530;
|
danielebarchiesi@0
|
4993
|
danielebarchiesi@0
|
4994 if (!isset($php530)) {
|
danielebarchiesi@0
|
4995 $php530 = version_compare(PHP_VERSION, '5.3.0', '>=');
|
danielebarchiesi@0
|
4996 }
|
danielebarchiesi@0
|
4997
|
danielebarchiesi@0
|
4998 if ($php530) {
|
danielebarchiesi@0
|
4999 // Encode <, >, ', &, and " using the json_encode() options parameter.
|
danielebarchiesi@0
|
5000 return json_encode($var, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
|
danielebarchiesi@0
|
5001 }
|
danielebarchiesi@0
|
5002
|
danielebarchiesi@0
|
5003 // json_encode() escapes <, >, ', &, and " using its options parameter, but
|
danielebarchiesi@0
|
5004 // does not support this parameter prior to PHP 5.3.0. Use a helper instead.
|
danielebarchiesi@0
|
5005 include_once DRUPAL_ROOT . '/includes/json-encode.inc';
|
danielebarchiesi@0
|
5006 return drupal_json_encode_helper($var);
|
danielebarchiesi@0
|
5007 }
|
danielebarchiesi@0
|
5008
|
danielebarchiesi@0
|
5009 /**
|
danielebarchiesi@0
|
5010 * Converts an HTML-safe JSON string into its PHP equivalent.
|
danielebarchiesi@0
|
5011 *
|
danielebarchiesi@0
|
5012 * @see drupal_json_encode()
|
danielebarchiesi@0
|
5013 * @ingroup php_wrappers
|
danielebarchiesi@0
|
5014 */
|
danielebarchiesi@0
|
5015 function drupal_json_decode($var) {
|
danielebarchiesi@0
|
5016 return json_decode($var, TRUE);
|
danielebarchiesi@0
|
5017 }
|
danielebarchiesi@0
|
5018
|
danielebarchiesi@0
|
5019 /**
|
danielebarchiesi@0
|
5020 * Returns data in JSON format.
|
danielebarchiesi@0
|
5021 *
|
danielebarchiesi@0
|
5022 * This function should be used for JavaScript callback functions returning
|
danielebarchiesi@0
|
5023 * data in JSON format. It sets the header for JavaScript output.
|
danielebarchiesi@0
|
5024 *
|
danielebarchiesi@0
|
5025 * @param $var
|
danielebarchiesi@0
|
5026 * (optional) If set, the variable will be converted to JSON and output.
|
danielebarchiesi@0
|
5027 */
|
danielebarchiesi@0
|
5028 function drupal_json_output($var = NULL) {
|
danielebarchiesi@0
|
5029 // We are returning JSON, so tell the browser.
|
danielebarchiesi@0
|
5030 drupal_add_http_header('Content-Type', 'application/json');
|
danielebarchiesi@0
|
5031
|
danielebarchiesi@0
|
5032 if (isset($var)) {
|
danielebarchiesi@0
|
5033 echo drupal_json_encode($var);
|
danielebarchiesi@0
|
5034 }
|
danielebarchiesi@0
|
5035 }
|
danielebarchiesi@0
|
5036
|
danielebarchiesi@0
|
5037 /**
|
danielebarchiesi@0
|
5038 * Ensures the private key variable used to generate tokens is set.
|
danielebarchiesi@0
|
5039 *
|
danielebarchiesi@0
|
5040 * @return
|
danielebarchiesi@0
|
5041 * The private key.
|
danielebarchiesi@0
|
5042 */
|
danielebarchiesi@0
|
5043 function drupal_get_private_key() {
|
danielebarchiesi@0
|
5044 if (!($key = variable_get('drupal_private_key', 0))) {
|
danielebarchiesi@0
|
5045 $key = drupal_hash_base64(drupal_random_bytes(55));
|
danielebarchiesi@0
|
5046 variable_set('drupal_private_key', $key);
|
danielebarchiesi@0
|
5047 }
|
danielebarchiesi@0
|
5048 return $key;
|
danielebarchiesi@0
|
5049 }
|
danielebarchiesi@0
|
5050
|
danielebarchiesi@0
|
5051 /**
|
danielebarchiesi@0
|
5052 * Generates a token based on $value, the user session, and the private key.
|
danielebarchiesi@0
|
5053 *
|
danielebarchiesi@0
|
5054 * @param $value
|
danielebarchiesi@0
|
5055 * An additional value to base the token on.
|
danielebarchiesi@0
|
5056 *
|
danielebarchiesi@0
|
5057 * @return string
|
danielebarchiesi@0
|
5058 * A 43-character URL-safe token for validation, based on the user session ID,
|
danielebarchiesi@0
|
5059 * the hash salt provided from drupal_get_hash_salt(), and the
|
danielebarchiesi@0
|
5060 * 'drupal_private_key' configuration variable.
|
danielebarchiesi@0
|
5061 *
|
danielebarchiesi@0
|
5062 * @see drupal_get_hash_salt()
|
danielebarchiesi@0
|
5063 */
|
danielebarchiesi@0
|
5064 function drupal_get_token($value = '') {
|
danielebarchiesi@0
|
5065 return drupal_hmac_base64($value, session_id() . drupal_get_private_key() . drupal_get_hash_salt());
|
danielebarchiesi@0
|
5066 }
|
danielebarchiesi@0
|
5067
|
danielebarchiesi@0
|
5068 /**
|
danielebarchiesi@0
|
5069 * Validates a token based on $value, the user session, and the private key.
|
danielebarchiesi@0
|
5070 *
|
danielebarchiesi@0
|
5071 * @param $token
|
danielebarchiesi@0
|
5072 * The token to be validated.
|
danielebarchiesi@0
|
5073 * @param $value
|
danielebarchiesi@0
|
5074 * An additional value to base the token on.
|
danielebarchiesi@0
|
5075 * @param $skip_anonymous
|
danielebarchiesi@0
|
5076 * Set to true to skip token validation for anonymous users.
|
danielebarchiesi@0
|
5077 *
|
danielebarchiesi@0
|
5078 * @return
|
danielebarchiesi@0
|
5079 * True for a valid token, false for an invalid token. When $skip_anonymous
|
danielebarchiesi@0
|
5080 * is true, the return value will always be true for anonymous users.
|
danielebarchiesi@0
|
5081 */
|
danielebarchiesi@0
|
5082 function drupal_valid_token($token, $value = '', $skip_anonymous = FALSE) {
|
danielebarchiesi@0
|
5083 global $user;
|
danielebarchiesi@0
|
5084 return (($skip_anonymous && $user->uid == 0) || ($token == drupal_get_token($value)));
|
danielebarchiesi@0
|
5085 }
|
danielebarchiesi@0
|
5086
|
danielebarchiesi@0
|
5087 function _drupal_bootstrap_full() {
|
danielebarchiesi@0
|
5088 static $called = FALSE;
|
danielebarchiesi@0
|
5089
|
danielebarchiesi@0
|
5090 if ($called) {
|
danielebarchiesi@0
|
5091 return;
|
danielebarchiesi@0
|
5092 }
|
danielebarchiesi@0
|
5093 $called = TRUE;
|
danielebarchiesi@0
|
5094 require_once DRUPAL_ROOT . '/' . variable_get('path_inc', 'includes/path.inc');
|
danielebarchiesi@0
|
5095 require_once DRUPAL_ROOT . '/includes/theme.inc';
|
danielebarchiesi@0
|
5096 require_once DRUPAL_ROOT . '/includes/pager.inc';
|
danielebarchiesi@0
|
5097 require_once DRUPAL_ROOT . '/' . variable_get('menu_inc', 'includes/menu.inc');
|
danielebarchiesi@0
|
5098 require_once DRUPAL_ROOT . '/includes/tablesort.inc';
|
danielebarchiesi@0
|
5099 require_once DRUPAL_ROOT . '/includes/file.inc';
|
danielebarchiesi@0
|
5100 require_once DRUPAL_ROOT . '/includes/unicode.inc';
|
danielebarchiesi@0
|
5101 require_once DRUPAL_ROOT . '/includes/image.inc';
|
danielebarchiesi@0
|
5102 require_once DRUPAL_ROOT . '/includes/form.inc';
|
danielebarchiesi@0
|
5103 require_once DRUPAL_ROOT . '/includes/mail.inc';
|
danielebarchiesi@0
|
5104 require_once DRUPAL_ROOT . '/includes/actions.inc';
|
danielebarchiesi@0
|
5105 require_once DRUPAL_ROOT . '/includes/ajax.inc';
|
danielebarchiesi@0
|
5106 require_once DRUPAL_ROOT . '/includes/token.inc';
|
danielebarchiesi@0
|
5107 require_once DRUPAL_ROOT . '/includes/errors.inc';
|
danielebarchiesi@0
|
5108
|
danielebarchiesi@0
|
5109 // Detect string handling method
|
danielebarchiesi@0
|
5110 unicode_check();
|
danielebarchiesi@0
|
5111 // Undo magic quotes
|
danielebarchiesi@0
|
5112 fix_gpc_magic();
|
danielebarchiesi@0
|
5113 // Load all enabled modules
|
danielebarchiesi@0
|
5114 module_load_all();
|
danielebarchiesi@0
|
5115 // Make sure all stream wrappers are registered.
|
danielebarchiesi@0
|
5116 file_get_stream_wrappers();
|
danielebarchiesi@0
|
5117
|
danielebarchiesi@0
|
5118 $test_info = &$GLOBALS['drupal_test_info'];
|
danielebarchiesi@0
|
5119 if (!empty($test_info['in_child_site'])) {
|
danielebarchiesi@0
|
5120 // Running inside the simpletest child site, log fatal errors to test
|
danielebarchiesi@0
|
5121 // specific file directory.
|
danielebarchiesi@0
|
5122 ini_set('log_errors', 1);
|
danielebarchiesi@0
|
5123 ini_set('error_log', 'public://error.log');
|
danielebarchiesi@0
|
5124 }
|
danielebarchiesi@0
|
5125
|
danielebarchiesi@0
|
5126 // Initialize $_GET['q'] prior to invoking hook_init().
|
danielebarchiesi@0
|
5127 drupal_path_initialize();
|
danielebarchiesi@0
|
5128
|
danielebarchiesi@0
|
5129 // Let all modules take action before the menu system handles the request.
|
danielebarchiesi@0
|
5130 // We do not want this while running update.php.
|
danielebarchiesi@0
|
5131 if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {
|
danielebarchiesi@0
|
5132 // Prior to invoking hook_init(), initialize the theme (potentially a custom
|
danielebarchiesi@0
|
5133 // one for this page), so that:
|
danielebarchiesi@0
|
5134 // - Modules with hook_init() implementations that call theme() or
|
danielebarchiesi@0
|
5135 // theme_get_registry() don't initialize the incorrect theme.
|
danielebarchiesi@0
|
5136 // - The theme can have hook_*_alter() implementations affect page building
|
danielebarchiesi@0
|
5137 // (e.g., hook_form_alter(), hook_node_view_alter(), hook_page_alter()),
|
danielebarchiesi@0
|
5138 // ahead of when rendering starts.
|
danielebarchiesi@0
|
5139 menu_set_custom_theme();
|
danielebarchiesi@0
|
5140 drupal_theme_initialize();
|
danielebarchiesi@0
|
5141 module_invoke_all('init');
|
danielebarchiesi@0
|
5142 }
|
danielebarchiesi@0
|
5143 }
|
danielebarchiesi@0
|
5144
|
danielebarchiesi@0
|
5145 /**
|
danielebarchiesi@0
|
5146 * Stores the current page in the cache.
|
danielebarchiesi@0
|
5147 *
|
danielebarchiesi@0
|
5148 * If page_compression is enabled, a gzipped version of the page is stored in
|
danielebarchiesi@0
|
5149 * the cache to avoid compressing the output on each request. The cache entry
|
danielebarchiesi@0
|
5150 * is unzipped in the relatively rare event that the page is requested by a
|
danielebarchiesi@0
|
5151 * client without gzip support.
|
danielebarchiesi@0
|
5152 *
|
danielebarchiesi@0
|
5153 * Page compression requires the PHP zlib extension
|
danielebarchiesi@0
|
5154 * (http://php.net/manual/en/ref.zlib.php).
|
danielebarchiesi@0
|
5155 *
|
danielebarchiesi@0
|
5156 * @see drupal_page_header()
|
danielebarchiesi@0
|
5157 */
|
danielebarchiesi@0
|
5158 function drupal_page_set_cache() {
|
danielebarchiesi@0
|
5159 global $base_root;
|
danielebarchiesi@0
|
5160
|
danielebarchiesi@0
|
5161 if (drupal_page_is_cacheable()) {
|
danielebarchiesi@0
|
5162 $cache = (object) array(
|
danielebarchiesi@0
|
5163 'cid' => $base_root . request_uri(),
|
danielebarchiesi@0
|
5164 'data' => array(
|
danielebarchiesi@0
|
5165 'path' => $_GET['q'],
|
danielebarchiesi@0
|
5166 'body' => ob_get_clean(),
|
danielebarchiesi@0
|
5167 'title' => drupal_get_title(),
|
danielebarchiesi@0
|
5168 'headers' => array(),
|
danielebarchiesi@0
|
5169 ),
|
danielebarchiesi@0
|
5170 'expire' => CACHE_TEMPORARY,
|
danielebarchiesi@0
|
5171 'created' => REQUEST_TIME,
|
danielebarchiesi@0
|
5172 );
|
danielebarchiesi@0
|
5173
|
danielebarchiesi@0
|
5174 // Restore preferred header names based on the lower-case names returned
|
danielebarchiesi@0
|
5175 // by drupal_get_http_header().
|
danielebarchiesi@0
|
5176 $header_names = _drupal_set_preferred_header_name();
|
danielebarchiesi@0
|
5177 foreach (drupal_get_http_header() as $name_lower => $value) {
|
danielebarchiesi@0
|
5178 $cache->data['headers'][$header_names[$name_lower]] = $value;
|
danielebarchiesi@0
|
5179 if ($name_lower == 'expires') {
|
danielebarchiesi@0
|
5180 // Use the actual timestamp from an Expires header if available.
|
danielebarchiesi@0
|
5181 $cache->expire = strtotime($value);
|
danielebarchiesi@0
|
5182 }
|
danielebarchiesi@0
|
5183 }
|
danielebarchiesi@0
|
5184
|
danielebarchiesi@0
|
5185 if ($cache->data['body']) {
|
danielebarchiesi@0
|
5186 if (variable_get('page_compression', TRUE) && extension_loaded('zlib')) {
|
danielebarchiesi@0
|
5187 $cache->data['body'] = gzencode($cache->data['body'], 9, FORCE_GZIP);
|
danielebarchiesi@0
|
5188 }
|
danielebarchiesi@0
|
5189 cache_set($cache->cid, $cache->data, 'cache_page', $cache->expire);
|
danielebarchiesi@0
|
5190 }
|
danielebarchiesi@0
|
5191 return $cache;
|
danielebarchiesi@0
|
5192 }
|
danielebarchiesi@0
|
5193 }
|
danielebarchiesi@0
|
5194
|
danielebarchiesi@0
|
5195 /**
|
danielebarchiesi@0
|
5196 * Executes a cron run when called.
|
danielebarchiesi@0
|
5197 *
|
danielebarchiesi@0
|
5198 * Do not call this function from a test. Use $this->cronRun() instead.
|
danielebarchiesi@0
|
5199 *
|
danielebarchiesi@0
|
5200 * @return
|
danielebarchiesi@0
|
5201 * TRUE if cron ran successfully.
|
danielebarchiesi@0
|
5202 */
|
danielebarchiesi@0
|
5203 function drupal_cron_run() {
|
danielebarchiesi@0
|
5204 // Allow execution to continue even if the request gets canceled.
|
danielebarchiesi@0
|
5205 @ignore_user_abort(TRUE);
|
danielebarchiesi@0
|
5206
|
danielebarchiesi@0
|
5207 // Prevent session information from being saved while cron is running.
|
danielebarchiesi@0
|
5208 $original_session_saving = drupal_save_session();
|
danielebarchiesi@0
|
5209 drupal_save_session(FALSE);
|
danielebarchiesi@0
|
5210
|
danielebarchiesi@0
|
5211 // Force the current user to anonymous to ensure consistent permissions on
|
danielebarchiesi@0
|
5212 // cron runs.
|
danielebarchiesi@0
|
5213 $original_user = $GLOBALS['user'];
|
danielebarchiesi@0
|
5214 $GLOBALS['user'] = drupal_anonymous_user();
|
danielebarchiesi@0
|
5215
|
danielebarchiesi@0
|
5216 // Try to allocate enough time to run all the hook_cron implementations.
|
danielebarchiesi@0
|
5217 drupal_set_time_limit(240);
|
danielebarchiesi@0
|
5218
|
danielebarchiesi@0
|
5219 $return = FALSE;
|
danielebarchiesi@0
|
5220 // Grab the defined cron queues.
|
danielebarchiesi@0
|
5221 $queues = module_invoke_all('cron_queue_info');
|
danielebarchiesi@0
|
5222 drupal_alter('cron_queue_info', $queues);
|
danielebarchiesi@0
|
5223
|
danielebarchiesi@0
|
5224 // Try to acquire cron lock.
|
danielebarchiesi@0
|
5225 if (!lock_acquire('cron', 240.0)) {
|
danielebarchiesi@0
|
5226 // Cron is still running normally.
|
danielebarchiesi@0
|
5227 watchdog('cron', 'Attempting to re-run cron while it is already running.', array(), WATCHDOG_WARNING);
|
danielebarchiesi@0
|
5228 }
|
danielebarchiesi@0
|
5229 else {
|
danielebarchiesi@0
|
5230 // Make sure every queue exists. There is no harm in trying to recreate an
|
danielebarchiesi@0
|
5231 // existing queue.
|
danielebarchiesi@0
|
5232 foreach ($queues as $queue_name => $info) {
|
danielebarchiesi@0
|
5233 DrupalQueue::get($queue_name)->createQueue();
|
danielebarchiesi@0
|
5234 }
|
danielebarchiesi@0
|
5235 // Register shutdown callback.
|
danielebarchiesi@0
|
5236 drupal_register_shutdown_function('drupal_cron_cleanup');
|
danielebarchiesi@0
|
5237
|
danielebarchiesi@0
|
5238 // Iterate through the modules calling their cron handlers (if any):
|
danielebarchiesi@0
|
5239 foreach (module_implements('cron') as $module) {
|
danielebarchiesi@0
|
5240 // Do not let an exception thrown by one module disturb another.
|
danielebarchiesi@0
|
5241 try {
|
danielebarchiesi@0
|
5242 module_invoke($module, 'cron');
|
danielebarchiesi@0
|
5243 }
|
danielebarchiesi@0
|
5244 catch (Exception $e) {
|
danielebarchiesi@0
|
5245 watchdog_exception('cron', $e);
|
danielebarchiesi@0
|
5246 }
|
danielebarchiesi@0
|
5247 }
|
danielebarchiesi@0
|
5248
|
danielebarchiesi@0
|
5249 // Record cron time.
|
danielebarchiesi@0
|
5250 variable_set('cron_last', REQUEST_TIME);
|
danielebarchiesi@0
|
5251 watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE);
|
danielebarchiesi@0
|
5252
|
danielebarchiesi@0
|
5253 // Release cron lock.
|
danielebarchiesi@0
|
5254 lock_release('cron');
|
danielebarchiesi@0
|
5255
|
danielebarchiesi@0
|
5256 // Return TRUE so other functions can check if it did run successfully
|
danielebarchiesi@0
|
5257 $return = TRUE;
|
danielebarchiesi@0
|
5258 }
|
danielebarchiesi@0
|
5259
|
danielebarchiesi@0
|
5260 foreach ($queues as $queue_name => $info) {
|
danielebarchiesi@0
|
5261 $function = $info['worker callback'];
|
danielebarchiesi@0
|
5262 $end = time() + (isset($info['time']) ? $info['time'] : 15);
|
danielebarchiesi@0
|
5263 $queue = DrupalQueue::get($queue_name);
|
danielebarchiesi@0
|
5264 while (time() < $end && ($item = $queue->claimItem())) {
|
danielebarchiesi@0
|
5265 $function($item->data);
|
danielebarchiesi@0
|
5266 $queue->deleteItem($item);
|
danielebarchiesi@0
|
5267 }
|
danielebarchiesi@0
|
5268 }
|
danielebarchiesi@0
|
5269 // Restore the user.
|
danielebarchiesi@0
|
5270 $GLOBALS['user'] = $original_user;
|
danielebarchiesi@0
|
5271 drupal_save_session($original_session_saving);
|
danielebarchiesi@0
|
5272
|
danielebarchiesi@0
|
5273 return $return;
|
danielebarchiesi@0
|
5274 }
|
danielebarchiesi@0
|
5275
|
danielebarchiesi@0
|
5276 /**
|
danielebarchiesi@0
|
5277 * Shutdown function: Performs cron cleanup.
|
danielebarchiesi@0
|
5278 *
|
danielebarchiesi@0
|
5279 * @see drupal_cron_run()
|
danielebarchiesi@0
|
5280 * @see drupal_register_shutdown_function()
|
danielebarchiesi@0
|
5281 */
|
danielebarchiesi@0
|
5282 function drupal_cron_cleanup() {
|
danielebarchiesi@0
|
5283 // See if the semaphore is still locked.
|
danielebarchiesi@0
|
5284 if (variable_get('cron_semaphore', FALSE)) {
|
danielebarchiesi@0
|
5285 watchdog('cron', 'Cron run exceeded the time limit and was aborted.', array(), WATCHDOG_WARNING);
|
danielebarchiesi@0
|
5286
|
danielebarchiesi@0
|
5287 // Release cron semaphore.
|
danielebarchiesi@0
|
5288 variable_del('cron_semaphore');
|
danielebarchiesi@0
|
5289 }
|
danielebarchiesi@0
|
5290 }
|
danielebarchiesi@0
|
5291
|
danielebarchiesi@0
|
5292 /**
|
danielebarchiesi@0
|
5293 * Returns information about system object files (modules, themes, etc.).
|
danielebarchiesi@0
|
5294 *
|
danielebarchiesi@0
|
5295 * This function is used to find all or some system object files (module files,
|
danielebarchiesi@0
|
5296 * theme files, etc.) that exist on the site. It searches in several locations,
|
danielebarchiesi@0
|
5297 * depending on what type of object you are looking for. For instance, if you
|
danielebarchiesi@0
|
5298 * are looking for modules and call:
|
danielebarchiesi@0
|
5299 * @code
|
danielebarchiesi@0
|
5300 * drupal_system_listing("/\.module$/", "modules", 'name', 0);
|
danielebarchiesi@0
|
5301 * @endcode
|
danielebarchiesi@0
|
5302 * this function will search the site-wide modules directory (i.e., /modules/),
|
danielebarchiesi@0
|
5303 * your installation profile's directory (i.e.,
|
danielebarchiesi@0
|
5304 * /profiles/your_site_profile/modules/), the all-sites directory (i.e.,
|
danielebarchiesi@0
|
5305 * /sites/all/modules/), and your site-specific directory (i.e.,
|
danielebarchiesi@0
|
5306 * /sites/your_site_dir/modules/), in that order, and return information about
|
danielebarchiesi@0
|
5307 * all of the files ending in .module in those directories.
|
danielebarchiesi@0
|
5308 *
|
danielebarchiesi@0
|
5309 * The information is returned in an associative array, which can be keyed on
|
danielebarchiesi@0
|
5310 * the file name ($key = 'filename'), the file name without the extension ($key
|
danielebarchiesi@0
|
5311 * = 'name'), or the full file stream URI ($key = 'uri'). If you use a key of
|
danielebarchiesi@0
|
5312 * 'filename' or 'name', files found later in the search will take precedence
|
danielebarchiesi@0
|
5313 * over files found earlier (unless they belong to a module or theme not
|
danielebarchiesi@0
|
5314 * compatible with Drupal core); if you choose a key of 'uri', you will get all
|
danielebarchiesi@0
|
5315 * files found.
|
danielebarchiesi@0
|
5316 *
|
danielebarchiesi@0
|
5317 * @param string $mask
|
danielebarchiesi@0
|
5318 * The preg_match() regular expression for the files to find.
|
danielebarchiesi@0
|
5319 * @param string $directory
|
danielebarchiesi@0
|
5320 * The subdirectory name in which the files are found. For example,
|
danielebarchiesi@0
|
5321 * 'modules' will search in sub-directories of the top-level /modules
|
danielebarchiesi@0
|
5322 * directory, sub-directories of /sites/all/modules/, etc.
|
danielebarchiesi@0
|
5323 * @param string $key
|
danielebarchiesi@0
|
5324 * The key to be used for the associative array returned. Possible values are
|
danielebarchiesi@0
|
5325 * 'uri', for the file's URI; 'filename', for the basename of the file; and
|
danielebarchiesi@0
|
5326 * 'name' for the name of the file without the extension. If you choose 'name'
|
danielebarchiesi@0
|
5327 * or 'filename', only the highest-precedence file will be returned.
|
danielebarchiesi@0
|
5328 * @param int $min_depth
|
danielebarchiesi@0
|
5329 * Minimum depth of directories to return files from, relative to each
|
danielebarchiesi@0
|
5330 * directory searched. For instance, a minimum depth of 2 would find modules
|
danielebarchiesi@0
|
5331 * inside /modules/node/tests, but not modules directly in /modules/node.
|
danielebarchiesi@0
|
5332 *
|
danielebarchiesi@0
|
5333 * @return array
|
danielebarchiesi@0
|
5334 * An associative array of file objects, keyed on the chosen key. Each element
|
danielebarchiesi@0
|
5335 * in the array is an object containing file information, with properties:
|
danielebarchiesi@0
|
5336 * - 'uri': Full URI of the file.
|
danielebarchiesi@0
|
5337 * - 'filename': File name.
|
danielebarchiesi@0
|
5338 * - 'name': Name of file without the extension.
|
danielebarchiesi@0
|
5339 */
|
danielebarchiesi@0
|
5340 function drupal_system_listing($mask, $directory, $key = 'name', $min_depth = 1) {
|
danielebarchiesi@0
|
5341 $config = conf_path();
|
danielebarchiesi@0
|
5342
|
danielebarchiesi@0
|
5343 $searchdir = array($directory);
|
danielebarchiesi@0
|
5344 $files = array();
|
danielebarchiesi@0
|
5345
|
danielebarchiesi@0
|
5346 // The 'profiles' directory contains pristine collections of modules and
|
danielebarchiesi@0
|
5347 // themes as organized by a distribution. It is pristine in the same way
|
danielebarchiesi@0
|
5348 // that /modules is pristine for core; users should avoid changing anything
|
danielebarchiesi@0
|
5349 // there in favor of sites/all or sites/<domain> directories.
|
danielebarchiesi@0
|
5350 $profiles = array();
|
danielebarchiesi@0
|
5351 $profile = drupal_get_profile();
|
danielebarchiesi@0
|
5352 // For SimpleTest to be able to test modules packaged together with a
|
danielebarchiesi@0
|
5353 // distribution we need to include the profile of the parent site (in which
|
danielebarchiesi@0
|
5354 // test runs are triggered).
|
danielebarchiesi@0
|
5355 if (drupal_valid_test_ua()) {
|
danielebarchiesi@0
|
5356 $testing_profile = variable_get('simpletest_parent_profile', FALSE);
|
danielebarchiesi@0
|
5357 if ($testing_profile && $testing_profile != $profile) {
|
danielebarchiesi@0
|
5358 $profiles[] = $testing_profile;
|
danielebarchiesi@0
|
5359 }
|
danielebarchiesi@0
|
5360 }
|
danielebarchiesi@0
|
5361 // In case both profile directories contain the same extension, the actual
|
danielebarchiesi@0
|
5362 // profile always has precedence.
|
danielebarchiesi@0
|
5363 $profiles[] = $profile;
|
danielebarchiesi@0
|
5364 foreach ($profiles as $profile) {
|
danielebarchiesi@0
|
5365 if (file_exists("profiles/$profile/$directory")) {
|
danielebarchiesi@0
|
5366 $searchdir[] = "profiles/$profile/$directory";
|
danielebarchiesi@0
|
5367 }
|
danielebarchiesi@0
|
5368 }
|
danielebarchiesi@0
|
5369
|
danielebarchiesi@0
|
5370 // Always search sites/all/* as well as the global directories.
|
danielebarchiesi@0
|
5371 $searchdir[] = 'sites/all/' . $directory;
|
danielebarchiesi@0
|
5372
|
danielebarchiesi@0
|
5373 if (file_exists("$config/$directory")) {
|
danielebarchiesi@0
|
5374 $searchdir[] = "$config/$directory";
|
danielebarchiesi@0
|
5375 }
|
danielebarchiesi@0
|
5376
|
danielebarchiesi@0
|
5377 // Get current list of items.
|
danielebarchiesi@0
|
5378 if (!function_exists('file_scan_directory')) {
|
danielebarchiesi@0
|
5379 require_once DRUPAL_ROOT . '/includes/file.inc';
|
danielebarchiesi@0
|
5380 }
|
danielebarchiesi@0
|
5381 foreach ($searchdir as $dir) {
|
danielebarchiesi@0
|
5382 $files_to_add = file_scan_directory($dir, $mask, array('key' => $key, 'min_depth' => $min_depth));
|
danielebarchiesi@0
|
5383
|
danielebarchiesi@0
|
5384 // Duplicate files found in later search directories take precedence over
|
danielebarchiesi@0
|
5385 // earlier ones, so we want them to overwrite keys in our resulting
|
danielebarchiesi@0
|
5386 // $files array.
|
danielebarchiesi@0
|
5387 // The exception to this is if the later file is from a module or theme not
|
danielebarchiesi@0
|
5388 // compatible with Drupal core. This may occur during upgrades of Drupal
|
danielebarchiesi@0
|
5389 // core when new modules exist in core while older contrib modules with the
|
danielebarchiesi@0
|
5390 // same name exist in a directory such as sites/all/modules/.
|
danielebarchiesi@0
|
5391 foreach (array_intersect_key($files_to_add, $files) as $file_key => $file) {
|
danielebarchiesi@0
|
5392 // If it has no info file, then we just behave liberally and accept the
|
danielebarchiesi@0
|
5393 // new resource on the list for merging.
|
danielebarchiesi@0
|
5394 if (file_exists($info_file = dirname($file->uri) . '/' . $file->name . '.info')) {
|
danielebarchiesi@0
|
5395 // Get the .info file for the module or theme this file belongs to.
|
danielebarchiesi@0
|
5396 $info = drupal_parse_info_file($info_file);
|
danielebarchiesi@0
|
5397
|
danielebarchiesi@0
|
5398 // If the module or theme is incompatible with Drupal core, remove it
|
danielebarchiesi@0
|
5399 // from the array for the current search directory, so it is not
|
danielebarchiesi@0
|
5400 // overwritten when merged with the $files array.
|
danielebarchiesi@0
|
5401 if (isset($info['core']) && $info['core'] != DRUPAL_CORE_COMPATIBILITY) {
|
danielebarchiesi@0
|
5402 unset($files_to_add[$file_key]);
|
danielebarchiesi@0
|
5403 }
|
danielebarchiesi@0
|
5404 }
|
danielebarchiesi@0
|
5405 }
|
danielebarchiesi@0
|
5406 $files = array_merge($files, $files_to_add);
|
danielebarchiesi@0
|
5407 }
|
danielebarchiesi@0
|
5408
|
danielebarchiesi@0
|
5409 return $files;
|
danielebarchiesi@0
|
5410 }
|
danielebarchiesi@0
|
5411
|
danielebarchiesi@0
|
5412 /**
|
danielebarchiesi@0
|
5413 * Sets the main page content value for later use.
|
danielebarchiesi@0
|
5414 *
|
danielebarchiesi@0
|
5415 * Given the nature of the Drupal page handling, this will be called once with
|
danielebarchiesi@0
|
5416 * a string or array. We store that and return it later as the block is being
|
danielebarchiesi@0
|
5417 * displayed.
|
danielebarchiesi@0
|
5418 *
|
danielebarchiesi@0
|
5419 * @param $content
|
danielebarchiesi@0
|
5420 * A string or renderable array representing the body of the page.
|
danielebarchiesi@0
|
5421 *
|
danielebarchiesi@0
|
5422 * @return
|
danielebarchiesi@0
|
5423 * If called without $content, a renderable array representing the body of
|
danielebarchiesi@0
|
5424 * the page.
|
danielebarchiesi@0
|
5425 */
|
danielebarchiesi@0
|
5426 function drupal_set_page_content($content = NULL) {
|
danielebarchiesi@0
|
5427 $content_block = &drupal_static(__FUNCTION__, NULL);
|
danielebarchiesi@0
|
5428 $main_content_display = &drupal_static('system_main_content_added', FALSE);
|
danielebarchiesi@0
|
5429
|
danielebarchiesi@0
|
5430 if (!empty($content)) {
|
danielebarchiesi@0
|
5431 $content_block = (is_array($content) ? $content : array('main' => array('#markup' => $content)));
|
danielebarchiesi@0
|
5432 }
|
danielebarchiesi@0
|
5433 else {
|
danielebarchiesi@0
|
5434 // Indicate that the main content has been requested. We assume that
|
danielebarchiesi@0
|
5435 // the module requesting the content will be adding it to the page.
|
danielebarchiesi@0
|
5436 // A module can indicate that it does not handle the content by setting
|
danielebarchiesi@0
|
5437 // the static variable back to FALSE after calling this function.
|
danielebarchiesi@0
|
5438 $main_content_display = TRUE;
|
danielebarchiesi@0
|
5439 return $content_block;
|
danielebarchiesi@0
|
5440 }
|
danielebarchiesi@0
|
5441 }
|
danielebarchiesi@0
|
5442
|
danielebarchiesi@0
|
5443 /**
|
danielebarchiesi@0
|
5444 * #pre_render callback to render #browsers into #prefix and #suffix.
|
danielebarchiesi@0
|
5445 *
|
danielebarchiesi@0
|
5446 * @param $elements
|
danielebarchiesi@0
|
5447 * A render array with a '#browsers' property. The '#browsers' property can
|
danielebarchiesi@0
|
5448 * contain any or all of the following keys:
|
danielebarchiesi@0
|
5449 * - 'IE': If FALSE, the element is not rendered by Internet Explorer. If
|
danielebarchiesi@0
|
5450 * TRUE, the element is rendered by Internet Explorer. Can also be a string
|
danielebarchiesi@0
|
5451 * containing an expression for Internet Explorer to evaluate as part of a
|
danielebarchiesi@0
|
5452 * conditional comment. For example, this can be set to 'lt IE 7' for the
|
danielebarchiesi@0
|
5453 * element to be rendered in Internet Explorer 6, but not in Internet
|
danielebarchiesi@0
|
5454 * Explorer 7 or higher. Defaults to TRUE.
|
danielebarchiesi@0
|
5455 * - '!IE': If FALSE, the element is not rendered by browsers other than
|
danielebarchiesi@0
|
5456 * Internet Explorer. If TRUE, the element is rendered by those browsers.
|
danielebarchiesi@0
|
5457 * Defaults to TRUE.
|
danielebarchiesi@0
|
5458 * Examples:
|
danielebarchiesi@0
|
5459 * - To render an element in all browsers, '#browsers' can be left out or set
|
danielebarchiesi@0
|
5460 * to array('IE' => TRUE, '!IE' => TRUE).
|
danielebarchiesi@0
|
5461 * - To render an element in Internet Explorer only, '#browsers' can be set
|
danielebarchiesi@0
|
5462 * to array('!IE' => FALSE).
|
danielebarchiesi@0
|
5463 * - To render an element in Internet Explorer 6 only, '#browsers' can be set
|
danielebarchiesi@0
|
5464 * to array('IE' => 'lt IE 7', '!IE' => FALSE).
|
danielebarchiesi@0
|
5465 * - To render an element in Internet Explorer 8 and higher and in all other
|
danielebarchiesi@0
|
5466 * browsers, '#browsers' can be set to array('IE' => 'gte IE 8').
|
danielebarchiesi@0
|
5467 *
|
danielebarchiesi@0
|
5468 * @return
|
danielebarchiesi@0
|
5469 * The passed-in element with markup for conditional comments potentially
|
danielebarchiesi@0
|
5470 * added to '#prefix' and '#suffix'.
|
danielebarchiesi@0
|
5471 */
|
danielebarchiesi@0
|
5472 function drupal_pre_render_conditional_comments($elements) {
|
danielebarchiesi@0
|
5473 $browsers = isset($elements['#browsers']) ? $elements['#browsers'] : array();
|
danielebarchiesi@0
|
5474 $browsers += array(
|
danielebarchiesi@0
|
5475 'IE' => TRUE,
|
danielebarchiesi@0
|
5476 '!IE' => TRUE,
|
danielebarchiesi@0
|
5477 );
|
danielebarchiesi@0
|
5478
|
danielebarchiesi@0
|
5479 // If rendering in all browsers, no need for conditional comments.
|
danielebarchiesi@0
|
5480 if ($browsers['IE'] === TRUE && $browsers['!IE']) {
|
danielebarchiesi@0
|
5481 return $elements;
|
danielebarchiesi@0
|
5482 }
|
danielebarchiesi@0
|
5483
|
danielebarchiesi@0
|
5484 // Determine the conditional comment expression for Internet Explorer to
|
danielebarchiesi@0
|
5485 // evaluate.
|
danielebarchiesi@0
|
5486 if ($browsers['IE'] === TRUE) {
|
danielebarchiesi@0
|
5487 $expression = 'IE';
|
danielebarchiesi@0
|
5488 }
|
danielebarchiesi@0
|
5489 elseif ($browsers['IE'] === FALSE) {
|
danielebarchiesi@0
|
5490 $expression = '!IE';
|
danielebarchiesi@0
|
5491 }
|
danielebarchiesi@0
|
5492 else {
|
danielebarchiesi@0
|
5493 $expression = $browsers['IE'];
|
danielebarchiesi@0
|
5494 }
|
danielebarchiesi@0
|
5495
|
danielebarchiesi@0
|
5496 // Wrap the element's potentially existing #prefix and #suffix properties with
|
danielebarchiesi@0
|
5497 // conditional comment markup. The conditional comment expression is evaluated
|
danielebarchiesi@0
|
5498 // by Internet Explorer only. To control the rendering by other browsers,
|
danielebarchiesi@0
|
5499 // either the "downlevel-hidden" or "downlevel-revealed" technique must be
|
danielebarchiesi@0
|
5500 // used. See http://en.wikipedia.org/wiki/Conditional_comment for details.
|
danielebarchiesi@0
|
5501 $elements += array(
|
danielebarchiesi@0
|
5502 '#prefix' => '',
|
danielebarchiesi@0
|
5503 '#suffix' => '',
|
danielebarchiesi@0
|
5504 );
|
danielebarchiesi@0
|
5505 if (!$browsers['!IE']) {
|
danielebarchiesi@0
|
5506 // "downlevel-hidden".
|
danielebarchiesi@0
|
5507 $elements['#prefix'] = "\n<!--[if $expression]>\n" . $elements['#prefix'];
|
danielebarchiesi@0
|
5508 $elements['#suffix'] .= "<![endif]-->\n";
|
danielebarchiesi@0
|
5509 }
|
danielebarchiesi@0
|
5510 else {
|
danielebarchiesi@0
|
5511 // "downlevel-revealed".
|
danielebarchiesi@0
|
5512 $elements['#prefix'] = "\n<!--[if $expression]><!-->\n" . $elements['#prefix'];
|
danielebarchiesi@0
|
5513 $elements['#suffix'] .= "<!--<![endif]-->\n";
|
danielebarchiesi@0
|
5514 }
|
danielebarchiesi@0
|
5515
|
danielebarchiesi@0
|
5516 return $elements;
|
danielebarchiesi@0
|
5517 }
|
danielebarchiesi@0
|
5518
|
danielebarchiesi@0
|
5519 /**
|
danielebarchiesi@0
|
5520 * #pre_render callback to render a link into #markup.
|
danielebarchiesi@0
|
5521 *
|
danielebarchiesi@0
|
5522 * Doing so during pre_render gives modules a chance to alter the link parts.
|
danielebarchiesi@0
|
5523 *
|
danielebarchiesi@0
|
5524 * @param $elements
|
danielebarchiesi@0
|
5525 * A structured array whose keys form the arguments to l():
|
danielebarchiesi@0
|
5526 * - #title: The link text to pass as argument to l().
|
danielebarchiesi@0
|
5527 * - #href: The URL path component to pass as argument to l().
|
danielebarchiesi@0
|
5528 * - #options: (optional) An array of options to pass to l().
|
danielebarchiesi@0
|
5529 *
|
danielebarchiesi@0
|
5530 * @return
|
danielebarchiesi@0
|
5531 * The passed-in elements containing a rendered link in '#markup'.
|
danielebarchiesi@0
|
5532 */
|
danielebarchiesi@0
|
5533 function drupal_pre_render_link($element) {
|
danielebarchiesi@0
|
5534 // By default, link options to pass to l() are normally set in #options.
|
danielebarchiesi@0
|
5535 $element += array('#options' => array());
|
danielebarchiesi@0
|
5536 // However, within the scope of renderable elements, #attributes is a valid
|
danielebarchiesi@0
|
5537 // way to specify attributes, too. Take them into account, but do not override
|
danielebarchiesi@0
|
5538 // attributes from #options.
|
danielebarchiesi@0
|
5539 if (isset($element['#attributes'])) {
|
danielebarchiesi@0
|
5540 $element['#options'] += array('attributes' => array());
|
danielebarchiesi@0
|
5541 $element['#options']['attributes'] += $element['#attributes'];
|
danielebarchiesi@0
|
5542 }
|
danielebarchiesi@0
|
5543
|
danielebarchiesi@0
|
5544 // This #pre_render callback can be invoked from inside or outside of a Form
|
danielebarchiesi@0
|
5545 // API context, and depending on that, a HTML ID may be already set in
|
danielebarchiesi@0
|
5546 // different locations. #options should have precedence over Form API's #id.
|
danielebarchiesi@0
|
5547 // #attributes have been taken over into #options above already.
|
danielebarchiesi@0
|
5548 if (isset($element['#options']['attributes']['id'])) {
|
danielebarchiesi@0
|
5549 $element['#id'] = $element['#options']['attributes']['id'];
|
danielebarchiesi@0
|
5550 }
|
danielebarchiesi@0
|
5551 elseif (isset($element['#id'])) {
|
danielebarchiesi@0
|
5552 $element['#options']['attributes']['id'] = $element['#id'];
|
danielebarchiesi@0
|
5553 }
|
danielebarchiesi@0
|
5554
|
danielebarchiesi@0
|
5555 // Conditionally invoke ajax_pre_render_element(), if #ajax is set.
|
danielebarchiesi@0
|
5556 if (isset($element['#ajax']) && !isset($element['#ajax_processed'])) {
|
danielebarchiesi@0
|
5557 // If no HTML ID was found above, automatically create one.
|
danielebarchiesi@0
|
5558 if (!isset($element['#id'])) {
|
danielebarchiesi@0
|
5559 $element['#id'] = $element['#options']['attributes']['id'] = drupal_html_id('ajax-link');
|
danielebarchiesi@0
|
5560 }
|
danielebarchiesi@0
|
5561 // If #ajax['path] was not specified, use the href as Ajax request URL.
|
danielebarchiesi@0
|
5562 if (!isset($element['#ajax']['path'])) {
|
danielebarchiesi@0
|
5563 $element['#ajax']['path'] = $element['#href'];
|
danielebarchiesi@0
|
5564 $element['#ajax']['options'] = $element['#options'];
|
danielebarchiesi@0
|
5565 }
|
danielebarchiesi@0
|
5566 $element = ajax_pre_render_element($element);
|
danielebarchiesi@0
|
5567 }
|
danielebarchiesi@0
|
5568
|
danielebarchiesi@0
|
5569 $element['#markup'] = l($element['#title'], $element['#href'], $element['#options']);
|
danielebarchiesi@0
|
5570 return $element;
|
danielebarchiesi@0
|
5571 }
|
danielebarchiesi@0
|
5572
|
danielebarchiesi@0
|
5573 /**
|
danielebarchiesi@0
|
5574 * #pre_render callback that collects child links into a single array.
|
danielebarchiesi@0
|
5575 *
|
danielebarchiesi@0
|
5576 * This function can be added as a pre_render callback for a renderable array,
|
danielebarchiesi@0
|
5577 * usually one which will be themed by theme_links(). It iterates through all
|
danielebarchiesi@0
|
5578 * unrendered children of the element, collects any #links properties it finds,
|
danielebarchiesi@0
|
5579 * merges them into the parent element's #links array, and prevents those
|
danielebarchiesi@0
|
5580 * children from being rendered separately.
|
danielebarchiesi@0
|
5581 *
|
danielebarchiesi@0
|
5582 * The purpose of this is to allow links to be logically grouped into related
|
danielebarchiesi@0
|
5583 * categories, so that each child group can be rendered as its own list of
|
danielebarchiesi@0
|
5584 * links if drupal_render() is called on it, but calling drupal_render() on the
|
danielebarchiesi@0
|
5585 * parent element will still produce a single list containing all the remaining
|
danielebarchiesi@0
|
5586 * links, regardless of what group they were in.
|
danielebarchiesi@0
|
5587 *
|
danielebarchiesi@0
|
5588 * A typical example comes from node links, which are stored in a renderable
|
danielebarchiesi@0
|
5589 * array similar to this:
|
danielebarchiesi@0
|
5590 * @code
|
danielebarchiesi@0
|
5591 * $node->content['links'] = array(
|
danielebarchiesi@0
|
5592 * '#theme' => 'links__node',
|
danielebarchiesi@0
|
5593 * '#pre_render' => array('drupal_pre_render_links'),
|
danielebarchiesi@0
|
5594 * 'comment' => array(
|
danielebarchiesi@0
|
5595 * '#theme' => 'links__node__comment',
|
danielebarchiesi@0
|
5596 * '#links' => array(
|
danielebarchiesi@0
|
5597 * // An array of links associated with node comments, suitable for
|
danielebarchiesi@0
|
5598 * // passing in to theme_links().
|
danielebarchiesi@0
|
5599 * ),
|
danielebarchiesi@0
|
5600 * ),
|
danielebarchiesi@0
|
5601 * 'statistics' => array(
|
danielebarchiesi@0
|
5602 * '#theme' => 'links__node__statistics',
|
danielebarchiesi@0
|
5603 * '#links' => array(
|
danielebarchiesi@0
|
5604 * // An array of links associated with node statistics, suitable for
|
danielebarchiesi@0
|
5605 * // passing in to theme_links().
|
danielebarchiesi@0
|
5606 * ),
|
danielebarchiesi@0
|
5607 * ),
|
danielebarchiesi@0
|
5608 * 'translation' => array(
|
danielebarchiesi@0
|
5609 * '#theme' => 'links__node__translation',
|
danielebarchiesi@0
|
5610 * '#links' => array(
|
danielebarchiesi@0
|
5611 * // An array of links associated with node translation, suitable for
|
danielebarchiesi@0
|
5612 * // passing in to theme_links().
|
danielebarchiesi@0
|
5613 * ),
|
danielebarchiesi@0
|
5614 * ),
|
danielebarchiesi@0
|
5615 * );
|
danielebarchiesi@0
|
5616 * @endcode
|
danielebarchiesi@0
|
5617 *
|
danielebarchiesi@0
|
5618 * In this example, the links are grouped by functionality, which can be
|
danielebarchiesi@0
|
5619 * helpful to themers who want to display certain kinds of links independently.
|
danielebarchiesi@0
|
5620 * For example, adding this code to node.tpl.php will result in the comment
|
danielebarchiesi@0
|
5621 * links being rendered as a single list:
|
danielebarchiesi@0
|
5622 * @code
|
danielebarchiesi@0
|
5623 * print render($content['links']['comment']);
|
danielebarchiesi@0
|
5624 * @endcode
|
danielebarchiesi@0
|
5625 *
|
danielebarchiesi@0
|
5626 * (where $node->content has been transformed into $content before handing
|
danielebarchiesi@0
|
5627 * control to the node.tpl.php template).
|
danielebarchiesi@0
|
5628 *
|
danielebarchiesi@0
|
5629 * The pre_render function defined here allows the above flexibility, but also
|
danielebarchiesi@0
|
5630 * allows the following code to be used to render all remaining links into a
|
danielebarchiesi@0
|
5631 * single list, regardless of their group:
|
danielebarchiesi@0
|
5632 * @code
|
danielebarchiesi@0
|
5633 * print render($content['links']);
|
danielebarchiesi@0
|
5634 * @endcode
|
danielebarchiesi@0
|
5635 *
|
danielebarchiesi@0
|
5636 * In the above example, this will result in the statistics and translation
|
danielebarchiesi@0
|
5637 * links being rendered together in a single list (but not the comment links,
|
danielebarchiesi@0
|
5638 * which were rendered previously on their own).
|
danielebarchiesi@0
|
5639 *
|
danielebarchiesi@0
|
5640 * Because of the way this function works, the individual properties of each
|
danielebarchiesi@0
|
5641 * group (for example, a group-specific #theme property such as
|
danielebarchiesi@0
|
5642 * 'links__node__comment' in the example above, or any other property such as
|
danielebarchiesi@0
|
5643 * #attributes or #pre_render that is attached to it) are only used when that
|
danielebarchiesi@0
|
5644 * group is rendered on its own. When the group is rendered together with other
|
danielebarchiesi@0
|
5645 * children, these child-specific properties are ignored, and only the overall
|
danielebarchiesi@0
|
5646 * properties of the parent are used.
|
danielebarchiesi@0
|
5647 */
|
danielebarchiesi@0
|
5648 function drupal_pre_render_links($element) {
|
danielebarchiesi@0
|
5649 $element += array('#links' => array());
|
danielebarchiesi@0
|
5650 foreach (element_children($element) as $key) {
|
danielebarchiesi@0
|
5651 $child = &$element[$key];
|
danielebarchiesi@0
|
5652 // If the child has links which have not been printed yet and the user has
|
danielebarchiesi@0
|
5653 // access to it, merge its links in to the parent.
|
danielebarchiesi@0
|
5654 if (isset($child['#links']) && empty($child['#printed']) && (!isset($child['#access']) || $child['#access'])) {
|
danielebarchiesi@0
|
5655 $element['#links'] += $child['#links'];
|
danielebarchiesi@0
|
5656 // Mark the child as having been printed already (so that its links
|
danielebarchiesi@0
|
5657 // cannot be mistakenly rendered twice).
|
danielebarchiesi@0
|
5658 $child['#printed'] = TRUE;
|
danielebarchiesi@0
|
5659 }
|
danielebarchiesi@0
|
5660 }
|
danielebarchiesi@0
|
5661 return $element;
|
danielebarchiesi@0
|
5662 }
|
danielebarchiesi@0
|
5663
|
danielebarchiesi@0
|
5664 /**
|
danielebarchiesi@0
|
5665 * #pre_render callback to append contents in #markup to #children.
|
danielebarchiesi@0
|
5666 *
|
danielebarchiesi@0
|
5667 * This needs to be a #pre_render callback, because eventually assigned
|
danielebarchiesi@0
|
5668 * #theme_wrappers will expect the element's rendered content in #children.
|
danielebarchiesi@0
|
5669 * Note that if also a #theme is defined for the element, then the result of
|
danielebarchiesi@0
|
5670 * the theme callback will override #children.
|
danielebarchiesi@0
|
5671 *
|
danielebarchiesi@0
|
5672 * @param $elements
|
danielebarchiesi@0
|
5673 * A structured array using the #markup key.
|
danielebarchiesi@0
|
5674 *
|
danielebarchiesi@0
|
5675 * @return
|
danielebarchiesi@0
|
5676 * The passed-in elements, but #markup appended to #children.
|
danielebarchiesi@0
|
5677 *
|
danielebarchiesi@0
|
5678 * @see drupal_render()
|
danielebarchiesi@0
|
5679 */
|
danielebarchiesi@0
|
5680 function drupal_pre_render_markup($elements) {
|
danielebarchiesi@0
|
5681 $elements['#children'] = $elements['#markup'];
|
danielebarchiesi@0
|
5682 return $elements;
|
danielebarchiesi@0
|
5683 }
|
danielebarchiesi@0
|
5684
|
danielebarchiesi@0
|
5685 /**
|
danielebarchiesi@0
|
5686 * Renders the page, including all theming.
|
danielebarchiesi@0
|
5687 *
|
danielebarchiesi@0
|
5688 * @param $page
|
danielebarchiesi@0
|
5689 * A string or array representing the content of a page. The array consists of
|
danielebarchiesi@0
|
5690 * the following keys:
|
danielebarchiesi@0
|
5691 * - #type: Value is always 'page'. This pushes the theming through
|
danielebarchiesi@0
|
5692 * page.tpl.php (required).
|
danielebarchiesi@0
|
5693 * - #show_messages: Suppress drupal_get_message() items. Used by Batch
|
danielebarchiesi@0
|
5694 * API (optional).
|
danielebarchiesi@0
|
5695 *
|
danielebarchiesi@0
|
5696 * @see hook_page_alter()
|
danielebarchiesi@0
|
5697 * @see element_info()
|
danielebarchiesi@0
|
5698 */
|
danielebarchiesi@0
|
5699 function drupal_render_page($page) {
|
danielebarchiesi@0
|
5700 $main_content_display = &drupal_static('system_main_content_added', FALSE);
|
danielebarchiesi@0
|
5701
|
danielebarchiesi@0
|
5702 // Allow menu callbacks to return strings or arbitrary arrays to render.
|
danielebarchiesi@0
|
5703 // If the array returned is not of #type page directly, we need to fill
|
danielebarchiesi@0
|
5704 // in the page with defaults.
|
danielebarchiesi@0
|
5705 if (is_string($page) || (is_array($page) && (!isset($page['#type']) || ($page['#type'] != 'page')))) {
|
danielebarchiesi@0
|
5706 drupal_set_page_content($page);
|
danielebarchiesi@0
|
5707 $page = element_info('page');
|
danielebarchiesi@0
|
5708 }
|
danielebarchiesi@0
|
5709
|
danielebarchiesi@0
|
5710 // Modules can add elements to $page as needed in hook_page_build().
|
danielebarchiesi@0
|
5711 foreach (module_implements('page_build') as $module) {
|
danielebarchiesi@0
|
5712 $function = $module . '_page_build';
|
danielebarchiesi@0
|
5713 $function($page);
|
danielebarchiesi@0
|
5714 }
|
danielebarchiesi@0
|
5715 // Modules alter the $page as needed. Blocks are populated into regions like
|
danielebarchiesi@0
|
5716 // 'sidebar_first', 'footer', etc.
|
danielebarchiesi@0
|
5717 drupal_alter('page', $page);
|
danielebarchiesi@0
|
5718
|
danielebarchiesi@0
|
5719 // If no module has taken care of the main content, add it to the page now.
|
danielebarchiesi@0
|
5720 // This allows the site to still be usable even if no modules that
|
danielebarchiesi@0
|
5721 // control page regions (for example, the Block module) are enabled.
|
danielebarchiesi@0
|
5722 if (!$main_content_display) {
|
danielebarchiesi@0
|
5723 $page['content']['system_main'] = drupal_set_page_content();
|
danielebarchiesi@0
|
5724 }
|
danielebarchiesi@0
|
5725
|
danielebarchiesi@0
|
5726 return drupal_render($page);
|
danielebarchiesi@0
|
5727 }
|
danielebarchiesi@0
|
5728
|
danielebarchiesi@0
|
5729 /**
|
danielebarchiesi@0
|
5730 * Renders HTML given a structured array tree.
|
danielebarchiesi@0
|
5731 *
|
danielebarchiesi@0
|
5732 * Recursively iterates over each of the array elements, generating HTML code.
|
danielebarchiesi@0
|
5733 *
|
danielebarchiesi@0
|
5734 * Renderable arrays have two kinds of key/value pairs: properties and
|
danielebarchiesi@0
|
5735 * children. Properties have keys starting with '#' and their values influence
|
danielebarchiesi@0
|
5736 * how the array will be rendered. Children are all elements whose keys do not
|
danielebarchiesi@0
|
5737 * start with a '#'. Their values should be renderable arrays themselves,
|
danielebarchiesi@0
|
5738 * which will be rendered during the rendering of the parent array. The markup
|
danielebarchiesi@0
|
5739 * provided by the children is typically inserted into the markup generated by
|
danielebarchiesi@0
|
5740 * the parent array.
|
danielebarchiesi@0
|
5741 *
|
danielebarchiesi@0
|
5742 * HTML generation for a renderable array, and the treatment of any children,
|
danielebarchiesi@0
|
5743 * is controlled by two properties containing theme functions, #theme and
|
danielebarchiesi@0
|
5744 * #theme_wrappers.
|
danielebarchiesi@0
|
5745 *
|
danielebarchiesi@0
|
5746 * #theme is the theme function called first. If it is set and the element has
|
danielebarchiesi@0
|
5747 * any children, it is the responsibility of the theme function to render
|
danielebarchiesi@0
|
5748 * these children. For elements that are not allowed to have any children,
|
danielebarchiesi@0
|
5749 * e.g. buttons or textfields, the theme function can be used to render the
|
danielebarchiesi@0
|
5750 * element itself. If #theme is not present and the element has children, each
|
danielebarchiesi@0
|
5751 * child is itself rendered by a call to drupal_render(), and the results are
|
danielebarchiesi@0
|
5752 * concatenated.
|
danielebarchiesi@0
|
5753 *
|
danielebarchiesi@0
|
5754 * The #theme_wrappers property contains an array of theme functions which will
|
danielebarchiesi@0
|
5755 * be called, in order, after #theme has run. These can be used to add further
|
danielebarchiesi@0
|
5756 * markup around the rendered children; e.g., fieldsets add the required markup
|
danielebarchiesi@0
|
5757 * for a fieldset around their rendered child elements. All wrapper theme
|
danielebarchiesi@0
|
5758 * functions have to include the element's #children property in their output,
|
danielebarchiesi@0
|
5759 * as it contains the output of the previous theme functions and the rendered
|
danielebarchiesi@0
|
5760 * children.
|
danielebarchiesi@0
|
5761 *
|
danielebarchiesi@0
|
5762 * For example, for the form element type, by default only the #theme_wrappers
|
danielebarchiesi@0
|
5763 * property is set, which adds the form markup around the rendered child
|
danielebarchiesi@0
|
5764 * elements of the form. This allows you to set the #theme property on a
|
danielebarchiesi@0
|
5765 * specific form to a custom theme function, giving you complete control over
|
danielebarchiesi@0
|
5766 * the placement of the form's children while not at all having to deal with
|
danielebarchiesi@0
|
5767 * the form markup itself.
|
danielebarchiesi@0
|
5768 *
|
danielebarchiesi@0
|
5769 * drupal_render() can optionally cache the rendered output of elements to
|
danielebarchiesi@0
|
5770 * improve performance. To use drupal_render() caching, set the element's #cache
|
danielebarchiesi@0
|
5771 * property to an associative array with one or several of the following keys:
|
danielebarchiesi@0
|
5772 * - 'keys': An array of one or more keys that identify the element. If 'keys'
|
danielebarchiesi@0
|
5773 * is set, the cache ID is created automatically from these keys. See
|
danielebarchiesi@0
|
5774 * drupal_render_cid_create().
|
danielebarchiesi@0
|
5775 * - 'granularity' (optional): Define the cache granularity using binary
|
danielebarchiesi@0
|
5776 * combinations of the cache granularity constants, e.g.
|
danielebarchiesi@0
|
5777 * DRUPAL_CACHE_PER_USER to cache for each user separately or
|
danielebarchiesi@0
|
5778 * DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE to cache separately for each
|
danielebarchiesi@0
|
5779 * page and role. If not specified the element is cached globally for each
|
danielebarchiesi@0
|
5780 * theme and language.
|
danielebarchiesi@0
|
5781 * - 'cid': Specify the cache ID directly. Either 'keys' or 'cid' is required.
|
danielebarchiesi@0
|
5782 * If 'cid' is set, 'keys' and 'granularity' are ignored. Use only if you
|
danielebarchiesi@0
|
5783 * have special requirements.
|
danielebarchiesi@0
|
5784 * - 'expire': Set to one of the cache lifetime constants.
|
danielebarchiesi@0
|
5785 * - 'bin': Specify a cache bin to cache the element in. Defaults to 'cache'.
|
danielebarchiesi@0
|
5786 *
|
danielebarchiesi@0
|
5787 * This function is usually called from within another function, like
|
danielebarchiesi@0
|
5788 * drupal_get_form() or a theme function. Elements are sorted internally
|
danielebarchiesi@0
|
5789 * using uasort(). Since this is expensive, when passing already sorted
|
danielebarchiesi@0
|
5790 * elements to drupal_render(), for example from a database query, set
|
danielebarchiesi@0
|
5791 * $elements['#sorted'] = TRUE to avoid sorting them a second time.
|
danielebarchiesi@0
|
5792 *
|
danielebarchiesi@0
|
5793 * drupal_render() flags each element with a '#printed' status to indicate that
|
danielebarchiesi@0
|
5794 * the element has been rendered, which allows individual elements of a given
|
danielebarchiesi@0
|
5795 * array to be rendered independently and prevents them from being rendered
|
danielebarchiesi@0
|
5796 * more than once on subsequent calls to drupal_render() (e.g., as part of a
|
danielebarchiesi@0
|
5797 * larger array). If the same array or array element is passed more than once
|
danielebarchiesi@0
|
5798 * to drupal_render(), it simply returns an empty string.
|
danielebarchiesi@0
|
5799 *
|
danielebarchiesi@0
|
5800 * @param array $elements
|
danielebarchiesi@0
|
5801 * The structured array describing the data to be rendered.
|
danielebarchiesi@0
|
5802 *
|
danielebarchiesi@0
|
5803 * @return string
|
danielebarchiesi@0
|
5804 * The rendered HTML.
|
danielebarchiesi@0
|
5805 */
|
danielebarchiesi@0
|
5806 function drupal_render(&$elements) {
|
danielebarchiesi@0
|
5807 // Early-return nothing if user does not have access.
|
danielebarchiesi@0
|
5808 if (empty($elements) || (isset($elements['#access']) && !$elements['#access'])) {
|
danielebarchiesi@0
|
5809 return '';
|
danielebarchiesi@0
|
5810 }
|
danielebarchiesi@0
|
5811
|
danielebarchiesi@0
|
5812 // Do not print elements twice.
|
danielebarchiesi@0
|
5813 if (!empty($elements['#printed'])) {
|
danielebarchiesi@0
|
5814 return '';
|
danielebarchiesi@0
|
5815 }
|
danielebarchiesi@0
|
5816
|
danielebarchiesi@0
|
5817 // Try to fetch the element's markup from cache and return.
|
danielebarchiesi@0
|
5818 if (isset($elements['#cache'])) {
|
danielebarchiesi@0
|
5819 $cached_output = drupal_render_cache_get($elements);
|
danielebarchiesi@0
|
5820 if ($cached_output !== FALSE) {
|
danielebarchiesi@0
|
5821 return $cached_output;
|
danielebarchiesi@0
|
5822 }
|
danielebarchiesi@0
|
5823 }
|
danielebarchiesi@0
|
5824
|
danielebarchiesi@0
|
5825 // If #markup is set, ensure #type is set. This allows to specify just #markup
|
danielebarchiesi@0
|
5826 // on an element without setting #type.
|
danielebarchiesi@0
|
5827 if (isset($elements['#markup']) && !isset($elements['#type'])) {
|
danielebarchiesi@0
|
5828 $elements['#type'] = 'markup';
|
danielebarchiesi@0
|
5829 }
|
danielebarchiesi@0
|
5830
|
danielebarchiesi@0
|
5831 // If the default values for this element have not been loaded yet, populate
|
danielebarchiesi@0
|
5832 // them.
|
danielebarchiesi@0
|
5833 if (isset($elements['#type']) && empty($elements['#defaults_loaded'])) {
|
danielebarchiesi@0
|
5834 $elements += element_info($elements['#type']);
|
danielebarchiesi@0
|
5835 }
|
danielebarchiesi@0
|
5836
|
danielebarchiesi@0
|
5837 // Make any final changes to the element before it is rendered. This means
|
danielebarchiesi@0
|
5838 // that the $element or the children can be altered or corrected before the
|
danielebarchiesi@0
|
5839 // element is rendered into the final text.
|
danielebarchiesi@0
|
5840 if (isset($elements['#pre_render'])) {
|
danielebarchiesi@0
|
5841 foreach ($elements['#pre_render'] as $function) {
|
danielebarchiesi@0
|
5842 if (function_exists($function)) {
|
danielebarchiesi@0
|
5843 $elements = $function($elements);
|
danielebarchiesi@0
|
5844 }
|
danielebarchiesi@0
|
5845 }
|
danielebarchiesi@0
|
5846 }
|
danielebarchiesi@0
|
5847
|
danielebarchiesi@0
|
5848 // Allow #pre_render to abort rendering.
|
danielebarchiesi@0
|
5849 if (!empty($elements['#printed'])) {
|
danielebarchiesi@0
|
5850 return '';
|
danielebarchiesi@0
|
5851 }
|
danielebarchiesi@0
|
5852
|
danielebarchiesi@0
|
5853 // Get the children of the element, sorted by weight.
|
danielebarchiesi@0
|
5854 $children = element_children($elements, TRUE);
|
danielebarchiesi@0
|
5855
|
danielebarchiesi@0
|
5856 // Initialize this element's #children, unless a #pre_render callback already
|
danielebarchiesi@0
|
5857 // preset #children.
|
danielebarchiesi@0
|
5858 if (!isset($elements['#children'])) {
|
danielebarchiesi@0
|
5859 $elements['#children'] = '';
|
danielebarchiesi@0
|
5860 }
|
danielebarchiesi@0
|
5861 // Call the element's #theme function if it is set. Then any children of the
|
danielebarchiesi@0
|
5862 // element have to be rendered there.
|
danielebarchiesi@0
|
5863 if (isset($elements['#theme'])) {
|
danielebarchiesi@0
|
5864 $elements['#children'] = theme($elements['#theme'], $elements);
|
danielebarchiesi@0
|
5865 }
|
danielebarchiesi@0
|
5866 // If #theme was not set and the element has children, render them now.
|
danielebarchiesi@0
|
5867 // This is the same process as drupal_render_children() but is inlined
|
danielebarchiesi@0
|
5868 // for speed.
|
danielebarchiesi@0
|
5869 if ($elements['#children'] == '') {
|
danielebarchiesi@0
|
5870 foreach ($children as $key) {
|
danielebarchiesi@0
|
5871 $elements['#children'] .= drupal_render($elements[$key]);
|
danielebarchiesi@0
|
5872 }
|
danielebarchiesi@0
|
5873 }
|
danielebarchiesi@0
|
5874
|
danielebarchiesi@0
|
5875 // Let the theme functions in #theme_wrappers add markup around the rendered
|
danielebarchiesi@0
|
5876 // children.
|
danielebarchiesi@0
|
5877 if (isset($elements['#theme_wrappers'])) {
|
danielebarchiesi@0
|
5878 foreach ($elements['#theme_wrappers'] as $theme_wrapper) {
|
danielebarchiesi@0
|
5879 $elements['#children'] = theme($theme_wrapper, $elements);
|
danielebarchiesi@0
|
5880 }
|
danielebarchiesi@0
|
5881 }
|
danielebarchiesi@0
|
5882
|
danielebarchiesi@0
|
5883 // Filter the outputted content and make any last changes before the
|
danielebarchiesi@0
|
5884 // content is sent to the browser. The changes are made on $content
|
danielebarchiesi@0
|
5885 // which allows the output'ed text to be filtered.
|
danielebarchiesi@0
|
5886 if (isset($elements['#post_render'])) {
|
danielebarchiesi@0
|
5887 foreach ($elements['#post_render'] as $function) {
|
danielebarchiesi@0
|
5888 if (function_exists($function)) {
|
danielebarchiesi@0
|
5889 $elements['#children'] = $function($elements['#children'], $elements);
|
danielebarchiesi@0
|
5890 }
|
danielebarchiesi@0
|
5891 }
|
danielebarchiesi@0
|
5892 }
|
danielebarchiesi@0
|
5893
|
danielebarchiesi@0
|
5894 // Add any JavaScript state information associated with the element.
|
danielebarchiesi@0
|
5895 if (!empty($elements['#states'])) {
|
danielebarchiesi@0
|
5896 drupal_process_states($elements);
|
danielebarchiesi@0
|
5897 }
|
danielebarchiesi@0
|
5898
|
danielebarchiesi@0
|
5899 // Add additional libraries, CSS, JavaScript an other custom
|
danielebarchiesi@0
|
5900 // attached data associated with this element.
|
danielebarchiesi@0
|
5901 if (!empty($elements['#attached'])) {
|
danielebarchiesi@0
|
5902 drupal_process_attached($elements);
|
danielebarchiesi@0
|
5903 }
|
danielebarchiesi@0
|
5904
|
danielebarchiesi@0
|
5905 $prefix = isset($elements['#prefix']) ? $elements['#prefix'] : '';
|
danielebarchiesi@0
|
5906 $suffix = isset($elements['#suffix']) ? $elements['#suffix'] : '';
|
danielebarchiesi@0
|
5907 $output = $prefix . $elements['#children'] . $suffix;
|
danielebarchiesi@0
|
5908
|
danielebarchiesi@0
|
5909 // Cache the processed element if #cache is set.
|
danielebarchiesi@0
|
5910 if (isset($elements['#cache'])) {
|
danielebarchiesi@0
|
5911 drupal_render_cache_set($output, $elements);
|
danielebarchiesi@0
|
5912 }
|
danielebarchiesi@0
|
5913
|
danielebarchiesi@0
|
5914 $elements['#printed'] = TRUE;
|
danielebarchiesi@0
|
5915 return $output;
|
danielebarchiesi@0
|
5916 }
|
danielebarchiesi@0
|
5917
|
danielebarchiesi@0
|
5918 /**
|
danielebarchiesi@0
|
5919 * Renders children of an element and concatenates them.
|
danielebarchiesi@0
|
5920 *
|
danielebarchiesi@0
|
5921 * This renders all children of an element using drupal_render() and then
|
danielebarchiesi@0
|
5922 * joins them together into a single string.
|
danielebarchiesi@0
|
5923 *
|
danielebarchiesi@0
|
5924 * @param $element
|
danielebarchiesi@0
|
5925 * The structured array whose children shall be rendered.
|
danielebarchiesi@0
|
5926 * @param $children_keys
|
danielebarchiesi@0
|
5927 * If the keys of the element's children are already known, they can be passed
|
danielebarchiesi@0
|
5928 * in to save another run of element_children().
|
danielebarchiesi@0
|
5929 */
|
danielebarchiesi@0
|
5930 function drupal_render_children(&$element, $children_keys = NULL) {
|
danielebarchiesi@0
|
5931 if ($children_keys === NULL) {
|
danielebarchiesi@0
|
5932 $children_keys = element_children($element);
|
danielebarchiesi@0
|
5933 }
|
danielebarchiesi@0
|
5934 $output = '';
|
danielebarchiesi@0
|
5935 foreach ($children_keys as $key) {
|
danielebarchiesi@0
|
5936 if (!empty($element[$key])) {
|
danielebarchiesi@0
|
5937 $output .= drupal_render($element[$key]);
|
danielebarchiesi@0
|
5938 }
|
danielebarchiesi@0
|
5939 }
|
danielebarchiesi@0
|
5940 return $output;
|
danielebarchiesi@0
|
5941 }
|
danielebarchiesi@0
|
5942
|
danielebarchiesi@0
|
5943 /**
|
danielebarchiesi@0
|
5944 * Renders an element.
|
danielebarchiesi@0
|
5945 *
|
danielebarchiesi@0
|
5946 * This function renders an element using drupal_render(). The top level
|
danielebarchiesi@0
|
5947 * element is shown with show() before rendering, so it will always be rendered
|
danielebarchiesi@0
|
5948 * even if hide() had been previously used on it.
|
danielebarchiesi@0
|
5949 *
|
danielebarchiesi@0
|
5950 * @param $element
|
danielebarchiesi@0
|
5951 * The element to be rendered.
|
danielebarchiesi@0
|
5952 *
|
danielebarchiesi@0
|
5953 * @return
|
danielebarchiesi@0
|
5954 * The rendered element.
|
danielebarchiesi@0
|
5955 *
|
danielebarchiesi@0
|
5956 * @see drupal_render()
|
danielebarchiesi@0
|
5957 * @see show()
|
danielebarchiesi@0
|
5958 * @see hide()
|
danielebarchiesi@0
|
5959 */
|
danielebarchiesi@0
|
5960 function render(&$element) {
|
danielebarchiesi@0
|
5961 if (is_array($element)) {
|
danielebarchiesi@0
|
5962 show($element);
|
danielebarchiesi@0
|
5963 return drupal_render($element);
|
danielebarchiesi@0
|
5964 }
|
danielebarchiesi@0
|
5965 else {
|
danielebarchiesi@0
|
5966 // Safe-guard for inappropriate use of render() on flat variables: return
|
danielebarchiesi@0
|
5967 // the variable as-is.
|
danielebarchiesi@0
|
5968 return $element;
|
danielebarchiesi@0
|
5969 }
|
danielebarchiesi@0
|
5970 }
|
danielebarchiesi@0
|
5971
|
danielebarchiesi@0
|
5972 /**
|
danielebarchiesi@0
|
5973 * Hides an element from later rendering.
|
danielebarchiesi@0
|
5974 *
|
danielebarchiesi@0
|
5975 * The first time render() or drupal_render() is called on an element tree,
|
danielebarchiesi@0
|
5976 * as each element in the tree is rendered, it is marked with a #printed flag
|
danielebarchiesi@0
|
5977 * and the rendered children of the element are cached. Subsequent calls to
|
danielebarchiesi@0
|
5978 * render() or drupal_render() will not traverse the child tree of this element
|
danielebarchiesi@0
|
5979 * again: they will just use the cached children. So if you want to hide an
|
danielebarchiesi@0
|
5980 * element, be sure to call hide() on the element before its parent tree is
|
danielebarchiesi@0
|
5981 * rendered for the first time, as it will have no effect on subsequent
|
danielebarchiesi@0
|
5982 * renderings of the parent tree.
|
danielebarchiesi@0
|
5983 *
|
danielebarchiesi@0
|
5984 * @param $element
|
danielebarchiesi@0
|
5985 * The element to be hidden.
|
danielebarchiesi@0
|
5986 *
|
danielebarchiesi@0
|
5987 * @return
|
danielebarchiesi@0
|
5988 * The element.
|
danielebarchiesi@0
|
5989 *
|
danielebarchiesi@0
|
5990 * @see render()
|
danielebarchiesi@0
|
5991 * @see show()
|
danielebarchiesi@0
|
5992 */
|
danielebarchiesi@0
|
5993 function hide(&$element) {
|
danielebarchiesi@0
|
5994 $element['#printed'] = TRUE;
|
danielebarchiesi@0
|
5995 return $element;
|
danielebarchiesi@0
|
5996 }
|
danielebarchiesi@0
|
5997
|
danielebarchiesi@0
|
5998 /**
|
danielebarchiesi@0
|
5999 * Shows a hidden element for later rendering.
|
danielebarchiesi@0
|
6000 *
|
danielebarchiesi@0
|
6001 * You can also use render($element), which shows the element while rendering
|
danielebarchiesi@0
|
6002 * it.
|
danielebarchiesi@0
|
6003 *
|
danielebarchiesi@0
|
6004 * The first time render() or drupal_render() is called on an element tree,
|
danielebarchiesi@0
|
6005 * as each element in the tree is rendered, it is marked with a #printed flag
|
danielebarchiesi@0
|
6006 * and the rendered children of the element are cached. Subsequent calls to
|
danielebarchiesi@0
|
6007 * render() or drupal_render() will not traverse the child tree of this element
|
danielebarchiesi@0
|
6008 * again: they will just use the cached children. So if you want to show an
|
danielebarchiesi@0
|
6009 * element, be sure to call show() on the element before its parent tree is
|
danielebarchiesi@0
|
6010 * rendered for the first time, as it will have no effect on subsequent
|
danielebarchiesi@0
|
6011 * renderings of the parent tree.
|
danielebarchiesi@0
|
6012 *
|
danielebarchiesi@0
|
6013 * @param $element
|
danielebarchiesi@0
|
6014 * The element to be shown.
|
danielebarchiesi@0
|
6015 *
|
danielebarchiesi@0
|
6016 * @return
|
danielebarchiesi@0
|
6017 * The element.
|
danielebarchiesi@0
|
6018 *
|
danielebarchiesi@0
|
6019 * @see render()
|
danielebarchiesi@0
|
6020 * @see hide()
|
danielebarchiesi@0
|
6021 */
|
danielebarchiesi@0
|
6022 function show(&$element) {
|
danielebarchiesi@0
|
6023 $element['#printed'] = FALSE;
|
danielebarchiesi@0
|
6024 return $element;
|
danielebarchiesi@0
|
6025 }
|
danielebarchiesi@0
|
6026
|
danielebarchiesi@0
|
6027 /**
|
danielebarchiesi@0
|
6028 * Gets the rendered output of a renderable element from the cache.
|
danielebarchiesi@0
|
6029 *
|
danielebarchiesi@0
|
6030 * @param $elements
|
danielebarchiesi@0
|
6031 * A renderable array.
|
danielebarchiesi@0
|
6032 *
|
danielebarchiesi@0
|
6033 * @return
|
danielebarchiesi@0
|
6034 * A markup string containing the rendered content of the element, or FALSE
|
danielebarchiesi@0
|
6035 * if no cached copy of the element is available.
|
danielebarchiesi@0
|
6036 *
|
danielebarchiesi@0
|
6037 * @see drupal_render()
|
danielebarchiesi@0
|
6038 * @see drupal_render_cache_set()
|
danielebarchiesi@0
|
6039 */
|
danielebarchiesi@0
|
6040 function drupal_render_cache_get($elements) {
|
danielebarchiesi@0
|
6041 if (!in_array($_SERVER['REQUEST_METHOD'], array('GET', 'HEAD')) || !$cid = drupal_render_cid_create($elements)) {
|
danielebarchiesi@0
|
6042 return FALSE;
|
danielebarchiesi@0
|
6043 }
|
danielebarchiesi@0
|
6044 $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';
|
danielebarchiesi@0
|
6045
|
danielebarchiesi@0
|
6046 if (!empty($cid) && $cache = cache_get($cid, $bin)) {
|
danielebarchiesi@0
|
6047 // Add additional libraries, JavaScript, CSS and other data attached
|
danielebarchiesi@0
|
6048 // to this element.
|
danielebarchiesi@0
|
6049 if (isset($cache->data['#attached'])) {
|
danielebarchiesi@0
|
6050 drupal_process_attached($cache->data);
|
danielebarchiesi@0
|
6051 }
|
danielebarchiesi@0
|
6052 // Return the rendered output.
|
danielebarchiesi@0
|
6053 return $cache->data['#markup'];
|
danielebarchiesi@0
|
6054 }
|
danielebarchiesi@0
|
6055 return FALSE;
|
danielebarchiesi@0
|
6056 }
|
danielebarchiesi@0
|
6057
|
danielebarchiesi@0
|
6058 /**
|
danielebarchiesi@0
|
6059 * Caches the rendered output of a renderable element.
|
danielebarchiesi@0
|
6060 *
|
danielebarchiesi@0
|
6061 * This is called by drupal_render() if the #cache property is set on an
|
danielebarchiesi@0
|
6062 * element.
|
danielebarchiesi@0
|
6063 *
|
danielebarchiesi@0
|
6064 * @param $markup
|
danielebarchiesi@0
|
6065 * The rendered output string of $elements.
|
danielebarchiesi@0
|
6066 * @param $elements
|
danielebarchiesi@0
|
6067 * A renderable array.
|
danielebarchiesi@0
|
6068 *
|
danielebarchiesi@0
|
6069 * @see drupal_render_cache_get()
|
danielebarchiesi@0
|
6070 */
|
danielebarchiesi@0
|
6071 function drupal_render_cache_set(&$markup, $elements) {
|
danielebarchiesi@0
|
6072 // Create the cache ID for the element.
|
danielebarchiesi@0
|
6073 if (!in_array($_SERVER['REQUEST_METHOD'], array('GET', 'HEAD')) || !$cid = drupal_render_cid_create($elements)) {
|
danielebarchiesi@0
|
6074 return FALSE;
|
danielebarchiesi@0
|
6075 }
|
danielebarchiesi@0
|
6076
|
danielebarchiesi@0
|
6077 // Cache implementations are allowed to modify the markup, to support
|
danielebarchiesi@0
|
6078 // replacing markup with edge-side include commands. The supporting cache
|
danielebarchiesi@0
|
6079 // backend will store the markup in some other key (like
|
danielebarchiesi@0
|
6080 // $data['#real-value']) and return an include command instead. When the
|
danielebarchiesi@0
|
6081 // ESI command is executed by the content accelerator, the real value can
|
danielebarchiesi@0
|
6082 // be retrieved and used.
|
danielebarchiesi@0
|
6083 $data['#markup'] = &$markup;
|
danielebarchiesi@0
|
6084 // Persist attached data associated with this element.
|
danielebarchiesi@0
|
6085 $attached = drupal_render_collect_attached($elements, TRUE);
|
danielebarchiesi@0
|
6086 if ($attached) {
|
danielebarchiesi@0
|
6087 $data['#attached'] = $attached;
|
danielebarchiesi@0
|
6088 }
|
danielebarchiesi@0
|
6089 $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';
|
danielebarchiesi@0
|
6090 $expire = isset($elements['#cache']['expire']) ? $elements['#cache']['expire'] : CACHE_PERMANENT;
|
danielebarchiesi@0
|
6091 cache_set($cid, $data, $bin, $expire);
|
danielebarchiesi@0
|
6092 }
|
danielebarchiesi@0
|
6093
|
danielebarchiesi@0
|
6094 /**
|
danielebarchiesi@0
|
6095 * Collects #attached for an element and its children into a single array.
|
danielebarchiesi@0
|
6096 *
|
danielebarchiesi@0
|
6097 * When caching elements, it is necessary to collect all libraries, JavaScript
|
danielebarchiesi@0
|
6098 * and CSS into a single array, from both the element itself and all child
|
danielebarchiesi@0
|
6099 * elements. This allows drupal_render() to add these back to the page when the
|
danielebarchiesi@0
|
6100 * element is returned from cache.
|
danielebarchiesi@0
|
6101 *
|
danielebarchiesi@0
|
6102 * @param $elements
|
danielebarchiesi@0
|
6103 * The element to collect #attached from.
|
danielebarchiesi@0
|
6104 * @param $return
|
danielebarchiesi@0
|
6105 * Whether to return the attached elements and reset the internal static.
|
danielebarchiesi@0
|
6106 *
|
danielebarchiesi@0
|
6107 * @return
|
danielebarchiesi@0
|
6108 * The #attached array for this element and its descendants.
|
danielebarchiesi@0
|
6109 */
|
danielebarchiesi@0
|
6110 function drupal_render_collect_attached($elements, $return = FALSE) {
|
danielebarchiesi@0
|
6111 $attached = &drupal_static(__FUNCTION__, array());
|
danielebarchiesi@0
|
6112
|
danielebarchiesi@0
|
6113 // Collect all #attached for this element.
|
danielebarchiesi@0
|
6114 if (isset($elements['#attached'])) {
|
danielebarchiesi@0
|
6115 foreach ($elements['#attached'] as $key => $value) {
|
danielebarchiesi@0
|
6116 if (!isset($attached[$key])) {
|
danielebarchiesi@0
|
6117 $attached[$key] = array();
|
danielebarchiesi@0
|
6118 }
|
danielebarchiesi@0
|
6119 $attached[$key] = array_merge($attached[$key], $value);
|
danielebarchiesi@0
|
6120 }
|
danielebarchiesi@0
|
6121 }
|
danielebarchiesi@0
|
6122 if ($children = element_children($elements)) {
|
danielebarchiesi@0
|
6123 foreach ($children as $child) {
|
danielebarchiesi@0
|
6124 drupal_render_collect_attached($elements[$child]);
|
danielebarchiesi@0
|
6125 }
|
danielebarchiesi@0
|
6126 }
|
danielebarchiesi@0
|
6127
|
danielebarchiesi@0
|
6128 // If this was the first call to the function, return all attached elements
|
danielebarchiesi@0
|
6129 // and reset the static cache.
|
danielebarchiesi@0
|
6130 if ($return) {
|
danielebarchiesi@0
|
6131 $return = $attached;
|
danielebarchiesi@0
|
6132 $attached = array();
|
danielebarchiesi@0
|
6133 return $return;
|
danielebarchiesi@0
|
6134 }
|
danielebarchiesi@0
|
6135 }
|
danielebarchiesi@0
|
6136
|
danielebarchiesi@0
|
6137 /**
|
danielebarchiesi@0
|
6138 * Prepares an element for caching based on a query.
|
danielebarchiesi@0
|
6139 *
|
danielebarchiesi@0
|
6140 * This smart caching strategy saves Drupal from querying and rendering to HTML
|
danielebarchiesi@0
|
6141 * when the underlying query is unchanged.
|
danielebarchiesi@0
|
6142 *
|
danielebarchiesi@0
|
6143 * Expensive queries should use the query builder to create the query and then
|
danielebarchiesi@0
|
6144 * call this function. Executing the query and formatting results should happen
|
danielebarchiesi@0
|
6145 * in a #pre_render callback.
|
danielebarchiesi@0
|
6146 *
|
danielebarchiesi@0
|
6147 * @param $query
|
danielebarchiesi@0
|
6148 * A select query object as returned by db_select().
|
danielebarchiesi@0
|
6149 * @param $function
|
danielebarchiesi@0
|
6150 * The name of the function doing this caching. A _pre_render suffix will be
|
danielebarchiesi@0
|
6151 * added to this string and is also part of the cache key in
|
danielebarchiesi@0
|
6152 * drupal_render_cache_set() and drupal_render_cache_get().
|
danielebarchiesi@0
|
6153 * @param $expire
|
danielebarchiesi@0
|
6154 * The cache expire time, passed eventually to cache_set().
|
danielebarchiesi@0
|
6155 * @param $granularity
|
danielebarchiesi@0
|
6156 * One or more granularity constants passed to drupal_render_cid_parts().
|
danielebarchiesi@0
|
6157 *
|
danielebarchiesi@0
|
6158 * @return
|
danielebarchiesi@0
|
6159 * A renderable array with the following keys and values:
|
danielebarchiesi@0
|
6160 * - #query: The passed-in $query.
|
danielebarchiesi@0
|
6161 * - #pre_render: $function with a _pre_render suffix.
|
danielebarchiesi@0
|
6162 * - #cache: An associative array prepared for drupal_render_cache_set().
|
danielebarchiesi@0
|
6163 */
|
danielebarchiesi@0
|
6164 function drupal_render_cache_by_query($query, $function, $expire = CACHE_TEMPORARY, $granularity = NULL) {
|
danielebarchiesi@0
|
6165 $cache_keys = array_merge(array($function), drupal_render_cid_parts($granularity));
|
danielebarchiesi@0
|
6166 $query->preExecute();
|
danielebarchiesi@0
|
6167 $cache_keys[] = hash('sha256', serialize(array((string) $query, $query->getArguments())));
|
danielebarchiesi@0
|
6168 return array(
|
danielebarchiesi@0
|
6169 '#query' => $query,
|
danielebarchiesi@0
|
6170 '#pre_render' => array($function . '_pre_render'),
|
danielebarchiesi@0
|
6171 '#cache' => array(
|
danielebarchiesi@0
|
6172 'keys' => $cache_keys,
|
danielebarchiesi@0
|
6173 'expire' => $expire,
|
danielebarchiesi@0
|
6174 ),
|
danielebarchiesi@0
|
6175 );
|
danielebarchiesi@0
|
6176 }
|
danielebarchiesi@0
|
6177
|
danielebarchiesi@0
|
6178 /**
|
danielebarchiesi@0
|
6179 * Returns cache ID parts for building a cache ID.
|
danielebarchiesi@0
|
6180 *
|
danielebarchiesi@0
|
6181 * @param $granularity
|
danielebarchiesi@0
|
6182 * One or more cache granularity constants. For example, to cache separately
|
danielebarchiesi@0
|
6183 * for each user, use DRUPAL_CACHE_PER_USER. To cache separately for each
|
danielebarchiesi@0
|
6184 * page and role, use the expression:
|
danielebarchiesi@0
|
6185 * @code
|
danielebarchiesi@0
|
6186 * DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE
|
danielebarchiesi@0
|
6187 * @endcode
|
danielebarchiesi@0
|
6188 *
|
danielebarchiesi@0
|
6189 * @return
|
danielebarchiesi@0
|
6190 * An array of cache ID parts, always containing the active theme. If the
|
danielebarchiesi@0
|
6191 * locale module is enabled it also contains the active language. If
|
danielebarchiesi@0
|
6192 * $granularity was passed in, more parts are added.
|
danielebarchiesi@0
|
6193 */
|
danielebarchiesi@0
|
6194 function drupal_render_cid_parts($granularity = NULL) {
|
danielebarchiesi@0
|
6195 global $theme, $base_root, $user;
|
danielebarchiesi@0
|
6196
|
danielebarchiesi@0
|
6197 $cid_parts[] = $theme;
|
danielebarchiesi@0
|
6198 // If Locale is enabled but we have only one language we do not need it as cid
|
danielebarchiesi@0
|
6199 // part.
|
danielebarchiesi@0
|
6200 if (drupal_multilingual()) {
|
danielebarchiesi@0
|
6201 foreach (language_types_configurable() as $language_type) {
|
danielebarchiesi@0
|
6202 $cid_parts[] = $GLOBALS[$language_type]->language;
|
danielebarchiesi@0
|
6203 }
|
danielebarchiesi@0
|
6204 }
|
danielebarchiesi@0
|
6205
|
danielebarchiesi@0
|
6206 if (!empty($granularity)) {
|
danielebarchiesi@0
|
6207 // 'PER_ROLE' and 'PER_USER' are mutually exclusive. 'PER_USER' can be a
|
danielebarchiesi@0
|
6208 // resource drag for sites with many users, so when a module is being
|
danielebarchiesi@0
|
6209 // equivocal, we favor the less expensive 'PER_ROLE' pattern.
|
danielebarchiesi@0
|
6210 if ($granularity & DRUPAL_CACHE_PER_ROLE) {
|
danielebarchiesi@0
|
6211 $cid_parts[] = 'r.' . implode(',', array_keys($user->roles));
|
danielebarchiesi@0
|
6212 }
|
danielebarchiesi@0
|
6213 elseif ($granularity & DRUPAL_CACHE_PER_USER) {
|
danielebarchiesi@0
|
6214 $cid_parts[] = "u.$user->uid";
|
danielebarchiesi@0
|
6215 }
|
danielebarchiesi@0
|
6216
|
danielebarchiesi@0
|
6217 if ($granularity & DRUPAL_CACHE_PER_PAGE) {
|
danielebarchiesi@0
|
6218 $cid_parts[] = $base_root . request_uri();
|
danielebarchiesi@0
|
6219 }
|
danielebarchiesi@0
|
6220 }
|
danielebarchiesi@0
|
6221
|
danielebarchiesi@0
|
6222 return $cid_parts;
|
danielebarchiesi@0
|
6223 }
|
danielebarchiesi@0
|
6224
|
danielebarchiesi@0
|
6225 /**
|
danielebarchiesi@0
|
6226 * Creates the cache ID for a renderable element.
|
danielebarchiesi@0
|
6227 *
|
danielebarchiesi@0
|
6228 * This creates the cache ID string, either by returning the #cache['cid']
|
danielebarchiesi@0
|
6229 * property if present or by building the cache ID out of the #cache['keys']
|
danielebarchiesi@0
|
6230 * and, optionally, the #cache['granularity'] properties.
|
danielebarchiesi@0
|
6231 *
|
danielebarchiesi@0
|
6232 * @param $elements
|
danielebarchiesi@0
|
6233 * A renderable array.
|
danielebarchiesi@0
|
6234 *
|
danielebarchiesi@0
|
6235 * @return
|
danielebarchiesi@0
|
6236 * The cache ID string, or FALSE if the element may not be cached.
|
danielebarchiesi@0
|
6237 */
|
danielebarchiesi@0
|
6238 function drupal_render_cid_create($elements) {
|
danielebarchiesi@0
|
6239 if (isset($elements['#cache']['cid'])) {
|
danielebarchiesi@0
|
6240 return $elements['#cache']['cid'];
|
danielebarchiesi@0
|
6241 }
|
danielebarchiesi@0
|
6242 elseif (isset($elements['#cache']['keys'])) {
|
danielebarchiesi@0
|
6243 $granularity = isset($elements['#cache']['granularity']) ? $elements['#cache']['granularity'] : NULL;
|
danielebarchiesi@0
|
6244 // Merge in additional cache ID parts based provided by drupal_render_cid_parts().
|
danielebarchiesi@0
|
6245 $cid_parts = array_merge($elements['#cache']['keys'], drupal_render_cid_parts($granularity));
|
danielebarchiesi@0
|
6246 return implode(':', $cid_parts);
|
danielebarchiesi@0
|
6247 }
|
danielebarchiesi@0
|
6248 return FALSE;
|
danielebarchiesi@0
|
6249 }
|
danielebarchiesi@0
|
6250
|
danielebarchiesi@0
|
6251 /**
|
danielebarchiesi@0
|
6252 * Function used by uasort to sort structured arrays by weight.
|
danielebarchiesi@0
|
6253 */
|
danielebarchiesi@0
|
6254 function element_sort($a, $b) {
|
danielebarchiesi@0
|
6255 $a_weight = (is_array($a) && isset($a['#weight'])) ? $a['#weight'] : 0;
|
danielebarchiesi@0
|
6256 $b_weight = (is_array($b) && isset($b['#weight'])) ? $b['#weight'] : 0;
|
danielebarchiesi@0
|
6257 if ($a_weight == $b_weight) {
|
danielebarchiesi@0
|
6258 return 0;
|
danielebarchiesi@0
|
6259 }
|
danielebarchiesi@0
|
6260 return ($a_weight < $b_weight) ? -1 : 1;
|
danielebarchiesi@0
|
6261 }
|
danielebarchiesi@0
|
6262
|
danielebarchiesi@0
|
6263 /**
|
danielebarchiesi@0
|
6264 * Array sorting callback; sorts elements by title.
|
danielebarchiesi@0
|
6265 */
|
danielebarchiesi@0
|
6266 function element_sort_by_title($a, $b) {
|
danielebarchiesi@0
|
6267 $a_title = (is_array($a) && isset($a['#title'])) ? $a['#title'] : '';
|
danielebarchiesi@0
|
6268 $b_title = (is_array($b) && isset($b['#title'])) ? $b['#title'] : '';
|
danielebarchiesi@0
|
6269 return strnatcasecmp($a_title, $b_title);
|
danielebarchiesi@0
|
6270 }
|
danielebarchiesi@0
|
6271
|
danielebarchiesi@0
|
6272 /**
|
danielebarchiesi@0
|
6273 * Retrieves the default properties for the defined element type.
|
danielebarchiesi@0
|
6274 *
|
danielebarchiesi@0
|
6275 * @param $type
|
danielebarchiesi@0
|
6276 * An element type as defined by hook_element_info().
|
danielebarchiesi@0
|
6277 */
|
danielebarchiesi@0
|
6278 function element_info($type) {
|
danielebarchiesi@0
|
6279 // Use the advanced drupal_static() pattern, since this is called very often.
|
danielebarchiesi@0
|
6280 static $drupal_static_fast;
|
danielebarchiesi@0
|
6281 if (!isset($drupal_static_fast)) {
|
danielebarchiesi@0
|
6282 $drupal_static_fast['cache'] = &drupal_static(__FUNCTION__);
|
danielebarchiesi@0
|
6283 }
|
danielebarchiesi@0
|
6284 $cache = &$drupal_static_fast['cache'];
|
danielebarchiesi@0
|
6285
|
danielebarchiesi@0
|
6286 if (!isset($cache)) {
|
danielebarchiesi@0
|
6287 $cache = module_invoke_all('element_info');
|
danielebarchiesi@0
|
6288 foreach ($cache as $element_type => $info) {
|
danielebarchiesi@0
|
6289 $cache[$element_type]['#type'] = $element_type;
|
danielebarchiesi@0
|
6290 }
|
danielebarchiesi@0
|
6291 // Allow modules to alter the element type defaults.
|
danielebarchiesi@0
|
6292 drupal_alter('element_info', $cache);
|
danielebarchiesi@0
|
6293 }
|
danielebarchiesi@0
|
6294
|
danielebarchiesi@0
|
6295 return isset($cache[$type]) ? $cache[$type] : array();
|
danielebarchiesi@0
|
6296 }
|
danielebarchiesi@0
|
6297
|
danielebarchiesi@0
|
6298 /**
|
danielebarchiesi@0
|
6299 * Retrieves a single property for the defined element type.
|
danielebarchiesi@0
|
6300 *
|
danielebarchiesi@0
|
6301 * @param $type
|
danielebarchiesi@0
|
6302 * An element type as defined by hook_element_info().
|
danielebarchiesi@0
|
6303 * @param $property_name
|
danielebarchiesi@0
|
6304 * The property within the element type that should be returned.
|
danielebarchiesi@0
|
6305 * @param $default
|
danielebarchiesi@0
|
6306 * (Optional) The value to return if the element type does not specify a
|
danielebarchiesi@0
|
6307 * value for the property. Defaults to NULL.
|
danielebarchiesi@0
|
6308 */
|
danielebarchiesi@0
|
6309 function element_info_property($type, $property_name, $default = NULL) {
|
danielebarchiesi@0
|
6310 return (($info = element_info($type)) && array_key_exists($property_name, $info)) ? $info[$property_name] : $default;
|
danielebarchiesi@0
|
6311 }
|
danielebarchiesi@0
|
6312
|
danielebarchiesi@0
|
6313 /**
|
danielebarchiesi@0
|
6314 * Sorts a structured array by the 'weight' element.
|
danielebarchiesi@0
|
6315 *
|
danielebarchiesi@0
|
6316 * Note that the sorting is by the 'weight' array element, not by the render
|
danielebarchiesi@0
|
6317 * element property '#weight'.
|
danielebarchiesi@0
|
6318 *
|
danielebarchiesi@0
|
6319 * Callback for uasort() used in various functions.
|
danielebarchiesi@0
|
6320 *
|
danielebarchiesi@0
|
6321 * @param $a
|
danielebarchiesi@0
|
6322 * First item for comparison. The compared items should be associative arrays
|
danielebarchiesi@0
|
6323 * that optionally include a 'weight' element. For items without a 'weight'
|
danielebarchiesi@0
|
6324 * element, a default value of 0 will be used.
|
danielebarchiesi@0
|
6325 * @param $b
|
danielebarchiesi@0
|
6326 * Second item for comparison.
|
danielebarchiesi@0
|
6327 */
|
danielebarchiesi@0
|
6328 function drupal_sort_weight($a, $b) {
|
danielebarchiesi@0
|
6329 $a_weight = (is_array($a) && isset($a['weight'])) ? $a['weight'] : 0;
|
danielebarchiesi@0
|
6330 $b_weight = (is_array($b) && isset($b['weight'])) ? $b['weight'] : 0;
|
danielebarchiesi@0
|
6331 if ($a_weight == $b_weight) {
|
danielebarchiesi@0
|
6332 return 0;
|
danielebarchiesi@0
|
6333 }
|
danielebarchiesi@0
|
6334 return ($a_weight < $b_weight) ? -1 : 1;
|
danielebarchiesi@0
|
6335 }
|
danielebarchiesi@0
|
6336
|
danielebarchiesi@0
|
6337 /**
|
danielebarchiesi@0
|
6338 * Array sorting callback; sorts elements by 'title' key.
|
danielebarchiesi@0
|
6339 */
|
danielebarchiesi@0
|
6340 function drupal_sort_title($a, $b) {
|
danielebarchiesi@0
|
6341 if (!isset($b['title'])) {
|
danielebarchiesi@0
|
6342 return -1;
|
danielebarchiesi@0
|
6343 }
|
danielebarchiesi@0
|
6344 if (!isset($a['title'])) {
|
danielebarchiesi@0
|
6345 return 1;
|
danielebarchiesi@0
|
6346 }
|
danielebarchiesi@0
|
6347 return strcasecmp($a['title'], $b['title']);
|
danielebarchiesi@0
|
6348 }
|
danielebarchiesi@0
|
6349
|
danielebarchiesi@0
|
6350 /**
|
danielebarchiesi@0
|
6351 * Checks if the key is a property.
|
danielebarchiesi@0
|
6352 */
|
danielebarchiesi@0
|
6353 function element_property($key) {
|
danielebarchiesi@0
|
6354 return $key[0] == '#';
|
danielebarchiesi@0
|
6355 }
|
danielebarchiesi@0
|
6356
|
danielebarchiesi@0
|
6357 /**
|
danielebarchiesi@0
|
6358 * Gets properties of a structured array element (keys beginning with '#').
|
danielebarchiesi@0
|
6359 */
|
danielebarchiesi@0
|
6360 function element_properties($element) {
|
danielebarchiesi@0
|
6361 return array_filter(array_keys((array) $element), 'element_property');
|
danielebarchiesi@0
|
6362 }
|
danielebarchiesi@0
|
6363
|
danielebarchiesi@0
|
6364 /**
|
danielebarchiesi@0
|
6365 * Checks if the key is a child.
|
danielebarchiesi@0
|
6366 */
|
danielebarchiesi@0
|
6367 function element_child($key) {
|
danielebarchiesi@0
|
6368 return !isset($key[0]) || $key[0] != '#';
|
danielebarchiesi@0
|
6369 }
|
danielebarchiesi@0
|
6370
|
danielebarchiesi@0
|
6371 /**
|
danielebarchiesi@0
|
6372 * Identifies the children of an element array, optionally sorted by weight.
|
danielebarchiesi@0
|
6373 *
|
danielebarchiesi@0
|
6374 * The children of a element array are those key/value pairs whose key does
|
danielebarchiesi@0
|
6375 * not start with a '#'. See drupal_render() for details.
|
danielebarchiesi@0
|
6376 *
|
danielebarchiesi@0
|
6377 * @param $elements
|
danielebarchiesi@0
|
6378 * The element array whose children are to be identified.
|
danielebarchiesi@0
|
6379 * @param $sort
|
danielebarchiesi@0
|
6380 * Boolean to indicate whether the children should be sorted by weight.
|
danielebarchiesi@0
|
6381 *
|
danielebarchiesi@0
|
6382 * @return
|
danielebarchiesi@0
|
6383 * The array keys of the element's children.
|
danielebarchiesi@0
|
6384 */
|
danielebarchiesi@0
|
6385 function element_children(&$elements, $sort = FALSE) {
|
danielebarchiesi@0
|
6386 // Do not attempt to sort elements which have already been sorted.
|
danielebarchiesi@0
|
6387 $sort = isset($elements['#sorted']) ? !$elements['#sorted'] : $sort;
|
danielebarchiesi@0
|
6388
|
danielebarchiesi@0
|
6389 // Filter out properties from the element, leaving only children.
|
danielebarchiesi@0
|
6390 $children = array();
|
danielebarchiesi@0
|
6391 $sortable = FALSE;
|
danielebarchiesi@0
|
6392 foreach ($elements as $key => $value) {
|
danielebarchiesi@0
|
6393 if ($key === '' || $key[0] !== '#') {
|
danielebarchiesi@0
|
6394 $children[$key] = $value;
|
danielebarchiesi@0
|
6395 if (is_array($value) && isset($value['#weight'])) {
|
danielebarchiesi@0
|
6396 $sortable = TRUE;
|
danielebarchiesi@0
|
6397 }
|
danielebarchiesi@0
|
6398 }
|
danielebarchiesi@0
|
6399 }
|
danielebarchiesi@0
|
6400 // Sort the children if necessary.
|
danielebarchiesi@0
|
6401 if ($sort && $sortable) {
|
danielebarchiesi@0
|
6402 uasort($children, 'element_sort');
|
danielebarchiesi@0
|
6403 // Put the sorted children back into $elements in the correct order, to
|
danielebarchiesi@0
|
6404 // preserve sorting if the same element is passed through
|
danielebarchiesi@0
|
6405 // element_children() twice.
|
danielebarchiesi@0
|
6406 foreach ($children as $key => $child) {
|
danielebarchiesi@0
|
6407 unset($elements[$key]);
|
danielebarchiesi@0
|
6408 $elements[$key] = $child;
|
danielebarchiesi@0
|
6409 }
|
danielebarchiesi@0
|
6410 $elements['#sorted'] = TRUE;
|
danielebarchiesi@0
|
6411 }
|
danielebarchiesi@0
|
6412
|
danielebarchiesi@0
|
6413 return array_keys($children);
|
danielebarchiesi@0
|
6414 }
|
danielebarchiesi@0
|
6415
|
danielebarchiesi@0
|
6416 /**
|
danielebarchiesi@0
|
6417 * Returns the visible children of an element.
|
danielebarchiesi@0
|
6418 *
|
danielebarchiesi@0
|
6419 * @param $elements
|
danielebarchiesi@0
|
6420 * The parent element.
|
danielebarchiesi@0
|
6421 *
|
danielebarchiesi@0
|
6422 * @return
|
danielebarchiesi@0
|
6423 * The array keys of the element's visible children.
|
danielebarchiesi@0
|
6424 */
|
danielebarchiesi@0
|
6425 function element_get_visible_children(array $elements) {
|
danielebarchiesi@0
|
6426 $visible_children = array();
|
danielebarchiesi@0
|
6427
|
danielebarchiesi@0
|
6428 foreach (element_children($elements) as $key) {
|
danielebarchiesi@0
|
6429 $child = $elements[$key];
|
danielebarchiesi@0
|
6430
|
danielebarchiesi@0
|
6431 // Skip un-accessible children.
|
danielebarchiesi@0
|
6432 if (isset($child['#access']) && !$child['#access']) {
|
danielebarchiesi@0
|
6433 continue;
|
danielebarchiesi@0
|
6434 }
|
danielebarchiesi@0
|
6435
|
danielebarchiesi@0
|
6436 // Skip value and hidden elements, since they are not rendered.
|
danielebarchiesi@0
|
6437 if (isset($child['#type']) && in_array($child['#type'], array('value', 'hidden'))) {
|
danielebarchiesi@0
|
6438 continue;
|
danielebarchiesi@0
|
6439 }
|
danielebarchiesi@0
|
6440
|
danielebarchiesi@0
|
6441 $visible_children[$key] = $child;
|
danielebarchiesi@0
|
6442 }
|
danielebarchiesi@0
|
6443
|
danielebarchiesi@0
|
6444 return array_keys($visible_children);
|
danielebarchiesi@0
|
6445 }
|
danielebarchiesi@0
|
6446
|
danielebarchiesi@0
|
6447 /**
|
danielebarchiesi@0
|
6448 * Sets HTML attributes based on element properties.
|
danielebarchiesi@0
|
6449 *
|
danielebarchiesi@0
|
6450 * @param $element
|
danielebarchiesi@0
|
6451 * The renderable element to process.
|
danielebarchiesi@0
|
6452 * @param $map
|
danielebarchiesi@0
|
6453 * An associative array whose keys are element property names and whose values
|
danielebarchiesi@0
|
6454 * are the HTML attribute names to set for corresponding the property; e.g.,
|
danielebarchiesi@0
|
6455 * array('#propertyname' => 'attributename'). If both names are identical
|
danielebarchiesi@0
|
6456 * except for the leading '#', then an attribute name value is sufficient and
|
danielebarchiesi@0
|
6457 * no property name needs to be specified.
|
danielebarchiesi@0
|
6458 */
|
danielebarchiesi@0
|
6459 function element_set_attributes(array &$element, array $map) {
|
danielebarchiesi@0
|
6460 foreach ($map as $property => $attribute) {
|
danielebarchiesi@0
|
6461 // If the key is numeric, the attribute name needs to be taken over.
|
danielebarchiesi@0
|
6462 if (is_int($property)) {
|
danielebarchiesi@0
|
6463 $property = '#' . $attribute;
|
danielebarchiesi@0
|
6464 }
|
danielebarchiesi@0
|
6465 // Do not overwrite already existing attributes.
|
danielebarchiesi@0
|
6466 if (isset($element[$property]) && !isset($element['#attributes'][$attribute])) {
|
danielebarchiesi@0
|
6467 $element['#attributes'][$attribute] = $element[$property];
|
danielebarchiesi@0
|
6468 }
|
danielebarchiesi@0
|
6469 }
|
danielebarchiesi@0
|
6470 }
|
danielebarchiesi@0
|
6471
|
danielebarchiesi@0
|
6472 /**
|
danielebarchiesi@0
|
6473 * Recursively computes the difference of arrays with additional index check.
|
danielebarchiesi@0
|
6474 *
|
danielebarchiesi@0
|
6475 * This is a version of array_diff_assoc() that supports multidimensional
|
danielebarchiesi@0
|
6476 * arrays.
|
danielebarchiesi@0
|
6477 *
|
danielebarchiesi@0
|
6478 * @param array $array1
|
danielebarchiesi@0
|
6479 * The array to compare from.
|
danielebarchiesi@0
|
6480 * @param array $array2
|
danielebarchiesi@0
|
6481 * The array to compare to.
|
danielebarchiesi@0
|
6482 *
|
danielebarchiesi@0
|
6483 * @return array
|
danielebarchiesi@0
|
6484 * Returns an array containing all the values from array1 that are not present
|
danielebarchiesi@0
|
6485 * in array2.
|
danielebarchiesi@0
|
6486 */
|
danielebarchiesi@0
|
6487 function drupal_array_diff_assoc_recursive($array1, $array2) {
|
danielebarchiesi@0
|
6488 $difference = array();
|
danielebarchiesi@0
|
6489
|
danielebarchiesi@0
|
6490 foreach ($array1 as $key => $value) {
|
danielebarchiesi@0
|
6491 if (is_array($value)) {
|
danielebarchiesi@0
|
6492 if (!array_key_exists($key, $array2) || !is_array($array2[$key])) {
|
danielebarchiesi@0
|
6493 $difference[$key] = $value;
|
danielebarchiesi@0
|
6494 }
|
danielebarchiesi@0
|
6495 else {
|
danielebarchiesi@0
|
6496 $new_diff = drupal_array_diff_assoc_recursive($value, $array2[$key]);
|
danielebarchiesi@0
|
6497 if (!empty($new_diff)) {
|
danielebarchiesi@0
|
6498 $difference[$key] = $new_diff;
|
danielebarchiesi@0
|
6499 }
|
danielebarchiesi@0
|
6500 }
|
danielebarchiesi@0
|
6501 }
|
danielebarchiesi@0
|
6502 elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) {
|
danielebarchiesi@0
|
6503 $difference[$key] = $value;
|
danielebarchiesi@0
|
6504 }
|
danielebarchiesi@0
|
6505 }
|
danielebarchiesi@0
|
6506
|
danielebarchiesi@0
|
6507 return $difference;
|
danielebarchiesi@0
|
6508 }
|
danielebarchiesi@0
|
6509
|
danielebarchiesi@0
|
6510 /**
|
danielebarchiesi@0
|
6511 * Sets a value in a nested array with variable depth.
|
danielebarchiesi@0
|
6512 *
|
danielebarchiesi@0
|
6513 * This helper function should be used when the depth of the array element you
|
danielebarchiesi@0
|
6514 * are changing may vary (that is, the number of parent keys is variable). It
|
danielebarchiesi@0
|
6515 * is primarily used for form structures and renderable arrays.
|
danielebarchiesi@0
|
6516 *
|
danielebarchiesi@0
|
6517 * Example:
|
danielebarchiesi@0
|
6518 * @code
|
danielebarchiesi@0
|
6519 * // Assume you have a 'signature' element somewhere in a form. It might be:
|
danielebarchiesi@0
|
6520 * $form['signature_settings']['signature'] = array(
|
danielebarchiesi@0
|
6521 * '#type' => 'text_format',
|
danielebarchiesi@0
|
6522 * '#title' => t('Signature'),
|
danielebarchiesi@0
|
6523 * );
|
danielebarchiesi@0
|
6524 * // Or, it might be further nested:
|
danielebarchiesi@0
|
6525 * $form['signature_settings']['user']['signature'] = array(
|
danielebarchiesi@0
|
6526 * '#type' => 'text_format',
|
danielebarchiesi@0
|
6527 * '#title' => t('Signature'),
|
danielebarchiesi@0
|
6528 * );
|
danielebarchiesi@0
|
6529 * @endcode
|
danielebarchiesi@0
|
6530 *
|
danielebarchiesi@0
|
6531 * To deal with the situation, the code needs to figure out the route to the
|
danielebarchiesi@0
|
6532 * element, given an array of parents that is either
|
danielebarchiesi@0
|
6533 * @code array('signature_settings', 'signature') @endcode in the first case or
|
danielebarchiesi@0
|
6534 * @code array('signature_settings', 'user', 'signature') @endcode in the second
|
danielebarchiesi@0
|
6535 * case.
|
danielebarchiesi@0
|
6536 *
|
danielebarchiesi@0
|
6537 * Without this helper function the only way to set the signature element in one
|
danielebarchiesi@0
|
6538 * line would be using eval(), which should be avoided:
|
danielebarchiesi@0
|
6539 * @code
|
danielebarchiesi@0
|
6540 * // Do not do this! Avoid eval().
|
danielebarchiesi@0
|
6541 * eval('$form[\'' . implode("']['", $parents) . '\'] = $element;');
|
danielebarchiesi@0
|
6542 * @endcode
|
danielebarchiesi@0
|
6543 *
|
danielebarchiesi@0
|
6544 * Instead, use this helper function:
|
danielebarchiesi@0
|
6545 * @code
|
danielebarchiesi@0
|
6546 * drupal_array_set_nested_value($form, $parents, $element);
|
danielebarchiesi@0
|
6547 * @endcode
|
danielebarchiesi@0
|
6548 *
|
danielebarchiesi@0
|
6549 * However if the number of array parent keys is static, the value should always
|
danielebarchiesi@0
|
6550 * be set directly rather than calling this function. For instance, for the
|
danielebarchiesi@0
|
6551 * first example we could just do:
|
danielebarchiesi@0
|
6552 * @code
|
danielebarchiesi@0
|
6553 * $form['signature_settings']['signature'] = $element;
|
danielebarchiesi@0
|
6554 * @endcode
|
danielebarchiesi@0
|
6555 *
|
danielebarchiesi@0
|
6556 * @param $array
|
danielebarchiesi@0
|
6557 * A reference to the array to modify.
|
danielebarchiesi@0
|
6558 * @param $parents
|
danielebarchiesi@0
|
6559 * An array of parent keys, starting with the outermost key.
|
danielebarchiesi@0
|
6560 * @param $value
|
danielebarchiesi@0
|
6561 * The value to set.
|
danielebarchiesi@0
|
6562 * @param $force
|
danielebarchiesi@0
|
6563 * (Optional) If TRUE, the value is forced into the structure even if it
|
danielebarchiesi@0
|
6564 * requires the deletion of an already existing non-array parent value. If
|
danielebarchiesi@0
|
6565 * FALSE, PHP throws an error if trying to add into a value that is not an
|
danielebarchiesi@0
|
6566 * array. Defaults to FALSE.
|
danielebarchiesi@0
|
6567 *
|
danielebarchiesi@0
|
6568 * @see drupal_array_get_nested_value()
|
danielebarchiesi@0
|
6569 */
|
danielebarchiesi@0
|
6570 function drupal_array_set_nested_value(array &$array, array $parents, $value, $force = FALSE) {
|
danielebarchiesi@0
|
6571 $ref = &$array;
|
danielebarchiesi@0
|
6572 foreach ($parents as $parent) {
|
danielebarchiesi@0
|
6573 // PHP auto-creates container arrays and NULL entries without error if $ref
|
danielebarchiesi@0
|
6574 // is NULL, but throws an error if $ref is set, but not an array.
|
danielebarchiesi@0
|
6575 if ($force && isset($ref) && !is_array($ref)) {
|
danielebarchiesi@0
|
6576 $ref = array();
|
danielebarchiesi@0
|
6577 }
|
danielebarchiesi@0
|
6578 $ref = &$ref[$parent];
|
danielebarchiesi@0
|
6579 }
|
danielebarchiesi@0
|
6580 $ref = $value;
|
danielebarchiesi@0
|
6581 }
|
danielebarchiesi@0
|
6582
|
danielebarchiesi@0
|
6583 /**
|
danielebarchiesi@0
|
6584 * Retrieves a value from a nested array with variable depth.
|
danielebarchiesi@0
|
6585 *
|
danielebarchiesi@0
|
6586 * This helper function should be used when the depth of the array element being
|
danielebarchiesi@0
|
6587 * retrieved may vary (that is, the number of parent keys is variable). It is
|
danielebarchiesi@0
|
6588 * primarily used for form structures and renderable arrays.
|
danielebarchiesi@0
|
6589 *
|
danielebarchiesi@0
|
6590 * Without this helper function the only way to get a nested array value with
|
danielebarchiesi@0
|
6591 * variable depth in one line would be using eval(), which should be avoided:
|
danielebarchiesi@0
|
6592 * @code
|
danielebarchiesi@0
|
6593 * // Do not do this! Avoid eval().
|
danielebarchiesi@0
|
6594 * // May also throw a PHP notice, if the variable array keys do not exist.
|
danielebarchiesi@0
|
6595 * eval('$value = $array[\'' . implode("']['", $parents) . "'];");
|
danielebarchiesi@0
|
6596 * @endcode
|
danielebarchiesi@0
|
6597 *
|
danielebarchiesi@0
|
6598 * Instead, use this helper function:
|
danielebarchiesi@0
|
6599 * @code
|
danielebarchiesi@0
|
6600 * $value = drupal_array_get_nested_value($form, $parents);
|
danielebarchiesi@0
|
6601 * @endcode
|
danielebarchiesi@0
|
6602 *
|
danielebarchiesi@0
|
6603 * The return value will be NULL, regardless of whether the actual value is NULL
|
danielebarchiesi@0
|
6604 * or whether the requested key does not exist. If it is required to know
|
danielebarchiesi@0
|
6605 * whether the nested array key actually exists, pass a third argument that is
|
danielebarchiesi@0
|
6606 * altered by reference:
|
danielebarchiesi@0
|
6607 * @code
|
danielebarchiesi@0
|
6608 * $key_exists = NULL;
|
danielebarchiesi@0
|
6609 * $value = drupal_array_get_nested_value($form, $parents, $key_exists);
|
danielebarchiesi@0
|
6610 * if ($key_exists) {
|
danielebarchiesi@0
|
6611 * // ... do something with $value ...
|
danielebarchiesi@0
|
6612 * }
|
danielebarchiesi@0
|
6613 * @endcode
|
danielebarchiesi@0
|
6614 *
|
danielebarchiesi@0
|
6615 * However if the number of array parent keys is static, the value should always
|
danielebarchiesi@0
|
6616 * be retrieved directly rather than calling this function. For instance:
|
danielebarchiesi@0
|
6617 * @code
|
danielebarchiesi@0
|
6618 * $value = $form['signature_settings']['signature'];
|
danielebarchiesi@0
|
6619 * @endcode
|
danielebarchiesi@0
|
6620 *
|
danielebarchiesi@0
|
6621 * @param $array
|
danielebarchiesi@0
|
6622 * The array from which to get the value.
|
danielebarchiesi@0
|
6623 * @param $parents
|
danielebarchiesi@0
|
6624 * An array of parent keys of the value, starting with the outermost key.
|
danielebarchiesi@0
|
6625 * @param $key_exists
|
danielebarchiesi@0
|
6626 * (optional) If given, an already defined variable that is altered by
|
danielebarchiesi@0
|
6627 * reference.
|
danielebarchiesi@0
|
6628 *
|
danielebarchiesi@0
|
6629 * @return
|
danielebarchiesi@0
|
6630 * The requested nested value. Possibly NULL if the value is NULL or not all
|
danielebarchiesi@0
|
6631 * nested parent keys exist. $key_exists is altered by reference and is a
|
danielebarchiesi@0
|
6632 * Boolean that indicates whether all nested parent keys exist (TRUE) or not
|
danielebarchiesi@0
|
6633 * (FALSE). This allows to distinguish between the two possibilities when NULL
|
danielebarchiesi@0
|
6634 * is returned.
|
danielebarchiesi@0
|
6635 *
|
danielebarchiesi@0
|
6636 * @see drupal_array_set_nested_value()
|
danielebarchiesi@0
|
6637 */
|
danielebarchiesi@0
|
6638 function &drupal_array_get_nested_value(array &$array, array $parents, &$key_exists = NULL) {
|
danielebarchiesi@0
|
6639 $ref = &$array;
|
danielebarchiesi@0
|
6640 foreach ($parents as $parent) {
|
danielebarchiesi@0
|
6641 if (is_array($ref) && array_key_exists($parent, $ref)) {
|
danielebarchiesi@0
|
6642 $ref = &$ref[$parent];
|
danielebarchiesi@0
|
6643 }
|
danielebarchiesi@0
|
6644 else {
|
danielebarchiesi@0
|
6645 $key_exists = FALSE;
|
danielebarchiesi@0
|
6646 $null = NULL;
|
danielebarchiesi@0
|
6647 return $null;
|
danielebarchiesi@0
|
6648 }
|
danielebarchiesi@0
|
6649 }
|
danielebarchiesi@0
|
6650 $key_exists = TRUE;
|
danielebarchiesi@0
|
6651 return $ref;
|
danielebarchiesi@0
|
6652 }
|
danielebarchiesi@0
|
6653
|
danielebarchiesi@0
|
6654 /**
|
danielebarchiesi@0
|
6655 * Determines whether a nested array contains the requested keys.
|
danielebarchiesi@0
|
6656 *
|
danielebarchiesi@0
|
6657 * This helper function should be used when the depth of the array element to be
|
danielebarchiesi@0
|
6658 * checked may vary (that is, the number of parent keys is variable). See
|
danielebarchiesi@0
|
6659 * drupal_array_set_nested_value() for details. It is primarily used for form
|
danielebarchiesi@0
|
6660 * structures and renderable arrays.
|
danielebarchiesi@0
|
6661 *
|
danielebarchiesi@0
|
6662 * If it is required to also get the value of the checked nested key, use
|
danielebarchiesi@0
|
6663 * drupal_array_get_nested_value() instead.
|
danielebarchiesi@0
|
6664 *
|
danielebarchiesi@0
|
6665 * If the number of array parent keys is static, this helper function is
|
danielebarchiesi@0
|
6666 * unnecessary and the following code can be used instead:
|
danielebarchiesi@0
|
6667 * @code
|
danielebarchiesi@0
|
6668 * $value_exists = isset($form['signature_settings']['signature']);
|
danielebarchiesi@0
|
6669 * $key_exists = array_key_exists('signature', $form['signature_settings']);
|
danielebarchiesi@0
|
6670 * @endcode
|
danielebarchiesi@0
|
6671 *
|
danielebarchiesi@0
|
6672 * @param $array
|
danielebarchiesi@0
|
6673 * The array with the value to check for.
|
danielebarchiesi@0
|
6674 * @param $parents
|
danielebarchiesi@0
|
6675 * An array of parent keys of the value, starting with the outermost key.
|
danielebarchiesi@0
|
6676 *
|
danielebarchiesi@0
|
6677 * @return
|
danielebarchiesi@0
|
6678 * TRUE if all the parent keys exist, FALSE otherwise.
|
danielebarchiesi@0
|
6679 *
|
danielebarchiesi@0
|
6680 * @see drupal_array_get_nested_value()
|
danielebarchiesi@0
|
6681 */
|
danielebarchiesi@0
|
6682 function drupal_array_nested_key_exists(array $array, array $parents) {
|
danielebarchiesi@0
|
6683 // Although this function is similar to PHP's array_key_exists(), its
|
danielebarchiesi@0
|
6684 // arguments should be consistent with drupal_array_get_nested_value().
|
danielebarchiesi@0
|
6685 $key_exists = NULL;
|
danielebarchiesi@0
|
6686 drupal_array_get_nested_value($array, $parents, $key_exists);
|
danielebarchiesi@0
|
6687 return $key_exists;
|
danielebarchiesi@0
|
6688 }
|
danielebarchiesi@0
|
6689
|
danielebarchiesi@0
|
6690 /**
|
danielebarchiesi@0
|
6691 * Provides theme registration for themes across .inc files.
|
danielebarchiesi@0
|
6692 */
|
danielebarchiesi@0
|
6693 function drupal_common_theme() {
|
danielebarchiesi@0
|
6694 return array(
|
danielebarchiesi@0
|
6695 // From theme.inc.
|
danielebarchiesi@0
|
6696 'html' => array(
|
danielebarchiesi@0
|
6697 'render element' => 'page',
|
danielebarchiesi@0
|
6698 'template' => 'html',
|
danielebarchiesi@0
|
6699 ),
|
danielebarchiesi@0
|
6700 'page' => array(
|
danielebarchiesi@0
|
6701 'render element' => 'page',
|
danielebarchiesi@0
|
6702 'template' => 'page',
|
danielebarchiesi@0
|
6703 ),
|
danielebarchiesi@0
|
6704 'region' => array(
|
danielebarchiesi@0
|
6705 'render element' => 'elements',
|
danielebarchiesi@0
|
6706 'template' => 'region',
|
danielebarchiesi@0
|
6707 ),
|
danielebarchiesi@0
|
6708 'status_messages' => array(
|
danielebarchiesi@0
|
6709 'variables' => array('display' => NULL),
|
danielebarchiesi@0
|
6710 ),
|
danielebarchiesi@0
|
6711 'link' => array(
|
danielebarchiesi@0
|
6712 'variables' => array('text' => NULL, 'path' => NULL, 'options' => array()),
|
danielebarchiesi@0
|
6713 ),
|
danielebarchiesi@0
|
6714 'links' => array(
|
danielebarchiesi@0
|
6715 'variables' => array('links' => NULL, 'attributes' => array('class' => array('links')), 'heading' => array()),
|
danielebarchiesi@0
|
6716 ),
|
danielebarchiesi@0
|
6717 'image' => array(
|
danielebarchiesi@0
|
6718 // HTML 4 and XHTML 1.0 always require an alt attribute. The HTML 5 draft
|
danielebarchiesi@0
|
6719 // allows the alt attribute to be omitted in some cases. Therefore,
|
danielebarchiesi@0
|
6720 // default the alt attribute to an empty string, but allow code calling
|
danielebarchiesi@0
|
6721 // theme('image') to pass explicit NULL for it to be omitted. Usually,
|
danielebarchiesi@0
|
6722 // neither omission nor an empty string satisfies accessibility
|
danielebarchiesi@0
|
6723 // requirements, so it is strongly encouraged for code calling
|
danielebarchiesi@0
|
6724 // theme('image') to pass a meaningful value for the alt variable.
|
danielebarchiesi@0
|
6725 // - http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.8
|
danielebarchiesi@0
|
6726 // - http://www.w3.org/TR/xhtml1/dtds.html
|
danielebarchiesi@0
|
6727 // - http://dev.w3.org/html5/spec/Overview.html#alt
|
danielebarchiesi@0
|
6728 // The title attribute is optional in all cases, so it is omitted by
|
danielebarchiesi@0
|
6729 // default.
|
danielebarchiesi@0
|
6730 'variables' => array('path' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'attributes' => array()),
|
danielebarchiesi@0
|
6731 ),
|
danielebarchiesi@0
|
6732 'breadcrumb' => array(
|
danielebarchiesi@0
|
6733 'variables' => array('breadcrumb' => NULL),
|
danielebarchiesi@0
|
6734 ),
|
danielebarchiesi@0
|
6735 'help' => array(
|
danielebarchiesi@0
|
6736 'variables' => array(),
|
danielebarchiesi@0
|
6737 ),
|
danielebarchiesi@0
|
6738 'table' => array(
|
danielebarchiesi@0
|
6739 'variables' => array('header' => NULL, 'rows' => NULL, 'attributes' => array(), 'caption' => NULL, 'colgroups' => array(), 'sticky' => TRUE, 'empty' => ''),
|
danielebarchiesi@0
|
6740 ),
|
danielebarchiesi@0
|
6741 'tablesort_indicator' => array(
|
danielebarchiesi@0
|
6742 'variables' => array('style' => NULL),
|
danielebarchiesi@0
|
6743 ),
|
danielebarchiesi@0
|
6744 'mark' => array(
|
danielebarchiesi@0
|
6745 'variables' => array('type' => MARK_NEW),
|
danielebarchiesi@0
|
6746 ),
|
danielebarchiesi@0
|
6747 'item_list' => array(
|
danielebarchiesi@0
|
6748 'variables' => array('items' => array(), 'title' => NULL, 'type' => 'ul', 'attributes' => array()),
|
danielebarchiesi@0
|
6749 ),
|
danielebarchiesi@0
|
6750 'more_help_link' => array(
|
danielebarchiesi@0
|
6751 'variables' => array('url' => NULL),
|
danielebarchiesi@0
|
6752 ),
|
danielebarchiesi@0
|
6753 'feed_icon' => array(
|
danielebarchiesi@0
|
6754 'variables' => array('url' => NULL, 'title' => NULL),
|
danielebarchiesi@0
|
6755 ),
|
danielebarchiesi@0
|
6756 'more_link' => array(
|
danielebarchiesi@0
|
6757 'variables' => array('url' => NULL, 'title' => NULL)
|
danielebarchiesi@0
|
6758 ),
|
danielebarchiesi@0
|
6759 'username' => array(
|
danielebarchiesi@0
|
6760 'variables' => array('account' => NULL),
|
danielebarchiesi@0
|
6761 ),
|
danielebarchiesi@0
|
6762 'progress_bar' => array(
|
danielebarchiesi@0
|
6763 'variables' => array('percent' => NULL, 'message' => NULL),
|
danielebarchiesi@0
|
6764 ),
|
danielebarchiesi@0
|
6765 'indentation' => array(
|
danielebarchiesi@0
|
6766 'variables' => array('size' => 1),
|
danielebarchiesi@0
|
6767 ),
|
danielebarchiesi@0
|
6768 'html_tag' => array(
|
danielebarchiesi@0
|
6769 'render element' => 'element',
|
danielebarchiesi@0
|
6770 ),
|
danielebarchiesi@0
|
6771 // From theme.maintenance.inc.
|
danielebarchiesi@0
|
6772 'maintenance_page' => array(
|
danielebarchiesi@0
|
6773 'variables' => array('content' => NULL, 'show_messages' => TRUE),
|
danielebarchiesi@0
|
6774 'template' => 'maintenance-page',
|
danielebarchiesi@0
|
6775 ),
|
danielebarchiesi@0
|
6776 'update_page' => array(
|
danielebarchiesi@0
|
6777 'variables' => array('content' => NULL, 'show_messages' => TRUE),
|
danielebarchiesi@0
|
6778 ),
|
danielebarchiesi@0
|
6779 'install_page' => array(
|
danielebarchiesi@0
|
6780 'variables' => array('content' => NULL),
|
danielebarchiesi@0
|
6781 ),
|
danielebarchiesi@0
|
6782 'task_list' => array(
|
danielebarchiesi@0
|
6783 'variables' => array('items' => NULL, 'active' => NULL),
|
danielebarchiesi@0
|
6784 ),
|
danielebarchiesi@0
|
6785 'authorize_message' => array(
|
danielebarchiesi@0
|
6786 'variables' => array('message' => NULL, 'success' => TRUE),
|
danielebarchiesi@0
|
6787 ),
|
danielebarchiesi@0
|
6788 'authorize_report' => array(
|
danielebarchiesi@0
|
6789 'variables' => array('messages' => array()),
|
danielebarchiesi@0
|
6790 ),
|
danielebarchiesi@0
|
6791 // From pager.inc.
|
danielebarchiesi@0
|
6792 'pager' => array(
|
danielebarchiesi@0
|
6793 'variables' => array('tags' => array(), 'element' => 0, 'parameters' => array(), 'quantity' => 9),
|
danielebarchiesi@0
|
6794 ),
|
danielebarchiesi@0
|
6795 'pager_first' => array(
|
danielebarchiesi@0
|
6796 'variables' => array('text' => NULL, 'element' => 0, 'parameters' => array()),
|
danielebarchiesi@0
|
6797 ),
|
danielebarchiesi@0
|
6798 'pager_previous' => array(
|
danielebarchiesi@0
|
6799 'variables' => array('text' => NULL, 'element' => 0, 'interval' => 1, 'parameters' => array()),
|
danielebarchiesi@0
|
6800 ),
|
danielebarchiesi@0
|
6801 'pager_next' => array(
|
danielebarchiesi@0
|
6802 'variables' => array('text' => NULL, 'element' => 0, 'interval' => 1, 'parameters' => array()),
|
danielebarchiesi@0
|
6803 ),
|
danielebarchiesi@0
|
6804 'pager_last' => array(
|
danielebarchiesi@0
|
6805 'variables' => array('text' => NULL, 'element' => 0, 'parameters' => array()),
|
danielebarchiesi@0
|
6806 ),
|
danielebarchiesi@0
|
6807 'pager_link' => array(
|
danielebarchiesi@0
|
6808 'variables' => array('text' => NULL, 'page_new' => NULL, 'element' => NULL, 'parameters' => array(), 'attributes' => array()),
|
danielebarchiesi@0
|
6809 ),
|
danielebarchiesi@0
|
6810 // From menu.inc.
|
danielebarchiesi@0
|
6811 'menu_link' => array(
|
danielebarchiesi@0
|
6812 'render element' => 'element',
|
danielebarchiesi@0
|
6813 ),
|
danielebarchiesi@0
|
6814 'menu_tree' => array(
|
danielebarchiesi@0
|
6815 'render element' => 'tree',
|
danielebarchiesi@0
|
6816 ),
|
danielebarchiesi@0
|
6817 'menu_local_task' => array(
|
danielebarchiesi@0
|
6818 'render element' => 'element',
|
danielebarchiesi@0
|
6819 ),
|
danielebarchiesi@0
|
6820 'menu_local_action' => array(
|
danielebarchiesi@0
|
6821 'render element' => 'element',
|
danielebarchiesi@0
|
6822 ),
|
danielebarchiesi@0
|
6823 'menu_local_tasks' => array(
|
danielebarchiesi@0
|
6824 'variables' => array('primary' => array(), 'secondary' => array()),
|
danielebarchiesi@0
|
6825 ),
|
danielebarchiesi@0
|
6826 // From form.inc.
|
danielebarchiesi@0
|
6827 'select' => array(
|
danielebarchiesi@0
|
6828 'render element' => 'element',
|
danielebarchiesi@0
|
6829 ),
|
danielebarchiesi@0
|
6830 'fieldset' => array(
|
danielebarchiesi@0
|
6831 'render element' => 'element',
|
danielebarchiesi@0
|
6832 ),
|
danielebarchiesi@0
|
6833 'radio' => array(
|
danielebarchiesi@0
|
6834 'render element' => 'element',
|
danielebarchiesi@0
|
6835 ),
|
danielebarchiesi@0
|
6836 'radios' => array(
|
danielebarchiesi@0
|
6837 'render element' => 'element',
|
danielebarchiesi@0
|
6838 ),
|
danielebarchiesi@0
|
6839 'date' => array(
|
danielebarchiesi@0
|
6840 'render element' => 'element',
|
danielebarchiesi@0
|
6841 ),
|
danielebarchiesi@0
|
6842 'exposed_filters' => array(
|
danielebarchiesi@0
|
6843 'render element' => 'form',
|
danielebarchiesi@0
|
6844 ),
|
danielebarchiesi@0
|
6845 'checkbox' => array(
|
danielebarchiesi@0
|
6846 'render element' => 'element',
|
danielebarchiesi@0
|
6847 ),
|
danielebarchiesi@0
|
6848 'checkboxes' => array(
|
danielebarchiesi@0
|
6849 'render element' => 'element',
|
danielebarchiesi@0
|
6850 ),
|
danielebarchiesi@0
|
6851 'button' => array(
|
danielebarchiesi@0
|
6852 'render element' => 'element',
|
danielebarchiesi@0
|
6853 ),
|
danielebarchiesi@0
|
6854 'image_button' => array(
|
danielebarchiesi@0
|
6855 'render element' => 'element',
|
danielebarchiesi@0
|
6856 ),
|
danielebarchiesi@0
|
6857 'hidden' => array(
|
danielebarchiesi@0
|
6858 'render element' => 'element',
|
danielebarchiesi@0
|
6859 ),
|
danielebarchiesi@0
|
6860 'textfield' => array(
|
danielebarchiesi@0
|
6861 'render element' => 'element',
|
danielebarchiesi@0
|
6862 ),
|
danielebarchiesi@0
|
6863 'form' => array(
|
danielebarchiesi@0
|
6864 'render element' => 'element',
|
danielebarchiesi@0
|
6865 ),
|
danielebarchiesi@0
|
6866 'textarea' => array(
|
danielebarchiesi@0
|
6867 'render element' => 'element',
|
danielebarchiesi@0
|
6868 ),
|
danielebarchiesi@0
|
6869 'password' => array(
|
danielebarchiesi@0
|
6870 'render element' => 'element',
|
danielebarchiesi@0
|
6871 ),
|
danielebarchiesi@0
|
6872 'file' => array(
|
danielebarchiesi@0
|
6873 'render element' => 'element',
|
danielebarchiesi@0
|
6874 ),
|
danielebarchiesi@0
|
6875 'tableselect' => array(
|
danielebarchiesi@0
|
6876 'render element' => 'element',
|
danielebarchiesi@0
|
6877 ),
|
danielebarchiesi@0
|
6878 'form_element' => array(
|
danielebarchiesi@0
|
6879 'render element' => 'element',
|
danielebarchiesi@0
|
6880 ),
|
danielebarchiesi@0
|
6881 'form_required_marker' => array(
|
danielebarchiesi@0
|
6882 'render element' => 'element',
|
danielebarchiesi@0
|
6883 ),
|
danielebarchiesi@0
|
6884 'form_element_label' => array(
|
danielebarchiesi@0
|
6885 'render element' => 'element',
|
danielebarchiesi@0
|
6886 ),
|
danielebarchiesi@0
|
6887 'vertical_tabs' => array(
|
danielebarchiesi@0
|
6888 'render element' => 'element',
|
danielebarchiesi@0
|
6889 ),
|
danielebarchiesi@0
|
6890 'container' => array(
|
danielebarchiesi@0
|
6891 'render element' => 'element',
|
danielebarchiesi@0
|
6892 ),
|
danielebarchiesi@0
|
6893 );
|
danielebarchiesi@0
|
6894 }
|
danielebarchiesi@0
|
6895
|
danielebarchiesi@0
|
6896 /**
|
danielebarchiesi@0
|
6897 * @addtogroup schemaapi
|
danielebarchiesi@0
|
6898 * @{
|
danielebarchiesi@0
|
6899 */
|
danielebarchiesi@0
|
6900
|
danielebarchiesi@0
|
6901 /**
|
danielebarchiesi@0
|
6902 * Creates all tables defined in a module's hook_schema().
|
danielebarchiesi@0
|
6903 *
|
danielebarchiesi@0
|
6904 * Note: This function does not pass the module's schema through
|
danielebarchiesi@0
|
6905 * hook_schema_alter(). The module's tables will be created exactly as the
|
danielebarchiesi@0
|
6906 * module defines them.
|
danielebarchiesi@0
|
6907 *
|
danielebarchiesi@0
|
6908 * @param $module
|
danielebarchiesi@0
|
6909 * The module for which the tables will be created.
|
danielebarchiesi@0
|
6910 */
|
danielebarchiesi@0
|
6911 function drupal_install_schema($module) {
|
danielebarchiesi@0
|
6912 $schema = drupal_get_schema_unprocessed($module);
|
danielebarchiesi@0
|
6913 _drupal_schema_initialize($schema, $module, FALSE);
|
danielebarchiesi@0
|
6914
|
danielebarchiesi@0
|
6915 foreach ($schema as $name => $table) {
|
danielebarchiesi@0
|
6916 db_create_table($name, $table);
|
danielebarchiesi@0
|
6917 }
|
danielebarchiesi@0
|
6918 }
|
danielebarchiesi@0
|
6919
|
danielebarchiesi@0
|
6920 /**
|
danielebarchiesi@0
|
6921 * Removes all tables defined in a module's hook_schema().
|
danielebarchiesi@0
|
6922 *
|
danielebarchiesi@0
|
6923 * Note: This function does not pass the module's schema through
|
danielebarchiesi@0
|
6924 * hook_schema_alter(). The module's tables will be created exactly as the
|
danielebarchiesi@0
|
6925 * module defines them.
|
danielebarchiesi@0
|
6926 *
|
danielebarchiesi@0
|
6927 * @param $module
|
danielebarchiesi@0
|
6928 * The module for which the tables will be removed.
|
danielebarchiesi@0
|
6929 *
|
danielebarchiesi@0
|
6930 * @return
|
danielebarchiesi@0
|
6931 * An array of arrays with the following key/value pairs:
|
danielebarchiesi@0
|
6932 * - success: a boolean indicating whether the query succeeded.
|
danielebarchiesi@0
|
6933 * - query: the SQL query(s) executed, passed through check_plain().
|
danielebarchiesi@0
|
6934 */
|
danielebarchiesi@0
|
6935 function drupal_uninstall_schema($module) {
|
danielebarchiesi@0
|
6936 $schema = drupal_get_schema_unprocessed($module);
|
danielebarchiesi@0
|
6937 _drupal_schema_initialize($schema, $module, FALSE);
|
danielebarchiesi@0
|
6938
|
danielebarchiesi@0
|
6939 foreach ($schema as $table) {
|
danielebarchiesi@0
|
6940 if (db_table_exists($table['name'])) {
|
danielebarchiesi@0
|
6941 db_drop_table($table['name']);
|
danielebarchiesi@0
|
6942 }
|
danielebarchiesi@0
|
6943 }
|
danielebarchiesi@0
|
6944 }
|
danielebarchiesi@0
|
6945
|
danielebarchiesi@0
|
6946 /**
|
danielebarchiesi@0
|
6947 * Returns the unprocessed and unaltered version of a module's schema.
|
danielebarchiesi@0
|
6948 *
|
danielebarchiesi@0
|
6949 * Use this function only if you explicitly need the original
|
danielebarchiesi@0
|
6950 * specification of a schema, as it was defined in a module's
|
danielebarchiesi@0
|
6951 * hook_schema(). No additional default values will be set,
|
danielebarchiesi@0
|
6952 * hook_schema_alter() is not invoked and these unprocessed
|
danielebarchiesi@0
|
6953 * definitions won't be cached.
|
danielebarchiesi@0
|
6954 *
|
danielebarchiesi@0
|
6955 * This function can be used to retrieve a schema specification in
|
danielebarchiesi@0
|
6956 * hook_schema(), so it allows you to derive your tables from existing
|
danielebarchiesi@0
|
6957 * specifications.
|
danielebarchiesi@0
|
6958 *
|
danielebarchiesi@0
|
6959 * It is also used by drupal_install_schema() and
|
danielebarchiesi@0
|
6960 * drupal_uninstall_schema() to ensure that a module's tables are
|
danielebarchiesi@0
|
6961 * created exactly as specified without any changes introduced by a
|
danielebarchiesi@0
|
6962 * module that implements hook_schema_alter().
|
danielebarchiesi@0
|
6963 *
|
danielebarchiesi@0
|
6964 * @param $module
|
danielebarchiesi@0
|
6965 * The module to which the table belongs.
|
danielebarchiesi@0
|
6966 * @param $table
|
danielebarchiesi@0
|
6967 * The name of the table. If not given, the module's complete schema
|
danielebarchiesi@0
|
6968 * is returned.
|
danielebarchiesi@0
|
6969 */
|
danielebarchiesi@0
|
6970 function drupal_get_schema_unprocessed($module, $table = NULL) {
|
danielebarchiesi@0
|
6971 // Load the .install file to get hook_schema.
|
danielebarchiesi@0
|
6972 module_load_install($module);
|
danielebarchiesi@0
|
6973 $schema = module_invoke($module, 'schema');
|
danielebarchiesi@0
|
6974
|
danielebarchiesi@0
|
6975 if (isset($table) && isset($schema[$table])) {
|
danielebarchiesi@0
|
6976 return $schema[$table];
|
danielebarchiesi@0
|
6977 }
|
danielebarchiesi@0
|
6978 elseif (!empty($schema)) {
|
danielebarchiesi@0
|
6979 return $schema;
|
danielebarchiesi@0
|
6980 }
|
danielebarchiesi@0
|
6981 return array();
|
danielebarchiesi@0
|
6982 }
|
danielebarchiesi@0
|
6983
|
danielebarchiesi@0
|
6984 /**
|
danielebarchiesi@0
|
6985 * Fills in required default values for table definitions from hook_schema().
|
danielebarchiesi@0
|
6986 *
|
danielebarchiesi@0
|
6987 * @param $schema
|
danielebarchiesi@0
|
6988 * The schema definition array as it was returned by the module's
|
danielebarchiesi@0
|
6989 * hook_schema().
|
danielebarchiesi@0
|
6990 * @param $module
|
danielebarchiesi@0
|
6991 * The module for which hook_schema() was invoked.
|
danielebarchiesi@0
|
6992 * @param $remove_descriptions
|
danielebarchiesi@0
|
6993 * (optional) Whether to additionally remove 'description' keys of all tables
|
danielebarchiesi@0
|
6994 * and fields to improve performance of serialize() and unserialize().
|
danielebarchiesi@0
|
6995 * Defaults to TRUE.
|
danielebarchiesi@0
|
6996 */
|
danielebarchiesi@0
|
6997 function _drupal_schema_initialize(&$schema, $module, $remove_descriptions = TRUE) {
|
danielebarchiesi@0
|
6998 // Set the name and module key for all tables.
|
danielebarchiesi@0
|
6999 foreach ($schema as $name => &$table) {
|
danielebarchiesi@0
|
7000 if (empty($table['module'])) {
|
danielebarchiesi@0
|
7001 $table['module'] = $module;
|
danielebarchiesi@0
|
7002 }
|
danielebarchiesi@0
|
7003 if (!isset($table['name'])) {
|
danielebarchiesi@0
|
7004 $table['name'] = $name;
|
danielebarchiesi@0
|
7005 }
|
danielebarchiesi@0
|
7006 if ($remove_descriptions) {
|
danielebarchiesi@0
|
7007 unset($table['description']);
|
danielebarchiesi@0
|
7008 foreach ($table['fields'] as &$field) {
|
danielebarchiesi@0
|
7009 unset($field['description']);
|
danielebarchiesi@0
|
7010 }
|
danielebarchiesi@0
|
7011 }
|
danielebarchiesi@0
|
7012 }
|
danielebarchiesi@0
|
7013 }
|
danielebarchiesi@0
|
7014
|
danielebarchiesi@0
|
7015 /**
|
danielebarchiesi@0
|
7016 * Retrieves a list of fields from a table schema.
|
danielebarchiesi@0
|
7017 *
|
danielebarchiesi@0
|
7018 * The returned list is suitable for use in an SQL query.
|
danielebarchiesi@0
|
7019 *
|
danielebarchiesi@0
|
7020 * @param $table
|
danielebarchiesi@0
|
7021 * The name of the table from which to retrieve fields.
|
danielebarchiesi@0
|
7022 * @param
|
danielebarchiesi@0
|
7023 * An optional prefix to to all fields.
|
danielebarchiesi@0
|
7024 *
|
danielebarchiesi@0
|
7025 * @return An array of fields.
|
danielebarchiesi@0
|
7026 */
|
danielebarchiesi@0
|
7027 function drupal_schema_fields_sql($table, $prefix = NULL) {
|
danielebarchiesi@0
|
7028 $schema = drupal_get_schema($table);
|
danielebarchiesi@0
|
7029 $fields = array_keys($schema['fields']);
|
danielebarchiesi@0
|
7030 if ($prefix) {
|
danielebarchiesi@0
|
7031 $columns = array();
|
danielebarchiesi@0
|
7032 foreach ($fields as $field) {
|
danielebarchiesi@0
|
7033 $columns[] = "$prefix.$field";
|
danielebarchiesi@0
|
7034 }
|
danielebarchiesi@0
|
7035 return $columns;
|
danielebarchiesi@0
|
7036 }
|
danielebarchiesi@0
|
7037 else {
|
danielebarchiesi@0
|
7038 return $fields;
|
danielebarchiesi@0
|
7039 }
|
danielebarchiesi@0
|
7040 }
|
danielebarchiesi@0
|
7041
|
danielebarchiesi@0
|
7042 /**
|
danielebarchiesi@0
|
7043 * Saves (inserts or updates) a record to the database based upon the schema.
|
danielebarchiesi@0
|
7044 *
|
danielebarchiesi@0
|
7045 * Do not use drupal_write_record() within hook_update_N() functions, since the
|
danielebarchiesi@0
|
7046 * database schema cannot be relied upon when a user is running a series of
|
danielebarchiesi@0
|
7047 * updates. Instead, use db_insert() or db_update() to save the record.
|
danielebarchiesi@0
|
7048 *
|
danielebarchiesi@0
|
7049 * @param $table
|
danielebarchiesi@0
|
7050 * The name of the table; this must be defined by a hook_schema()
|
danielebarchiesi@0
|
7051 * implementation.
|
danielebarchiesi@0
|
7052 * @param $record
|
danielebarchiesi@0
|
7053 * An object or array representing the record to write, passed in by
|
danielebarchiesi@0
|
7054 * reference. If inserting a new record, values not provided in $record will
|
danielebarchiesi@0
|
7055 * be populated in $record and in the database with the default values from
|
danielebarchiesi@0
|
7056 * the schema, as well as a single serial (auto-increment) field (if present).
|
danielebarchiesi@0
|
7057 * If updating an existing record, only provided values are updated in the
|
danielebarchiesi@0
|
7058 * database, and $record is not modified.
|
danielebarchiesi@0
|
7059 * @param $primary_keys
|
danielebarchiesi@0
|
7060 * To indicate that this is a new record to be inserted, omit this argument.
|
danielebarchiesi@0
|
7061 * If this is an update, this argument specifies the primary keys' field
|
danielebarchiesi@0
|
7062 * names. If there is only 1 field in the key, you may pass in a string; if
|
danielebarchiesi@0
|
7063 * there are multiple fields in the key, pass in an array.
|
danielebarchiesi@0
|
7064 *
|
danielebarchiesi@0
|
7065 * @return
|
danielebarchiesi@0
|
7066 * If the record insert or update failed, returns FALSE. If it succeeded,
|
danielebarchiesi@0
|
7067 * returns SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
|
danielebarchiesi@0
|
7068 */
|
danielebarchiesi@0
|
7069 function drupal_write_record($table, &$record, $primary_keys = array()) {
|
danielebarchiesi@0
|
7070 // Standardize $primary_keys to an array.
|
danielebarchiesi@0
|
7071 if (is_string($primary_keys)) {
|
danielebarchiesi@0
|
7072 $primary_keys = array($primary_keys);
|
danielebarchiesi@0
|
7073 }
|
danielebarchiesi@0
|
7074
|
danielebarchiesi@0
|
7075 $schema = drupal_get_schema($table);
|
danielebarchiesi@0
|
7076 if (empty($schema)) {
|
danielebarchiesi@0
|
7077 return FALSE;
|
danielebarchiesi@0
|
7078 }
|
danielebarchiesi@0
|
7079
|
danielebarchiesi@0
|
7080 $object = (object) $record;
|
danielebarchiesi@0
|
7081 $fields = array();
|
danielebarchiesi@0
|
7082
|
danielebarchiesi@0
|
7083 // Go through the schema to determine fields to write.
|
danielebarchiesi@0
|
7084 foreach ($schema['fields'] as $field => $info) {
|
danielebarchiesi@0
|
7085 if ($info['type'] == 'serial') {
|
danielebarchiesi@0
|
7086 // Skip serial types if we are updating.
|
danielebarchiesi@0
|
7087 if (!empty($primary_keys)) {
|
danielebarchiesi@0
|
7088 continue;
|
danielebarchiesi@0
|
7089 }
|
danielebarchiesi@0
|
7090 // Track serial field so we can helpfully populate them after the query.
|
danielebarchiesi@0
|
7091 // NOTE: Each table should come with one serial field only.
|
danielebarchiesi@0
|
7092 $serial = $field;
|
danielebarchiesi@0
|
7093 }
|
danielebarchiesi@0
|
7094
|
danielebarchiesi@0
|
7095 // Skip field if it is in $primary_keys as it is unnecessary to update a
|
danielebarchiesi@0
|
7096 // field to the value it is already set to.
|
danielebarchiesi@0
|
7097 if (in_array($field, $primary_keys)) {
|
danielebarchiesi@0
|
7098 continue;
|
danielebarchiesi@0
|
7099 }
|
danielebarchiesi@0
|
7100
|
danielebarchiesi@0
|
7101 if (!property_exists($object, $field)) {
|
danielebarchiesi@0
|
7102 // Skip fields that are not provided, default values are already known
|
danielebarchiesi@0
|
7103 // by the database.
|
danielebarchiesi@0
|
7104 continue;
|
danielebarchiesi@0
|
7105 }
|
danielebarchiesi@0
|
7106
|
danielebarchiesi@0
|
7107 // Build array of fields to update or insert.
|
danielebarchiesi@0
|
7108 if (empty($info['serialize'])) {
|
danielebarchiesi@0
|
7109 $fields[$field] = $object->$field;
|
danielebarchiesi@0
|
7110 }
|
danielebarchiesi@0
|
7111 else {
|
danielebarchiesi@0
|
7112 $fields[$field] = serialize($object->$field);
|
danielebarchiesi@0
|
7113 }
|
danielebarchiesi@0
|
7114
|
danielebarchiesi@0
|
7115 // Type cast to proper datatype, except when the value is NULL and the
|
danielebarchiesi@0
|
7116 // column allows this.
|
danielebarchiesi@0
|
7117 //
|
danielebarchiesi@0
|
7118 // MySQL PDO silently casts e.g. FALSE and '' to 0 when inserting the value
|
danielebarchiesi@0
|
7119 // into an integer column, but PostgreSQL PDO does not. Also type cast NULL
|
danielebarchiesi@0
|
7120 // when the column does not allow this.
|
danielebarchiesi@0
|
7121 if (isset($object->$field) || !empty($info['not null'])) {
|
danielebarchiesi@0
|
7122 if ($info['type'] == 'int' || $info['type'] == 'serial') {
|
danielebarchiesi@0
|
7123 $fields[$field] = (int) $fields[$field];
|
danielebarchiesi@0
|
7124 }
|
danielebarchiesi@0
|
7125 elseif ($info['type'] == 'float') {
|
danielebarchiesi@0
|
7126 $fields[$field] = (float) $fields[$field];
|
danielebarchiesi@0
|
7127 }
|
danielebarchiesi@0
|
7128 else {
|
danielebarchiesi@0
|
7129 $fields[$field] = (string) $fields[$field];
|
danielebarchiesi@0
|
7130 }
|
danielebarchiesi@0
|
7131 }
|
danielebarchiesi@0
|
7132 }
|
danielebarchiesi@0
|
7133
|
danielebarchiesi@0
|
7134 if (empty($fields)) {
|
danielebarchiesi@0
|
7135 return;
|
danielebarchiesi@0
|
7136 }
|
danielebarchiesi@0
|
7137
|
danielebarchiesi@0
|
7138 // Build the SQL.
|
danielebarchiesi@0
|
7139 if (empty($primary_keys)) {
|
danielebarchiesi@0
|
7140 // We are doing an insert.
|
danielebarchiesi@0
|
7141 $options = array('return' => Database::RETURN_INSERT_ID);
|
danielebarchiesi@0
|
7142 if (isset($serial) && isset($fields[$serial])) {
|
danielebarchiesi@0
|
7143 // If the serial column has been explicitly set with an ID, then we don't
|
danielebarchiesi@0
|
7144 // require the database to return the last insert id.
|
danielebarchiesi@0
|
7145 if ($fields[$serial]) {
|
danielebarchiesi@0
|
7146 $options['return'] = Database::RETURN_AFFECTED;
|
danielebarchiesi@0
|
7147 }
|
danielebarchiesi@0
|
7148 // If a serial column does exist with no value (i.e. 0) then remove it as
|
danielebarchiesi@0
|
7149 // the database will insert the correct value for us.
|
danielebarchiesi@0
|
7150 else {
|
danielebarchiesi@0
|
7151 unset($fields[$serial]);
|
danielebarchiesi@0
|
7152 }
|
danielebarchiesi@0
|
7153 }
|
danielebarchiesi@0
|
7154 $query = db_insert($table, $options)->fields($fields);
|
danielebarchiesi@0
|
7155 $return = SAVED_NEW;
|
danielebarchiesi@0
|
7156 }
|
danielebarchiesi@0
|
7157 else {
|
danielebarchiesi@0
|
7158 $query = db_update($table)->fields($fields);
|
danielebarchiesi@0
|
7159 foreach ($primary_keys as $key) {
|
danielebarchiesi@0
|
7160 $query->condition($key, $object->$key);
|
danielebarchiesi@0
|
7161 }
|
danielebarchiesi@0
|
7162 $return = SAVED_UPDATED;
|
danielebarchiesi@0
|
7163 }
|
danielebarchiesi@0
|
7164
|
danielebarchiesi@0
|
7165 // Execute the SQL.
|
danielebarchiesi@0
|
7166 if ($query_return = $query->execute()) {
|
danielebarchiesi@0
|
7167 if (isset($serial)) {
|
danielebarchiesi@0
|
7168 // If the database was not told to return the last insert id, it will be
|
danielebarchiesi@0
|
7169 // because we already know it.
|
danielebarchiesi@0
|
7170 if (isset($options) && $options['return'] != Database::RETURN_INSERT_ID) {
|
danielebarchiesi@0
|
7171 $object->$serial = $fields[$serial];
|
danielebarchiesi@0
|
7172 }
|
danielebarchiesi@0
|
7173 else {
|
danielebarchiesi@0
|
7174 $object->$serial = $query_return;
|
danielebarchiesi@0
|
7175 }
|
danielebarchiesi@0
|
7176 }
|
danielebarchiesi@0
|
7177 }
|
danielebarchiesi@0
|
7178 // If we have a single-field primary key but got no insert ID, the
|
danielebarchiesi@0
|
7179 // query failed. Note that we explicitly check for FALSE, because
|
danielebarchiesi@0
|
7180 // a valid update query which doesn't change any values will return
|
danielebarchiesi@0
|
7181 // zero (0) affected rows.
|
danielebarchiesi@0
|
7182 elseif ($query_return === FALSE && count($primary_keys) == 1) {
|
danielebarchiesi@0
|
7183 $return = FALSE;
|
danielebarchiesi@0
|
7184 }
|
danielebarchiesi@0
|
7185
|
danielebarchiesi@0
|
7186 // If we are inserting, populate empty fields with default values.
|
danielebarchiesi@0
|
7187 if (empty($primary_keys)) {
|
danielebarchiesi@0
|
7188 foreach ($schema['fields'] as $field => $info) {
|
danielebarchiesi@0
|
7189 if (isset($info['default']) && !property_exists($object, $field)) {
|
danielebarchiesi@0
|
7190 $object->$field = $info['default'];
|
danielebarchiesi@0
|
7191 }
|
danielebarchiesi@0
|
7192 }
|
danielebarchiesi@0
|
7193 }
|
danielebarchiesi@0
|
7194
|
danielebarchiesi@0
|
7195 // If we began with an array, convert back.
|
danielebarchiesi@0
|
7196 if (is_array($record)) {
|
danielebarchiesi@0
|
7197 $record = (array) $object;
|
danielebarchiesi@0
|
7198 }
|
danielebarchiesi@0
|
7199
|
danielebarchiesi@0
|
7200 return $return;
|
danielebarchiesi@0
|
7201 }
|
danielebarchiesi@0
|
7202
|
danielebarchiesi@0
|
7203 /**
|
danielebarchiesi@0
|
7204 * @} End of "addtogroup schemaapi".
|
danielebarchiesi@0
|
7205 */
|
danielebarchiesi@0
|
7206
|
danielebarchiesi@0
|
7207 /**
|
danielebarchiesi@0
|
7208 * Parses Drupal module and theme .info files.
|
danielebarchiesi@0
|
7209 *
|
danielebarchiesi@0
|
7210 * Info files are NOT for placing arbitrary theme and module-specific settings.
|
danielebarchiesi@0
|
7211 * Use variable_get() and variable_set() for that.
|
danielebarchiesi@0
|
7212 *
|
danielebarchiesi@0
|
7213 * Information stored in a module .info file:
|
danielebarchiesi@0
|
7214 * - name: The real name of the module for display purposes.
|
danielebarchiesi@0
|
7215 * - description: A brief description of the module.
|
danielebarchiesi@0
|
7216 * - dependencies: An array of shortnames of other modules this module requires.
|
danielebarchiesi@0
|
7217 * - package: The name of the package of modules this module belongs to.
|
danielebarchiesi@0
|
7218 *
|
danielebarchiesi@0
|
7219 * See forum.info for an example of a module .info file.
|
danielebarchiesi@0
|
7220 *
|
danielebarchiesi@0
|
7221 * Information stored in a theme .info file:
|
danielebarchiesi@0
|
7222 * - name: The real name of the theme for display purposes.
|
danielebarchiesi@0
|
7223 * - description: Brief description.
|
danielebarchiesi@0
|
7224 * - screenshot: Path to screenshot relative to the theme's .info file.
|
danielebarchiesi@0
|
7225 * - engine: Theme engine; typically phptemplate.
|
danielebarchiesi@0
|
7226 * - base: Name of a base theme, if applicable; e.g., base = zen.
|
danielebarchiesi@0
|
7227 * - regions: Listed regions; e.g., region[left] = Left sidebar.
|
danielebarchiesi@0
|
7228 * - features: Features available; e.g., features[] = logo.
|
danielebarchiesi@0
|
7229 * - stylesheets: Theme stylesheets; e.g., stylesheets[all][] = my-style.css.
|
danielebarchiesi@0
|
7230 * - scripts: Theme scripts; e.g., scripts[] = my-script.js.
|
danielebarchiesi@0
|
7231 *
|
danielebarchiesi@0
|
7232 * See bartik.info for an example of a theme .info file.
|
danielebarchiesi@0
|
7233 *
|
danielebarchiesi@0
|
7234 * @param $filename
|
danielebarchiesi@0
|
7235 * The file we are parsing. Accepts file with relative or absolute path.
|
danielebarchiesi@0
|
7236 *
|
danielebarchiesi@0
|
7237 * @return
|
danielebarchiesi@0
|
7238 * The info array.
|
danielebarchiesi@0
|
7239 *
|
danielebarchiesi@0
|
7240 * @see drupal_parse_info_format()
|
danielebarchiesi@0
|
7241 */
|
danielebarchiesi@0
|
7242 function drupal_parse_info_file($filename) {
|
danielebarchiesi@0
|
7243 $info = &drupal_static(__FUNCTION__, array());
|
danielebarchiesi@0
|
7244
|
danielebarchiesi@0
|
7245 if (!isset($info[$filename])) {
|
danielebarchiesi@0
|
7246 if (!file_exists($filename)) {
|
danielebarchiesi@0
|
7247 $info[$filename] = array();
|
danielebarchiesi@0
|
7248 }
|
danielebarchiesi@0
|
7249 else {
|
danielebarchiesi@0
|
7250 $data = file_get_contents($filename);
|
danielebarchiesi@0
|
7251 $info[$filename] = drupal_parse_info_format($data);
|
danielebarchiesi@0
|
7252 }
|
danielebarchiesi@0
|
7253 }
|
danielebarchiesi@0
|
7254 return $info[$filename];
|
danielebarchiesi@0
|
7255 }
|
danielebarchiesi@0
|
7256
|
danielebarchiesi@0
|
7257 /**
|
danielebarchiesi@0
|
7258 * Parses data in Drupal's .info format.
|
danielebarchiesi@0
|
7259 *
|
danielebarchiesi@0
|
7260 * Data should be in an .ini-like format to specify values. White-space
|
danielebarchiesi@0
|
7261 * generally doesn't matter, except inside values:
|
danielebarchiesi@0
|
7262 * @code
|
danielebarchiesi@0
|
7263 * key = value
|
danielebarchiesi@0
|
7264 * key = "value"
|
danielebarchiesi@0
|
7265 * key = 'value'
|
danielebarchiesi@0
|
7266 * key = "multi-line
|
danielebarchiesi@0
|
7267 * value"
|
danielebarchiesi@0
|
7268 * key = 'multi-line
|
danielebarchiesi@0
|
7269 * value'
|
danielebarchiesi@0
|
7270 * key
|
danielebarchiesi@0
|
7271 * =
|
danielebarchiesi@0
|
7272 * 'value'
|
danielebarchiesi@0
|
7273 * @endcode
|
danielebarchiesi@0
|
7274 *
|
danielebarchiesi@0
|
7275 * Arrays are created using a HTTP GET alike syntax:
|
danielebarchiesi@0
|
7276 * @code
|
danielebarchiesi@0
|
7277 * key[] = "numeric array"
|
danielebarchiesi@0
|
7278 * key[index] = "associative array"
|
danielebarchiesi@0
|
7279 * key[index][] = "nested numeric array"
|
danielebarchiesi@0
|
7280 * key[index][index] = "nested associative array"
|
danielebarchiesi@0
|
7281 * @endcode
|
danielebarchiesi@0
|
7282 *
|
danielebarchiesi@0
|
7283 * PHP constants are substituted in, but only when used as the entire value.
|
danielebarchiesi@0
|
7284 * Comments should start with a semi-colon at the beginning of a line.
|
danielebarchiesi@0
|
7285 *
|
danielebarchiesi@0
|
7286 * @param $data
|
danielebarchiesi@0
|
7287 * A string to parse.
|
danielebarchiesi@0
|
7288 *
|
danielebarchiesi@0
|
7289 * @return
|
danielebarchiesi@0
|
7290 * The info array.
|
danielebarchiesi@0
|
7291 *
|
danielebarchiesi@0
|
7292 * @see drupal_parse_info_file()
|
danielebarchiesi@0
|
7293 */
|
danielebarchiesi@0
|
7294 function drupal_parse_info_format($data) {
|
danielebarchiesi@0
|
7295 $info = array();
|
danielebarchiesi@0
|
7296 $constants = get_defined_constants();
|
danielebarchiesi@0
|
7297
|
danielebarchiesi@0
|
7298 if (preg_match_all('
|
danielebarchiesi@0
|
7299 @^\s* # Start at the beginning of a line, ignoring leading whitespace
|
danielebarchiesi@0
|
7300 ((?:
|
danielebarchiesi@0
|
7301 [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
|
danielebarchiesi@0
|
7302 \[[^\[\]]*\] # unless they are balanced and not nested
|
danielebarchiesi@0
|
7303 )+?)
|
danielebarchiesi@0
|
7304 \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
|
danielebarchiesi@0
|
7305 (?:
|
danielebarchiesi@0
|
7306 ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes
|
danielebarchiesi@0
|
7307 (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
|
danielebarchiesi@0
|
7308 ([^\r\n]*?) # Non-quoted string
|
danielebarchiesi@0
|
7309 )\s*$ # Stop at the next end of a line, ignoring trailing whitespace
|
danielebarchiesi@0
|
7310 @msx', $data, $matches, PREG_SET_ORDER)) {
|
danielebarchiesi@0
|
7311 foreach ($matches as $match) {
|
danielebarchiesi@0
|
7312 // Fetch the key and value string.
|
danielebarchiesi@0
|
7313 $i = 0;
|
danielebarchiesi@0
|
7314 foreach (array('key', 'value1', 'value2', 'value3') as $var) {
|
danielebarchiesi@0
|
7315 $$var = isset($match[++$i]) ? $match[$i] : '';
|
danielebarchiesi@0
|
7316 }
|
danielebarchiesi@0
|
7317 $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
|
danielebarchiesi@0
|
7318
|
danielebarchiesi@0
|
7319 // Parse array syntax.
|
danielebarchiesi@0
|
7320 $keys = preg_split('/\]?\[/', rtrim($key, ']'));
|
danielebarchiesi@0
|
7321 $last = array_pop($keys);
|
danielebarchiesi@0
|
7322 $parent = &$info;
|
danielebarchiesi@0
|
7323
|
danielebarchiesi@0
|
7324 // Create nested arrays.
|
danielebarchiesi@0
|
7325 foreach ($keys as $key) {
|
danielebarchiesi@0
|
7326 if ($key == '') {
|
danielebarchiesi@0
|
7327 $key = count($parent);
|
danielebarchiesi@0
|
7328 }
|
danielebarchiesi@0
|
7329 if (!isset($parent[$key]) || !is_array($parent[$key])) {
|
danielebarchiesi@0
|
7330 $parent[$key] = array();
|
danielebarchiesi@0
|
7331 }
|
danielebarchiesi@0
|
7332 $parent = &$parent[$key];
|
danielebarchiesi@0
|
7333 }
|
danielebarchiesi@0
|
7334
|
danielebarchiesi@0
|
7335 // Handle PHP constants.
|
danielebarchiesi@0
|
7336 if (isset($constants[$value])) {
|
danielebarchiesi@0
|
7337 $value = $constants[$value];
|
danielebarchiesi@0
|
7338 }
|
danielebarchiesi@0
|
7339
|
danielebarchiesi@0
|
7340 // Insert actual value.
|
danielebarchiesi@0
|
7341 if ($last == '') {
|
danielebarchiesi@0
|
7342 $last = count($parent);
|
danielebarchiesi@0
|
7343 }
|
danielebarchiesi@0
|
7344 $parent[$last] = $value;
|
danielebarchiesi@0
|
7345 }
|
danielebarchiesi@0
|
7346 }
|
danielebarchiesi@0
|
7347
|
danielebarchiesi@0
|
7348 return $info;
|
danielebarchiesi@0
|
7349 }
|
danielebarchiesi@0
|
7350
|
danielebarchiesi@0
|
7351 /**
|
danielebarchiesi@0
|
7352 * Returns a list of severity levels, as defined in RFC 3164.
|
danielebarchiesi@0
|
7353 *
|
danielebarchiesi@0
|
7354 * @return
|
danielebarchiesi@0
|
7355 * Array of the possible severity levels for log messages.
|
danielebarchiesi@0
|
7356 *
|
danielebarchiesi@0
|
7357 * @see http://www.ietf.org/rfc/rfc3164.txt
|
danielebarchiesi@0
|
7358 * @see watchdog()
|
danielebarchiesi@0
|
7359 * @ingroup logging_severity_levels
|
danielebarchiesi@0
|
7360 */
|
danielebarchiesi@0
|
7361 function watchdog_severity_levels() {
|
danielebarchiesi@0
|
7362 return array(
|
danielebarchiesi@0
|
7363 WATCHDOG_EMERGENCY => t('emergency'),
|
danielebarchiesi@0
|
7364 WATCHDOG_ALERT => t('alert'),
|
danielebarchiesi@0
|
7365 WATCHDOG_CRITICAL => t('critical'),
|
danielebarchiesi@0
|
7366 WATCHDOG_ERROR => t('error'),
|
danielebarchiesi@0
|
7367 WATCHDOG_WARNING => t('warning'),
|
danielebarchiesi@0
|
7368 WATCHDOG_NOTICE => t('notice'),
|
danielebarchiesi@0
|
7369 WATCHDOG_INFO => t('info'),
|
danielebarchiesi@0
|
7370 WATCHDOG_DEBUG => t('debug'),
|
danielebarchiesi@0
|
7371 );
|
danielebarchiesi@0
|
7372 }
|
danielebarchiesi@0
|
7373
|
danielebarchiesi@0
|
7374
|
danielebarchiesi@0
|
7375 /**
|
danielebarchiesi@0
|
7376 * Explodes a string of tags into an array.
|
danielebarchiesi@0
|
7377 *
|
danielebarchiesi@0
|
7378 * @see drupal_implode_tags()
|
danielebarchiesi@0
|
7379 */
|
danielebarchiesi@0
|
7380 function drupal_explode_tags($tags) {
|
danielebarchiesi@0
|
7381 // This regexp allows the following types of user input:
|
danielebarchiesi@0
|
7382 // this, "somecompany, llc", "and ""this"" w,o.rks", foo bar
|
danielebarchiesi@0
|
7383 $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
|
danielebarchiesi@0
|
7384 preg_match_all($regexp, $tags, $matches);
|
danielebarchiesi@0
|
7385 $typed_tags = array_unique($matches[1]);
|
danielebarchiesi@0
|
7386
|
danielebarchiesi@0
|
7387 $tags = array();
|
danielebarchiesi@0
|
7388 foreach ($typed_tags as $tag) {
|
danielebarchiesi@0
|
7389 // If a user has escaped a term (to demonstrate that it is a group,
|
danielebarchiesi@0
|
7390 // or includes a comma or quote character), we remove the escape
|
danielebarchiesi@0
|
7391 // formatting so to save the term into the database as the user intends.
|
danielebarchiesi@0
|
7392 $tag = trim(str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $tag)));
|
danielebarchiesi@0
|
7393 if ($tag != "") {
|
danielebarchiesi@0
|
7394 $tags[] = $tag;
|
danielebarchiesi@0
|
7395 }
|
danielebarchiesi@0
|
7396 }
|
danielebarchiesi@0
|
7397
|
danielebarchiesi@0
|
7398 return $tags;
|
danielebarchiesi@0
|
7399 }
|
danielebarchiesi@0
|
7400
|
danielebarchiesi@0
|
7401 /**
|
danielebarchiesi@0
|
7402 * Implodes an array of tags into a string.
|
danielebarchiesi@0
|
7403 *
|
danielebarchiesi@0
|
7404 * @see drupal_explode_tags()
|
danielebarchiesi@0
|
7405 */
|
danielebarchiesi@0
|
7406 function drupal_implode_tags($tags) {
|
danielebarchiesi@0
|
7407 $encoded_tags = array();
|
danielebarchiesi@0
|
7408 foreach ($tags as $tag) {
|
danielebarchiesi@0
|
7409 // Commas and quotes in tag names are special cases, so encode them.
|
danielebarchiesi@0
|
7410 if (strpos($tag, ',') !== FALSE || strpos($tag, '"') !== FALSE) {
|
danielebarchiesi@0
|
7411 $tag = '"' . str_replace('"', '""', $tag) . '"';
|
danielebarchiesi@0
|
7412 }
|
danielebarchiesi@0
|
7413
|
danielebarchiesi@0
|
7414 $encoded_tags[] = $tag;
|
danielebarchiesi@0
|
7415 }
|
danielebarchiesi@0
|
7416 return implode(', ', $encoded_tags);
|
danielebarchiesi@0
|
7417 }
|
danielebarchiesi@0
|
7418
|
danielebarchiesi@0
|
7419 /**
|
danielebarchiesi@0
|
7420 * Flushes all cached data on the site.
|
danielebarchiesi@0
|
7421 *
|
danielebarchiesi@0
|
7422 * Empties cache tables, rebuilds the menu cache and theme registries, and
|
danielebarchiesi@0
|
7423 * invokes a hook so that other modules' cache data can be cleared as well.
|
danielebarchiesi@0
|
7424 */
|
danielebarchiesi@0
|
7425 function drupal_flush_all_caches() {
|
danielebarchiesi@0
|
7426 // Change query-strings on css/js files to enforce reload for all users.
|
danielebarchiesi@0
|
7427 _drupal_flush_css_js();
|
danielebarchiesi@0
|
7428
|
danielebarchiesi@0
|
7429 registry_rebuild();
|
danielebarchiesi@0
|
7430 drupal_clear_css_cache();
|
danielebarchiesi@0
|
7431 drupal_clear_js_cache();
|
danielebarchiesi@0
|
7432
|
danielebarchiesi@0
|
7433 // Rebuild the theme data. Note that the module data is rebuilt above, as
|
danielebarchiesi@0
|
7434 // part of registry_rebuild().
|
danielebarchiesi@0
|
7435 system_rebuild_theme_data();
|
danielebarchiesi@0
|
7436 drupal_theme_rebuild();
|
danielebarchiesi@0
|
7437
|
danielebarchiesi@0
|
7438 entity_info_cache_clear();
|
danielebarchiesi@0
|
7439 node_types_rebuild();
|
danielebarchiesi@0
|
7440 // node_menu() defines menu items based on node types so it needs to come
|
danielebarchiesi@0
|
7441 // after node types are rebuilt.
|
danielebarchiesi@0
|
7442 menu_rebuild();
|
danielebarchiesi@0
|
7443
|
danielebarchiesi@0
|
7444 // Synchronize to catch any actions that were added or removed.
|
danielebarchiesi@0
|
7445 actions_synchronize();
|
danielebarchiesi@0
|
7446
|
danielebarchiesi@0
|
7447 // Don't clear cache_form - in-progress form submissions may break.
|
danielebarchiesi@0
|
7448 // Ordered so clearing the page cache will always be the last action.
|
danielebarchiesi@0
|
7449 $core = array('cache', 'cache_path', 'cache_filter', 'cache_bootstrap', 'cache_page');
|
danielebarchiesi@0
|
7450 $cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
|
danielebarchiesi@0
|
7451 foreach ($cache_tables as $table) {
|
danielebarchiesi@0
|
7452 cache_clear_all('*', $table, TRUE);
|
danielebarchiesi@0
|
7453 }
|
danielebarchiesi@0
|
7454
|
danielebarchiesi@0
|
7455 // Rebuild the bootstrap module list. We do this here so that developers
|
danielebarchiesi@0
|
7456 // can get new hook_boot() implementations registered without having to
|
danielebarchiesi@0
|
7457 // write a hook_update_N() function.
|
danielebarchiesi@0
|
7458 _system_update_bootstrap_status();
|
danielebarchiesi@0
|
7459 }
|
danielebarchiesi@0
|
7460
|
danielebarchiesi@0
|
7461 /**
|
danielebarchiesi@0
|
7462 * Changes the dummy query string added to all CSS and JavaScript files.
|
danielebarchiesi@0
|
7463 *
|
danielebarchiesi@0
|
7464 * Changing the dummy query string appended to CSS and JavaScript files forces
|
danielebarchiesi@0
|
7465 * all browsers to reload fresh files.
|
danielebarchiesi@0
|
7466 */
|
danielebarchiesi@0
|
7467 function _drupal_flush_css_js() {
|
danielebarchiesi@0
|
7468 // The timestamp is converted to base 36 in order to make it more compact.
|
danielebarchiesi@0
|
7469 variable_set('css_js_query_string', base_convert(REQUEST_TIME, 10, 36));
|
danielebarchiesi@0
|
7470 }
|
danielebarchiesi@0
|
7471
|
danielebarchiesi@0
|
7472 /**
|
danielebarchiesi@0
|
7473 * Outputs debug information.
|
danielebarchiesi@0
|
7474 *
|
danielebarchiesi@0
|
7475 * The debug information is passed on to trigger_error() after being converted
|
danielebarchiesi@0
|
7476 * to a string using _drupal_debug_message().
|
danielebarchiesi@0
|
7477 *
|
danielebarchiesi@0
|
7478 * @param $data
|
danielebarchiesi@0
|
7479 * Data to be output.
|
danielebarchiesi@0
|
7480 * @param $label
|
danielebarchiesi@0
|
7481 * Label to prefix the data.
|
danielebarchiesi@0
|
7482 * @param $print_r
|
danielebarchiesi@0
|
7483 * Flag to switch between print_r() and var_export() for data conversion to
|
danielebarchiesi@0
|
7484 * string. Set $print_r to TRUE when dealing with a recursive data structure
|
danielebarchiesi@0
|
7485 * as var_export() will generate an error.
|
danielebarchiesi@0
|
7486 */
|
danielebarchiesi@0
|
7487 function debug($data, $label = NULL, $print_r = FALSE) {
|
danielebarchiesi@0
|
7488 // Print $data contents to string.
|
danielebarchiesi@0
|
7489 $string = check_plain($print_r ? print_r($data, TRUE) : var_export($data, TRUE));
|
danielebarchiesi@0
|
7490
|
danielebarchiesi@0
|
7491 // Display values with pre-formatting to increase readability.
|
danielebarchiesi@0
|
7492 $string = '<pre>' . $string . '</pre>';
|
danielebarchiesi@0
|
7493
|
danielebarchiesi@0
|
7494 trigger_error(trim($label ? "$label: $string" : $string));
|
danielebarchiesi@0
|
7495 }
|
danielebarchiesi@0
|
7496
|
danielebarchiesi@0
|
7497 /**
|
danielebarchiesi@0
|
7498 * Parses a dependency for comparison by drupal_check_incompatibility().
|
danielebarchiesi@0
|
7499 *
|
danielebarchiesi@0
|
7500 * @param $dependency
|
danielebarchiesi@0
|
7501 * A dependency string, for example 'foo (>=7.x-4.5-beta5, 3.x)'.
|
danielebarchiesi@0
|
7502 *
|
danielebarchiesi@0
|
7503 * @return
|
danielebarchiesi@0
|
7504 * An associative array with three keys:
|
danielebarchiesi@0
|
7505 * - 'name' includes the name of the thing to depend on (e.g. 'foo').
|
danielebarchiesi@0
|
7506 * - 'original_version' contains the original version string (which can be
|
danielebarchiesi@0
|
7507 * used in the UI for reporting incompatibilities).
|
danielebarchiesi@0
|
7508 * - 'versions' is a list of associative arrays, each containing the keys
|
danielebarchiesi@0
|
7509 * 'op' and 'version'. 'op' can be one of: '=', '==', '!=', '<>', '<',
|
danielebarchiesi@0
|
7510 * '<=', '>', or '>='. 'version' is one piece like '4.5-beta3'.
|
danielebarchiesi@0
|
7511 * Callers should pass this structure to drupal_check_incompatibility().
|
danielebarchiesi@0
|
7512 *
|
danielebarchiesi@0
|
7513 * @see drupal_check_incompatibility()
|
danielebarchiesi@0
|
7514 */
|
danielebarchiesi@0
|
7515 function drupal_parse_dependency($dependency) {
|
danielebarchiesi@0
|
7516 // We use named subpatterns and support every op that version_compare
|
danielebarchiesi@0
|
7517 // supports. Also, op is optional and defaults to equals.
|
danielebarchiesi@0
|
7518 $p_op = '(?P<operation>!=|==|=|<|<=|>|>=|<>)?';
|
danielebarchiesi@0
|
7519 // Core version is always optional: 7.x-2.x and 2.x is treated the same.
|
danielebarchiesi@0
|
7520 $p_core = '(?:' . preg_quote(DRUPAL_CORE_COMPATIBILITY) . '-)?';
|
danielebarchiesi@0
|
7521 $p_major = '(?P<major>\d+)';
|
danielebarchiesi@0
|
7522 // By setting the minor version to x, branches can be matched.
|
danielebarchiesi@0
|
7523 $p_minor = '(?P<minor>(?:\d+|x)(?:-[A-Za-z]+\d+)?)';
|
danielebarchiesi@0
|
7524 $value = array();
|
danielebarchiesi@0
|
7525 $parts = explode('(', $dependency, 2);
|
danielebarchiesi@0
|
7526 $value['name'] = trim($parts[0]);
|
danielebarchiesi@0
|
7527 if (isset($parts[1])) {
|
danielebarchiesi@0
|
7528 $value['original_version'] = ' (' . $parts[1];
|
danielebarchiesi@0
|
7529 foreach (explode(',', $parts[1]) as $version) {
|
danielebarchiesi@0
|
7530 if (preg_match("/^\s*$p_op\s*$p_core$p_major\.$p_minor/", $version, $matches)) {
|
danielebarchiesi@0
|
7531 $op = !empty($matches['operation']) ? $matches['operation'] : '=';
|
danielebarchiesi@0
|
7532 if ($matches['minor'] == 'x') {
|
danielebarchiesi@0
|
7533 // Drupal considers "2.x" to mean any version that begins with
|
danielebarchiesi@0
|
7534 // "2" (e.g. 2.0, 2.9 are all "2.x"). PHP's version_compare(),
|
danielebarchiesi@0
|
7535 // on the other hand, treats "x" as a string; so to
|
danielebarchiesi@0
|
7536 // version_compare(), "2.x" is considered less than 2.0. This
|
danielebarchiesi@0
|
7537 // means that >=2.x and <2.x are handled by version_compare()
|
danielebarchiesi@0
|
7538 // as we need, but > and <= are not.
|
danielebarchiesi@0
|
7539 if ($op == '>' || $op == '<=') {
|
danielebarchiesi@0
|
7540 $matches['major']++;
|
danielebarchiesi@0
|
7541 }
|
danielebarchiesi@0
|
7542 // Equivalence can be checked by adding two restrictions.
|
danielebarchiesi@0
|
7543 if ($op == '=' || $op == '==') {
|
danielebarchiesi@0
|
7544 $value['versions'][] = array('op' => '<', 'version' => ($matches['major'] + 1) . '.x');
|
danielebarchiesi@0
|
7545 $op = '>=';
|
danielebarchiesi@0
|
7546 }
|
danielebarchiesi@0
|
7547 }
|
danielebarchiesi@0
|
7548 $value['versions'][] = array('op' => $op, 'version' => $matches['major'] . '.' . $matches['minor']);
|
danielebarchiesi@0
|
7549 }
|
danielebarchiesi@0
|
7550 }
|
danielebarchiesi@0
|
7551 }
|
danielebarchiesi@0
|
7552 return $value;
|
danielebarchiesi@0
|
7553 }
|
danielebarchiesi@0
|
7554
|
danielebarchiesi@0
|
7555 /**
|
danielebarchiesi@0
|
7556 * Checks whether a version is compatible with a given dependency.
|
danielebarchiesi@0
|
7557 *
|
danielebarchiesi@0
|
7558 * @param $v
|
danielebarchiesi@0
|
7559 * The parsed dependency structure from drupal_parse_dependency().
|
danielebarchiesi@0
|
7560 * @param $current_version
|
danielebarchiesi@0
|
7561 * The version to check against (like 4.2).
|
danielebarchiesi@0
|
7562 *
|
danielebarchiesi@0
|
7563 * @return
|
danielebarchiesi@0
|
7564 * NULL if compatible, otherwise the original dependency version string that
|
danielebarchiesi@0
|
7565 * caused the incompatibility.
|
danielebarchiesi@0
|
7566 *
|
danielebarchiesi@0
|
7567 * @see drupal_parse_dependency()
|
danielebarchiesi@0
|
7568 */
|
danielebarchiesi@0
|
7569 function drupal_check_incompatibility($v, $current_version) {
|
danielebarchiesi@0
|
7570 if (!empty($v['versions'])) {
|
danielebarchiesi@0
|
7571 foreach ($v['versions'] as $required_version) {
|
danielebarchiesi@0
|
7572 if ((isset($required_version['op']) && !version_compare($current_version, $required_version['version'], $required_version['op']))) {
|
danielebarchiesi@0
|
7573 return $v['original_version'];
|
danielebarchiesi@0
|
7574 }
|
danielebarchiesi@0
|
7575 }
|
danielebarchiesi@0
|
7576 }
|
danielebarchiesi@0
|
7577 }
|
danielebarchiesi@0
|
7578
|
danielebarchiesi@0
|
7579 /**
|
danielebarchiesi@0
|
7580 * Get the entity info array of an entity type.
|
danielebarchiesi@0
|
7581 *
|
danielebarchiesi@0
|
7582 * @param $entity_type
|
danielebarchiesi@0
|
7583 * The entity type, e.g. node, for which the info shall be returned, or NULL
|
danielebarchiesi@0
|
7584 * to return an array with info about all types.
|
danielebarchiesi@0
|
7585 *
|
danielebarchiesi@0
|
7586 * @see hook_entity_info()
|
danielebarchiesi@0
|
7587 * @see hook_entity_info_alter()
|
danielebarchiesi@0
|
7588 */
|
danielebarchiesi@0
|
7589 function entity_get_info($entity_type = NULL) {
|
danielebarchiesi@0
|
7590 global $language;
|
danielebarchiesi@0
|
7591
|
danielebarchiesi@0
|
7592 // Use the advanced drupal_static() pattern, since this is called very often.
|
danielebarchiesi@0
|
7593 static $drupal_static_fast;
|
danielebarchiesi@0
|
7594 if (!isset($drupal_static_fast)) {
|
danielebarchiesi@0
|
7595 $drupal_static_fast['entity_info'] = &drupal_static(__FUNCTION__);
|
danielebarchiesi@0
|
7596 }
|
danielebarchiesi@0
|
7597 $entity_info = &$drupal_static_fast['entity_info'];
|
danielebarchiesi@0
|
7598
|
danielebarchiesi@0
|
7599 // hook_entity_info() includes translated strings, so each language is cached
|
danielebarchiesi@0
|
7600 // separately.
|
danielebarchiesi@0
|
7601 $langcode = $language->language;
|
danielebarchiesi@0
|
7602
|
danielebarchiesi@0
|
7603 if (empty($entity_info)) {
|
danielebarchiesi@0
|
7604 if ($cache = cache_get("entity_info:$langcode")) {
|
danielebarchiesi@0
|
7605 $entity_info = $cache->data;
|
danielebarchiesi@0
|
7606 }
|
danielebarchiesi@0
|
7607 else {
|
danielebarchiesi@0
|
7608 $entity_info = module_invoke_all('entity_info');
|
danielebarchiesi@0
|
7609 // Merge in default values.
|
danielebarchiesi@0
|
7610 foreach ($entity_info as $name => $data) {
|
danielebarchiesi@0
|
7611 $entity_info[$name] += array(
|
danielebarchiesi@0
|
7612 'fieldable' => FALSE,
|
danielebarchiesi@0
|
7613 'controller class' => 'DrupalDefaultEntityController',
|
danielebarchiesi@0
|
7614 'static cache' => TRUE,
|
danielebarchiesi@0
|
7615 'field cache' => TRUE,
|
danielebarchiesi@0
|
7616 'load hook' => $name . '_load',
|
danielebarchiesi@0
|
7617 'bundles' => array(),
|
danielebarchiesi@0
|
7618 'view modes' => array(),
|
danielebarchiesi@0
|
7619 'entity keys' => array(),
|
danielebarchiesi@0
|
7620 'translation' => array(),
|
danielebarchiesi@0
|
7621 );
|
danielebarchiesi@0
|
7622 $entity_info[$name]['entity keys'] += array(
|
danielebarchiesi@0
|
7623 'revision' => '',
|
danielebarchiesi@0
|
7624 'bundle' => '',
|
danielebarchiesi@0
|
7625 );
|
danielebarchiesi@0
|
7626 foreach ($entity_info[$name]['view modes'] as $view_mode => $view_mode_info) {
|
danielebarchiesi@0
|
7627 $entity_info[$name]['view modes'][$view_mode] += array(
|
danielebarchiesi@0
|
7628 'custom settings' => FALSE,
|
danielebarchiesi@0
|
7629 );
|
danielebarchiesi@0
|
7630 }
|
danielebarchiesi@0
|
7631 // If no bundle key is provided, assume a single bundle, named after
|
danielebarchiesi@0
|
7632 // the entity type.
|
danielebarchiesi@0
|
7633 if (empty($entity_info[$name]['entity keys']['bundle']) && empty($entity_info[$name]['bundles'])) {
|
danielebarchiesi@0
|
7634 $entity_info[$name]['bundles'] = array($name => array('label' => $entity_info[$name]['label']));
|
danielebarchiesi@0
|
7635 }
|
danielebarchiesi@0
|
7636 // Prepare entity schema fields SQL info for
|
danielebarchiesi@0
|
7637 // DrupalEntityControllerInterface::buildQuery().
|
danielebarchiesi@0
|
7638 if (isset($entity_info[$name]['base table'])) {
|
danielebarchiesi@0
|
7639 $entity_info[$name]['schema_fields_sql']['base table'] = drupal_schema_fields_sql($entity_info[$name]['base table']);
|
danielebarchiesi@0
|
7640 if (isset($entity_info[$name]['revision table'])) {
|
danielebarchiesi@0
|
7641 $entity_info[$name]['schema_fields_sql']['revision table'] = drupal_schema_fields_sql($entity_info[$name]['revision table']);
|
danielebarchiesi@0
|
7642 }
|
danielebarchiesi@0
|
7643 }
|
danielebarchiesi@0
|
7644 }
|
danielebarchiesi@0
|
7645 // Let other modules alter the entity info.
|
danielebarchiesi@0
|
7646 drupal_alter('entity_info', $entity_info);
|
danielebarchiesi@0
|
7647 cache_set("entity_info:$langcode", $entity_info);
|
danielebarchiesi@0
|
7648 }
|
danielebarchiesi@0
|
7649 }
|
danielebarchiesi@0
|
7650
|
danielebarchiesi@0
|
7651 if (empty($entity_type)) {
|
danielebarchiesi@0
|
7652 return $entity_info;
|
danielebarchiesi@0
|
7653 }
|
danielebarchiesi@0
|
7654 elseif (isset($entity_info[$entity_type])) {
|
danielebarchiesi@0
|
7655 return $entity_info[$entity_type];
|
danielebarchiesi@0
|
7656 }
|
danielebarchiesi@0
|
7657 }
|
danielebarchiesi@0
|
7658
|
danielebarchiesi@0
|
7659 /**
|
danielebarchiesi@0
|
7660 * Resets the cached information about entity types.
|
danielebarchiesi@0
|
7661 */
|
danielebarchiesi@0
|
7662 function entity_info_cache_clear() {
|
danielebarchiesi@0
|
7663 drupal_static_reset('entity_get_info');
|
danielebarchiesi@0
|
7664 // Clear all languages.
|
danielebarchiesi@0
|
7665 cache_clear_all('entity_info:', 'cache', TRUE);
|
danielebarchiesi@0
|
7666 }
|
danielebarchiesi@0
|
7667
|
danielebarchiesi@0
|
7668 /**
|
danielebarchiesi@0
|
7669 * Helper function to extract id, vid, and bundle name from an entity.
|
danielebarchiesi@0
|
7670 *
|
danielebarchiesi@0
|
7671 * @param $entity_type
|
danielebarchiesi@0
|
7672 * The entity type; e.g. 'node' or 'user'.
|
danielebarchiesi@0
|
7673 * @param $entity
|
danielebarchiesi@0
|
7674 * The entity from which to extract values.
|
danielebarchiesi@0
|
7675 *
|
danielebarchiesi@0
|
7676 * @return
|
danielebarchiesi@0
|
7677 * A numerically indexed array (not a hash table) containing these
|
danielebarchiesi@0
|
7678 * elements:
|
danielebarchiesi@0
|
7679 * - 0: Primary ID of the entity.
|
danielebarchiesi@0
|
7680 * - 1: Revision ID of the entity, or NULL if $entity_type is not versioned.
|
danielebarchiesi@0
|
7681 * - 2: Bundle name of the entity, or NULL if $entity_type has no bundles.
|
danielebarchiesi@0
|
7682 */
|
danielebarchiesi@0
|
7683 function entity_extract_ids($entity_type, $entity) {
|
danielebarchiesi@0
|
7684 $info = entity_get_info($entity_type);
|
danielebarchiesi@0
|
7685
|
danielebarchiesi@0
|
7686 // Objects being created might not have id/vid yet.
|
danielebarchiesi@0
|
7687 $id = isset($entity->{$info['entity keys']['id']}) ? $entity->{$info['entity keys']['id']} : NULL;
|
danielebarchiesi@0
|
7688 $vid = ($info['entity keys']['revision'] && isset($entity->{$info['entity keys']['revision']})) ? $entity->{$info['entity keys']['revision']} : NULL;
|
danielebarchiesi@0
|
7689
|
danielebarchiesi@0
|
7690 if (!empty($info['entity keys']['bundle'])) {
|
danielebarchiesi@0
|
7691 // Explicitly fail for malformed entities missing the bundle property.
|
danielebarchiesi@0
|
7692 if (!isset($entity->{$info['entity keys']['bundle']}) || $entity->{$info['entity keys']['bundle']} === '') {
|
danielebarchiesi@0
|
7693 throw new EntityMalformedException(t('Missing bundle property on entity of type @entity_type.', array('@entity_type' => $entity_type)));
|
danielebarchiesi@0
|
7694 }
|
danielebarchiesi@0
|
7695 $bundle = $entity->{$info['entity keys']['bundle']};
|
danielebarchiesi@0
|
7696 }
|
danielebarchiesi@0
|
7697 else {
|
danielebarchiesi@0
|
7698 // The entity type provides no bundle key: assume a single bundle, named
|
danielebarchiesi@0
|
7699 // after the entity type.
|
danielebarchiesi@0
|
7700 $bundle = $entity_type;
|
danielebarchiesi@0
|
7701 }
|
danielebarchiesi@0
|
7702
|
danielebarchiesi@0
|
7703 return array($id, $vid, $bundle);
|
danielebarchiesi@0
|
7704 }
|
danielebarchiesi@0
|
7705
|
danielebarchiesi@0
|
7706 /**
|
danielebarchiesi@0
|
7707 * Helper function to assemble an object structure with initial ids.
|
danielebarchiesi@0
|
7708 *
|
danielebarchiesi@0
|
7709 * This function can be seen as reciprocal to entity_extract_ids().
|
danielebarchiesi@0
|
7710 *
|
danielebarchiesi@0
|
7711 * @param $entity_type
|
danielebarchiesi@0
|
7712 * The entity type; e.g. 'node' or 'user'.
|
danielebarchiesi@0
|
7713 * @param $ids
|
danielebarchiesi@0
|
7714 * A numerically indexed array, as returned by entity_extract_ids().
|
danielebarchiesi@0
|
7715 *
|
danielebarchiesi@0
|
7716 * @return
|
danielebarchiesi@0
|
7717 * An entity structure, initialized with the ids provided.
|
danielebarchiesi@0
|
7718 *
|
danielebarchiesi@0
|
7719 * @see entity_extract_ids()
|
danielebarchiesi@0
|
7720 */
|
danielebarchiesi@0
|
7721 function entity_create_stub_entity($entity_type, $ids) {
|
danielebarchiesi@0
|
7722 $entity = new stdClass();
|
danielebarchiesi@0
|
7723 $info = entity_get_info($entity_type);
|
danielebarchiesi@0
|
7724 $entity->{$info['entity keys']['id']} = $ids[0];
|
danielebarchiesi@0
|
7725 if (!empty($info['entity keys']['revision']) && isset($ids[1])) {
|
danielebarchiesi@0
|
7726 $entity->{$info['entity keys']['revision']} = $ids[1];
|
danielebarchiesi@0
|
7727 }
|
danielebarchiesi@0
|
7728 if (!empty($info['entity keys']['bundle']) && isset($ids[2])) {
|
danielebarchiesi@0
|
7729 $entity->{$info['entity keys']['bundle']} = $ids[2];
|
danielebarchiesi@0
|
7730 }
|
danielebarchiesi@0
|
7731 return $entity;
|
danielebarchiesi@0
|
7732 }
|
danielebarchiesi@0
|
7733
|
danielebarchiesi@0
|
7734 /**
|
danielebarchiesi@0
|
7735 * Load entities from the database.
|
danielebarchiesi@0
|
7736 *
|
danielebarchiesi@0
|
7737 * The entities are stored in a static memory cache, and will not require
|
danielebarchiesi@0
|
7738 * database access if loaded again during the same page request.
|
danielebarchiesi@0
|
7739 *
|
danielebarchiesi@0
|
7740 * The actual loading is done through a class that has to implement the
|
danielebarchiesi@0
|
7741 * DrupalEntityControllerInterface interface. By default,
|
danielebarchiesi@0
|
7742 * DrupalDefaultEntityController is used. Entity types can specify that a
|
danielebarchiesi@0
|
7743 * different class should be used by setting the 'controller class' key in
|
danielebarchiesi@0
|
7744 * hook_entity_info(). These classes can either implement the
|
danielebarchiesi@0
|
7745 * DrupalEntityControllerInterface interface, or, most commonly, extend the
|
danielebarchiesi@0
|
7746 * DrupalDefaultEntityController class. See node_entity_info() and the
|
danielebarchiesi@0
|
7747 * NodeController in node.module as an example.
|
danielebarchiesi@0
|
7748 *
|
danielebarchiesi@0
|
7749 * @param $entity_type
|
danielebarchiesi@0
|
7750 * The entity type to load, e.g. node or user.
|
danielebarchiesi@0
|
7751 * @param $ids
|
danielebarchiesi@0
|
7752 * An array of entity IDs, or FALSE to load all entities.
|
danielebarchiesi@0
|
7753 * @param $conditions
|
danielebarchiesi@0
|
7754 * (deprecated) An associative array of conditions on the base table, where
|
danielebarchiesi@0
|
7755 * the keys are the database fields and the values are the values those
|
danielebarchiesi@0
|
7756 * fields must have. Instead, it is preferable to use EntityFieldQuery to
|
danielebarchiesi@0
|
7757 * retrieve a list of entity IDs loadable by this function.
|
danielebarchiesi@0
|
7758 * @param $reset
|
danielebarchiesi@0
|
7759 * Whether to reset the internal cache for the requested entity type.
|
danielebarchiesi@0
|
7760 *
|
danielebarchiesi@0
|
7761 * @return
|
danielebarchiesi@0
|
7762 * An array of entity objects indexed by their ids. When no results are
|
danielebarchiesi@0
|
7763 * found, an empty array is returned.
|
danielebarchiesi@0
|
7764 *
|
danielebarchiesi@0
|
7765 * @todo Remove $conditions in Drupal 8.
|
danielebarchiesi@0
|
7766 *
|
danielebarchiesi@0
|
7767 * @see hook_entity_info()
|
danielebarchiesi@0
|
7768 * @see DrupalEntityControllerInterface
|
danielebarchiesi@0
|
7769 * @see DrupalDefaultEntityController
|
danielebarchiesi@0
|
7770 * @see EntityFieldQuery
|
danielebarchiesi@0
|
7771 */
|
danielebarchiesi@0
|
7772 function entity_load($entity_type, $ids = FALSE, $conditions = array(), $reset = FALSE) {
|
danielebarchiesi@0
|
7773 if ($reset) {
|
danielebarchiesi@0
|
7774 entity_get_controller($entity_type)->resetCache();
|
danielebarchiesi@0
|
7775 }
|
danielebarchiesi@0
|
7776 return entity_get_controller($entity_type)->load($ids, $conditions);
|
danielebarchiesi@0
|
7777 }
|
danielebarchiesi@0
|
7778
|
danielebarchiesi@0
|
7779 /**
|
danielebarchiesi@0
|
7780 * Loads the unchanged, i.e. not modified, entity from the database.
|
danielebarchiesi@0
|
7781 *
|
danielebarchiesi@0
|
7782 * Unlike entity_load() this function ensures the entity is directly loaded from
|
danielebarchiesi@0
|
7783 * the database, thus bypassing any static cache. In particular, this function
|
danielebarchiesi@0
|
7784 * is useful to determine changes by comparing the entity being saved to the
|
danielebarchiesi@0
|
7785 * stored entity.
|
danielebarchiesi@0
|
7786 *
|
danielebarchiesi@0
|
7787 * @param $entity_type
|
danielebarchiesi@0
|
7788 * The entity type to load, e.g. node or user.
|
danielebarchiesi@0
|
7789 * @param $id
|
danielebarchiesi@0
|
7790 * The ID of the entity to load.
|
danielebarchiesi@0
|
7791 *
|
danielebarchiesi@0
|
7792 * @return
|
danielebarchiesi@0
|
7793 * The unchanged entity, or FALSE if the entity cannot be loaded.
|
danielebarchiesi@0
|
7794 */
|
danielebarchiesi@0
|
7795 function entity_load_unchanged($entity_type, $id) {
|
danielebarchiesi@0
|
7796 entity_get_controller($entity_type)->resetCache(array($id));
|
danielebarchiesi@0
|
7797 $result = entity_get_controller($entity_type)->load(array($id));
|
danielebarchiesi@0
|
7798 return reset($result);
|
danielebarchiesi@0
|
7799 }
|
danielebarchiesi@0
|
7800
|
danielebarchiesi@0
|
7801 /**
|
danielebarchiesi@0
|
7802 * Get the entity controller class for an entity type.
|
danielebarchiesi@0
|
7803 */
|
danielebarchiesi@0
|
7804 function entity_get_controller($entity_type) {
|
danielebarchiesi@0
|
7805 $controllers = &drupal_static(__FUNCTION__, array());
|
danielebarchiesi@0
|
7806 if (!isset($controllers[$entity_type])) {
|
danielebarchiesi@0
|
7807 $type_info = entity_get_info($entity_type);
|
danielebarchiesi@0
|
7808 $class = $type_info['controller class'];
|
danielebarchiesi@0
|
7809 $controllers[$entity_type] = new $class($entity_type);
|
danielebarchiesi@0
|
7810 }
|
danielebarchiesi@0
|
7811 return $controllers[$entity_type];
|
danielebarchiesi@0
|
7812 }
|
danielebarchiesi@0
|
7813
|
danielebarchiesi@0
|
7814 /**
|
danielebarchiesi@0
|
7815 * Invoke hook_entity_prepare_view().
|
danielebarchiesi@0
|
7816 *
|
danielebarchiesi@0
|
7817 * If adding a new entity similar to nodes, comments or users, you should
|
danielebarchiesi@0
|
7818 * invoke this function during the ENTITY_build_content() or
|
danielebarchiesi@0
|
7819 * ENTITY_view_multiple() phases of rendering to allow other modules to alter
|
danielebarchiesi@0
|
7820 * the objects during this phase. This is needed for situations where
|
danielebarchiesi@0
|
7821 * information needs to be loaded outside of ENTITY_load() - particularly
|
danielebarchiesi@0
|
7822 * when loading entities into one another - i.e. a user object into a node, due
|
danielebarchiesi@0
|
7823 * to the potential for unwanted side-effects such as caching and infinite
|
danielebarchiesi@0
|
7824 * recursion. By convention, entity_prepare_view() is called after
|
danielebarchiesi@0
|
7825 * field_attach_prepare_view() to allow entity level hooks to act on content
|
danielebarchiesi@0
|
7826 * loaded by field API.
|
danielebarchiesi@0
|
7827 *
|
danielebarchiesi@0
|
7828 * @param $entity_type
|
danielebarchiesi@0
|
7829 * The type of entity, i.e. 'node', 'user'.
|
danielebarchiesi@0
|
7830 * @param $entities
|
danielebarchiesi@0
|
7831 * The entity objects which are being prepared for view, keyed by object ID.
|
danielebarchiesi@0
|
7832 * @param $langcode
|
danielebarchiesi@0
|
7833 * (optional) A language code to be used for rendering. Defaults to the global
|
danielebarchiesi@0
|
7834 * content language of the current request.
|
danielebarchiesi@0
|
7835 *
|
danielebarchiesi@0
|
7836 * @see hook_entity_prepare_view()
|
danielebarchiesi@0
|
7837 */
|
danielebarchiesi@0
|
7838 function entity_prepare_view($entity_type, $entities, $langcode = NULL) {
|
danielebarchiesi@0
|
7839 if (!isset($langcode)) {
|
danielebarchiesi@0
|
7840 $langcode = $GLOBALS['language_content']->language;
|
danielebarchiesi@0
|
7841 }
|
danielebarchiesi@0
|
7842
|
danielebarchiesi@0
|
7843 // To ensure hooks are only run once per entity, check for an
|
danielebarchiesi@0
|
7844 // entity_view_prepared flag and only process items without it.
|
danielebarchiesi@0
|
7845 // @todo: resolve this more generally for both entity and field level hooks.
|
danielebarchiesi@0
|
7846 $prepare = array();
|
danielebarchiesi@0
|
7847 foreach ($entities as $id => $entity) {
|
danielebarchiesi@0
|
7848 if (empty($entity->entity_view_prepared)) {
|
danielebarchiesi@0
|
7849 // Add this entity to the items to be prepared.
|
danielebarchiesi@0
|
7850 $prepare[$id] = $entity;
|
danielebarchiesi@0
|
7851
|
danielebarchiesi@0
|
7852 // Mark this item as prepared.
|
danielebarchiesi@0
|
7853 $entity->entity_view_prepared = TRUE;
|
danielebarchiesi@0
|
7854 }
|
danielebarchiesi@0
|
7855 }
|
danielebarchiesi@0
|
7856
|
danielebarchiesi@0
|
7857 if (!empty($prepare)) {
|
danielebarchiesi@0
|
7858 module_invoke_all('entity_prepare_view', $prepare, $entity_type, $langcode);
|
danielebarchiesi@0
|
7859 }
|
danielebarchiesi@0
|
7860 }
|
danielebarchiesi@0
|
7861
|
danielebarchiesi@0
|
7862 /**
|
danielebarchiesi@0
|
7863 * Returns the URI elements of an entity.
|
danielebarchiesi@0
|
7864 *
|
danielebarchiesi@0
|
7865 * @param $entity_type
|
danielebarchiesi@0
|
7866 * The entity type; e.g. 'node' or 'user'.
|
danielebarchiesi@0
|
7867 * @param $entity
|
danielebarchiesi@0
|
7868 * The entity for which to generate a path.
|
danielebarchiesi@0
|
7869 * @return
|
danielebarchiesi@0
|
7870 * An array containing the 'path' and 'options' keys used to build the URI of
|
danielebarchiesi@0
|
7871 * the entity, and matching the signature of url(). NULL if the entity has no
|
danielebarchiesi@0
|
7872 * URI of its own.
|
danielebarchiesi@0
|
7873 */
|
danielebarchiesi@0
|
7874 function entity_uri($entity_type, $entity) {
|
danielebarchiesi@0
|
7875 $info = entity_get_info($entity_type);
|
danielebarchiesi@0
|
7876 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
danielebarchiesi@0
|
7877
|
danielebarchiesi@0
|
7878 // A bundle-specific callback takes precedence over the generic one for the
|
danielebarchiesi@0
|
7879 // entity type.
|
danielebarchiesi@0
|
7880 if (isset($info['bundles'][$bundle]['uri callback'])) {
|
danielebarchiesi@0
|
7881 $uri_callback = $info['bundles'][$bundle]['uri callback'];
|
danielebarchiesi@0
|
7882 }
|
danielebarchiesi@0
|
7883 elseif (isset($info['uri callback'])) {
|
danielebarchiesi@0
|
7884 $uri_callback = $info['uri callback'];
|
danielebarchiesi@0
|
7885 }
|
danielebarchiesi@0
|
7886 else {
|
danielebarchiesi@0
|
7887 return NULL;
|
danielebarchiesi@0
|
7888 }
|
danielebarchiesi@0
|
7889
|
danielebarchiesi@0
|
7890 // Invoke the callback to get the URI. If there is no callback, return NULL.
|
danielebarchiesi@0
|
7891 if (isset($uri_callback) && function_exists($uri_callback)) {
|
danielebarchiesi@0
|
7892 $uri = $uri_callback($entity);
|
danielebarchiesi@0
|
7893 // Pass the entity data to url() so that alter functions do not need to
|
danielebarchiesi@0
|
7894 // lookup this entity again.
|
danielebarchiesi@0
|
7895 $uri['options']['entity_type'] = $entity_type;
|
danielebarchiesi@0
|
7896 $uri['options']['entity'] = $entity;
|
danielebarchiesi@0
|
7897 return $uri;
|
danielebarchiesi@0
|
7898 }
|
danielebarchiesi@0
|
7899 }
|
danielebarchiesi@0
|
7900
|
danielebarchiesi@0
|
7901 /**
|
danielebarchiesi@0
|
7902 * Returns the label of an entity.
|
danielebarchiesi@0
|
7903 *
|
danielebarchiesi@0
|
7904 * See the 'label callback' component of the hook_entity_info() return value
|
danielebarchiesi@0
|
7905 * for more information.
|
danielebarchiesi@0
|
7906 *
|
danielebarchiesi@0
|
7907 * @param $entity_type
|
danielebarchiesi@0
|
7908 * The entity type; e.g., 'node' or 'user'.
|
danielebarchiesi@0
|
7909 * @param $entity
|
danielebarchiesi@0
|
7910 * The entity for which to generate the label.
|
danielebarchiesi@0
|
7911 *
|
danielebarchiesi@0
|
7912 * @return
|
danielebarchiesi@0
|
7913 * The entity label, or FALSE if not found.
|
danielebarchiesi@0
|
7914 */
|
danielebarchiesi@0
|
7915 function entity_label($entity_type, $entity) {
|
danielebarchiesi@0
|
7916 $label = FALSE;
|
danielebarchiesi@0
|
7917 $info = entity_get_info($entity_type);
|
danielebarchiesi@0
|
7918 if (isset($info['label callback']) && function_exists($info['label callback'])) {
|
danielebarchiesi@0
|
7919 $label = $info['label callback']($entity, $entity_type);
|
danielebarchiesi@0
|
7920 }
|
danielebarchiesi@0
|
7921 elseif (!empty($info['entity keys']['label']) && isset($entity->{$info['entity keys']['label']})) {
|
danielebarchiesi@0
|
7922 $label = $entity->{$info['entity keys']['label']};
|
danielebarchiesi@0
|
7923 }
|
danielebarchiesi@0
|
7924
|
danielebarchiesi@0
|
7925 return $label;
|
danielebarchiesi@0
|
7926 }
|
danielebarchiesi@0
|
7927
|
danielebarchiesi@0
|
7928 /**
|
danielebarchiesi@0
|
7929 * Returns the language of an entity.
|
danielebarchiesi@0
|
7930 *
|
danielebarchiesi@0
|
7931 * @param $entity_type
|
danielebarchiesi@0
|
7932 * The entity type; e.g., 'node' or 'user'.
|
danielebarchiesi@0
|
7933 * @param $entity
|
danielebarchiesi@0
|
7934 * The entity for which to get the language.
|
danielebarchiesi@0
|
7935 *
|
danielebarchiesi@0
|
7936 * @return
|
danielebarchiesi@0
|
7937 * A valid language code or NULL if the entity has no language support.
|
danielebarchiesi@0
|
7938 */
|
danielebarchiesi@0
|
7939 function entity_language($entity_type, $entity) {
|
danielebarchiesi@0
|
7940 $info = entity_get_info($entity_type);
|
danielebarchiesi@0
|
7941
|
danielebarchiesi@0
|
7942 // Invoke the callback to get the language. If there is no callback, try to
|
danielebarchiesi@0
|
7943 // get it from a property of the entity, otherwise NULL.
|
danielebarchiesi@0
|
7944 if (isset($info['language callback']) && function_exists($info['language callback'])) {
|
danielebarchiesi@0
|
7945 $langcode = $info['language callback']($entity_type, $entity);
|
danielebarchiesi@0
|
7946 }
|
danielebarchiesi@0
|
7947 elseif (!empty($info['entity keys']['language']) && isset($entity->{$info['entity keys']['language']})) {
|
danielebarchiesi@0
|
7948 $langcode = $entity->{$info['entity keys']['language']};
|
danielebarchiesi@0
|
7949 }
|
danielebarchiesi@0
|
7950 else {
|
danielebarchiesi@0
|
7951 // The value returned in D8 would be LANGUAGE_NONE, we cannot use it here to
|
danielebarchiesi@0
|
7952 // preserve backward compatibility. In fact this function has been
|
danielebarchiesi@0
|
7953 // introduced very late in the D7 life cycle, mainly as the proper default
|
danielebarchiesi@0
|
7954 // for field_attach_form(). By returning LANGUAGE_NONE when no language
|
danielebarchiesi@0
|
7955 // information is available, we would introduce a potentially BC-breaking
|
danielebarchiesi@0
|
7956 // API change, since field_attach_form() defaults to the default language
|
danielebarchiesi@0
|
7957 // instead of LANGUAGE_NONE. Moreover this allows us to distinguish between
|
danielebarchiesi@0
|
7958 // entities that have no language specified from ones that do not have
|
danielebarchiesi@0
|
7959 // language support at all.
|
danielebarchiesi@0
|
7960 $langcode = NULL;
|
danielebarchiesi@0
|
7961 }
|
danielebarchiesi@0
|
7962
|
danielebarchiesi@0
|
7963 return $langcode;
|
danielebarchiesi@0
|
7964 }
|
danielebarchiesi@0
|
7965
|
danielebarchiesi@0
|
7966 /**
|
danielebarchiesi@0
|
7967 * Attaches field API validation to entity forms.
|
danielebarchiesi@0
|
7968 */
|
danielebarchiesi@0
|
7969 function entity_form_field_validate($entity_type, $form, &$form_state) {
|
danielebarchiesi@0
|
7970 // All field attach API functions act on an entity object, but during form
|
danielebarchiesi@0
|
7971 // validation, we don't have one. $form_state contains the entity as it was
|
danielebarchiesi@0
|
7972 // prior to processing the current form submission, and we must not update it
|
danielebarchiesi@0
|
7973 // until we have fully validated the submitted input. Therefore, for
|
danielebarchiesi@0
|
7974 // validation, act on a pseudo entity created out of the form values.
|
danielebarchiesi@0
|
7975 $pseudo_entity = (object) $form_state['values'];
|
danielebarchiesi@0
|
7976 field_attach_form_validate($entity_type, $pseudo_entity, $form, $form_state);
|
danielebarchiesi@0
|
7977 }
|
danielebarchiesi@0
|
7978
|
danielebarchiesi@0
|
7979 /**
|
danielebarchiesi@0
|
7980 * Copies submitted values to entity properties for simple entity forms.
|
danielebarchiesi@0
|
7981 *
|
danielebarchiesi@0
|
7982 * During the submission handling of an entity form's "Save", "Preview", and
|
danielebarchiesi@0
|
7983 * possibly other buttons, the form state's entity needs to be updated with the
|
danielebarchiesi@0
|
7984 * submitted form values. Each entity form implements its own builder function
|
danielebarchiesi@0
|
7985 * for doing this, appropriate for the particular entity and form, whereas
|
danielebarchiesi@0
|
7986 * modules may specify additional builder functions in $form['#entity_builders']
|
danielebarchiesi@0
|
7987 * for copying the form values of added form elements to entity properties.
|
danielebarchiesi@0
|
7988 * Many of the main entity builder functions can call this helper function to
|
danielebarchiesi@0
|
7989 * re-use its logic of copying $form_state['values'][PROPERTY] values to
|
danielebarchiesi@0
|
7990 * $entity->PROPERTY for all entries in $form_state['values'] that are not field
|
danielebarchiesi@0
|
7991 * data, and calling field_attach_submit() to copy field data. Apart from that
|
danielebarchiesi@0
|
7992 * this helper invokes any additional builder functions that have been specified
|
danielebarchiesi@0
|
7993 * in $form['#entity_builders'].
|
danielebarchiesi@0
|
7994 *
|
danielebarchiesi@0
|
7995 * For some entity forms (e.g., forms with complex non-field data and forms that
|
danielebarchiesi@0
|
7996 * simultaneously edit multiple entities), this behavior may be inappropriate,
|
danielebarchiesi@0
|
7997 * so the builder function for such forms needs to implement the required
|
danielebarchiesi@0
|
7998 * functionality instead of calling this function.
|
danielebarchiesi@0
|
7999 */
|
danielebarchiesi@0
|
8000 function entity_form_submit_build_entity($entity_type, $entity, $form, &$form_state) {
|
danielebarchiesi@0
|
8001 $info = entity_get_info($entity_type);
|
danielebarchiesi@0
|
8002 list(, , $bundle) = entity_extract_ids($entity_type, $entity);
|
danielebarchiesi@0
|
8003
|
danielebarchiesi@0
|
8004 // Copy top-level form values that are not for fields to entity properties,
|
danielebarchiesi@0
|
8005 // without changing existing entity properties that are not being edited by
|
danielebarchiesi@0
|
8006 // this form. Copying field values must be done using field_attach_submit().
|
danielebarchiesi@0
|
8007 $values_excluding_fields = $info['fieldable'] ? array_diff_key($form_state['values'], field_info_instances($entity_type, $bundle)) : $form_state['values'];
|
danielebarchiesi@0
|
8008 foreach ($values_excluding_fields as $key => $value) {
|
danielebarchiesi@0
|
8009 $entity->$key = $value;
|
danielebarchiesi@0
|
8010 }
|
danielebarchiesi@0
|
8011
|
danielebarchiesi@0
|
8012 // Invoke all specified builders for copying form values to entity properties.
|
danielebarchiesi@0
|
8013 if (isset($form['#entity_builders'])) {
|
danielebarchiesi@0
|
8014 foreach ($form['#entity_builders'] as $function) {
|
danielebarchiesi@0
|
8015 $function($entity_type, $entity, $form, $form_state);
|
danielebarchiesi@0
|
8016 }
|
danielebarchiesi@0
|
8017 }
|
danielebarchiesi@0
|
8018
|
danielebarchiesi@0
|
8019 // Copy field values to the entity.
|
danielebarchiesi@0
|
8020 if ($info['fieldable']) {
|
danielebarchiesi@0
|
8021 field_attach_submit($entity_type, $entity, $form, $form_state);
|
danielebarchiesi@0
|
8022 }
|
danielebarchiesi@0
|
8023 }
|
danielebarchiesi@0
|
8024
|
danielebarchiesi@0
|
8025 /**
|
danielebarchiesi@0
|
8026 * Performs one or more XML-RPC request(s).
|
danielebarchiesi@0
|
8027 *
|
danielebarchiesi@0
|
8028 * Usage example:
|
danielebarchiesi@0
|
8029 * @code
|
danielebarchiesi@0
|
8030 * $result = xmlrpc('http://example.com/xmlrpc.php', array(
|
danielebarchiesi@0
|
8031 * 'service.methodName' => array($parameter, $second, $third),
|
danielebarchiesi@0
|
8032 * ));
|
danielebarchiesi@0
|
8033 * @endcode
|
danielebarchiesi@0
|
8034 *
|
danielebarchiesi@0
|
8035 * @param $url
|
danielebarchiesi@0
|
8036 * An absolute URL of the XML-RPC endpoint.
|
danielebarchiesi@0
|
8037 * @param $args
|
danielebarchiesi@0
|
8038 * An associative array whose keys are the methods to call and whose values
|
danielebarchiesi@0
|
8039 * are the arguments to pass to the respective method. If multiple methods
|
danielebarchiesi@0
|
8040 * are specified, a system.multicall is performed.
|
danielebarchiesi@0
|
8041 * @param $options
|
danielebarchiesi@0
|
8042 * (optional) An array of options to pass along to drupal_http_request().
|
danielebarchiesi@0
|
8043 *
|
danielebarchiesi@0
|
8044 * @return
|
danielebarchiesi@0
|
8045 * For one request:
|
danielebarchiesi@0
|
8046 * Either the return value of the method on success, or FALSE.
|
danielebarchiesi@0
|
8047 * If FALSE is returned, see xmlrpc_errno() and xmlrpc_error_msg().
|
danielebarchiesi@0
|
8048 * For multiple requests:
|
danielebarchiesi@0
|
8049 * An array of results. Each result will either be the result
|
danielebarchiesi@0
|
8050 * returned by the method called, or an xmlrpc_error object if the call
|
danielebarchiesi@0
|
8051 * failed. See xmlrpc_error().
|
danielebarchiesi@0
|
8052 */
|
danielebarchiesi@0
|
8053 function xmlrpc($url, $args, $options = array()) {
|
danielebarchiesi@0
|
8054 require_once DRUPAL_ROOT . '/includes/xmlrpc.inc';
|
danielebarchiesi@0
|
8055 return _xmlrpc($url, $args, $options);
|
danielebarchiesi@0
|
8056 }
|
danielebarchiesi@0
|
8057
|
danielebarchiesi@0
|
8058 /**
|
danielebarchiesi@0
|
8059 * Retrieves a list of all available archivers.
|
danielebarchiesi@0
|
8060 *
|
danielebarchiesi@0
|
8061 * @see hook_archiver_info()
|
danielebarchiesi@0
|
8062 * @see hook_archiver_info_alter()
|
danielebarchiesi@0
|
8063 */
|
danielebarchiesi@0
|
8064 function archiver_get_info() {
|
danielebarchiesi@0
|
8065 $archiver_info = &drupal_static(__FUNCTION__, array());
|
danielebarchiesi@0
|
8066
|
danielebarchiesi@0
|
8067 if (empty($archiver_info)) {
|
danielebarchiesi@0
|
8068 $cache = cache_get('archiver_info');
|
danielebarchiesi@0
|
8069 if ($cache === FALSE) {
|
danielebarchiesi@0
|
8070 // Rebuild the cache and save it.
|
danielebarchiesi@0
|
8071 $archiver_info = module_invoke_all('archiver_info');
|
danielebarchiesi@0
|
8072 drupal_alter('archiver_info', $archiver_info);
|
danielebarchiesi@0
|
8073 uasort($archiver_info, 'drupal_sort_weight');
|
danielebarchiesi@0
|
8074 cache_set('archiver_info', $archiver_info);
|
danielebarchiesi@0
|
8075 }
|
danielebarchiesi@0
|
8076 else {
|
danielebarchiesi@0
|
8077 $archiver_info = $cache->data;
|
danielebarchiesi@0
|
8078 }
|
danielebarchiesi@0
|
8079 }
|
danielebarchiesi@0
|
8080
|
danielebarchiesi@0
|
8081 return $archiver_info;
|
danielebarchiesi@0
|
8082 }
|
danielebarchiesi@0
|
8083
|
danielebarchiesi@0
|
8084 /**
|
danielebarchiesi@0
|
8085 * Returns a string of supported archive extensions.
|
danielebarchiesi@0
|
8086 *
|
danielebarchiesi@0
|
8087 * @return
|
danielebarchiesi@0
|
8088 * A space-separated string of extensions suitable for use by the file
|
danielebarchiesi@0
|
8089 * validation system.
|
danielebarchiesi@0
|
8090 */
|
danielebarchiesi@0
|
8091 function archiver_get_extensions() {
|
danielebarchiesi@0
|
8092 $valid_extensions = array();
|
danielebarchiesi@0
|
8093 foreach (archiver_get_info() as $archive) {
|
danielebarchiesi@0
|
8094 foreach ($archive['extensions'] as $extension) {
|
danielebarchiesi@0
|
8095 foreach (explode('.', $extension) as $part) {
|
danielebarchiesi@0
|
8096 if (!in_array($part, $valid_extensions)) {
|
danielebarchiesi@0
|
8097 $valid_extensions[] = $part;
|
danielebarchiesi@0
|
8098 }
|
danielebarchiesi@0
|
8099 }
|
danielebarchiesi@0
|
8100 }
|
danielebarchiesi@0
|
8101 }
|
danielebarchiesi@0
|
8102 return implode(' ', $valid_extensions);
|
danielebarchiesi@0
|
8103 }
|
danielebarchiesi@0
|
8104
|
danielebarchiesi@0
|
8105 /**
|
danielebarchiesi@0
|
8106 * Creates the appropriate archiver for the specified file.
|
danielebarchiesi@0
|
8107 *
|
danielebarchiesi@0
|
8108 * @param $file
|
danielebarchiesi@0
|
8109 * The full path of the archive file. Note that stream wrapper paths are
|
danielebarchiesi@0
|
8110 * supported, but not remote ones.
|
danielebarchiesi@0
|
8111 *
|
danielebarchiesi@0
|
8112 * @return
|
danielebarchiesi@0
|
8113 * A newly created instance of the archiver class appropriate
|
danielebarchiesi@0
|
8114 * for the specified file, already bound to that file.
|
danielebarchiesi@0
|
8115 * If no appropriate archiver class was found, will return FALSE.
|
danielebarchiesi@0
|
8116 */
|
danielebarchiesi@0
|
8117 function archiver_get_archiver($file) {
|
danielebarchiesi@0
|
8118 // Archivers can only work on local paths
|
danielebarchiesi@0
|
8119 $filepath = drupal_realpath($file);
|
danielebarchiesi@0
|
8120 if (!is_file($filepath)) {
|
danielebarchiesi@0
|
8121 throw new Exception(t('Archivers can only operate on local files: %file not supported', array('%file' => $file)));
|
danielebarchiesi@0
|
8122 }
|
danielebarchiesi@0
|
8123 $archiver_info = archiver_get_info();
|
danielebarchiesi@0
|
8124
|
danielebarchiesi@0
|
8125 foreach ($archiver_info as $implementation) {
|
danielebarchiesi@0
|
8126 foreach ($implementation['extensions'] as $extension) {
|
danielebarchiesi@0
|
8127 // Because extensions may be multi-part, such as .tar.gz,
|
danielebarchiesi@0
|
8128 // we cannot use simpler approaches like substr() or pathinfo().
|
danielebarchiesi@0
|
8129 // This method isn't quite as clean but gets the job done.
|
danielebarchiesi@0
|
8130 // Also note that the file may not yet exist, so we cannot rely
|
danielebarchiesi@0
|
8131 // on fileinfo() or other disk-level utilities.
|
danielebarchiesi@0
|
8132 if (strrpos($filepath, '.' . $extension) === strlen($filepath) - strlen('.' . $extension)) {
|
danielebarchiesi@0
|
8133 return new $implementation['class']($filepath);
|
danielebarchiesi@0
|
8134 }
|
danielebarchiesi@0
|
8135 }
|
danielebarchiesi@0
|
8136 }
|
danielebarchiesi@0
|
8137 }
|
danielebarchiesi@0
|
8138
|
danielebarchiesi@0
|
8139 /**
|
danielebarchiesi@0
|
8140 * Assembles the Drupal Updater registry.
|
danielebarchiesi@0
|
8141 *
|
danielebarchiesi@0
|
8142 * An Updater is a class that knows how to update various parts of the Drupal
|
danielebarchiesi@0
|
8143 * file system, for example to update modules that have newer releases, or to
|
danielebarchiesi@0
|
8144 * install a new theme.
|
danielebarchiesi@0
|
8145 *
|
danielebarchiesi@0
|
8146 * @return
|
danielebarchiesi@0
|
8147 * The Drupal Updater class registry.
|
danielebarchiesi@0
|
8148 *
|
danielebarchiesi@0
|
8149 * @see hook_updater_info()
|
danielebarchiesi@0
|
8150 * @see hook_updater_info_alter()
|
danielebarchiesi@0
|
8151 */
|
danielebarchiesi@0
|
8152 function drupal_get_updaters() {
|
danielebarchiesi@0
|
8153 $updaters = &drupal_static(__FUNCTION__);
|
danielebarchiesi@0
|
8154 if (!isset($updaters)) {
|
danielebarchiesi@0
|
8155 $updaters = module_invoke_all('updater_info');
|
danielebarchiesi@0
|
8156 drupal_alter('updater_info', $updaters);
|
danielebarchiesi@0
|
8157 uasort($updaters, 'drupal_sort_weight');
|
danielebarchiesi@0
|
8158 }
|
danielebarchiesi@0
|
8159 return $updaters;
|
danielebarchiesi@0
|
8160 }
|
danielebarchiesi@0
|
8161
|
danielebarchiesi@0
|
8162 /**
|
danielebarchiesi@0
|
8163 * Assembles the Drupal FileTransfer registry.
|
danielebarchiesi@0
|
8164 *
|
danielebarchiesi@0
|
8165 * @return
|
danielebarchiesi@0
|
8166 * The Drupal FileTransfer class registry.
|
danielebarchiesi@0
|
8167 *
|
danielebarchiesi@0
|
8168 * @see FileTransfer
|
danielebarchiesi@0
|
8169 * @see hook_filetransfer_info()
|
danielebarchiesi@0
|
8170 * @see hook_filetransfer_info_alter()
|
danielebarchiesi@0
|
8171 */
|
danielebarchiesi@0
|
8172 function drupal_get_filetransfer_info() {
|
danielebarchiesi@0
|
8173 $info = &drupal_static(__FUNCTION__);
|
danielebarchiesi@0
|
8174 if (!isset($info)) {
|
danielebarchiesi@0
|
8175 // Since we have to manually set the 'file path' default for each
|
danielebarchiesi@0
|
8176 // module separately, we can't use module_invoke_all().
|
danielebarchiesi@0
|
8177 $info = array();
|
danielebarchiesi@0
|
8178 foreach (module_implements('filetransfer_info') as $module) {
|
danielebarchiesi@0
|
8179 $function = $module . '_filetransfer_info';
|
danielebarchiesi@0
|
8180 if (function_exists($function)) {
|
danielebarchiesi@0
|
8181 $result = $function();
|
danielebarchiesi@0
|
8182 if (isset($result) && is_array($result)) {
|
danielebarchiesi@0
|
8183 foreach ($result as &$values) {
|
danielebarchiesi@0
|
8184 if (empty($values['file path'])) {
|
danielebarchiesi@0
|
8185 $values['file path'] = drupal_get_path('module', $module);
|
danielebarchiesi@0
|
8186 }
|
danielebarchiesi@0
|
8187 }
|
danielebarchiesi@0
|
8188 $info = array_merge_recursive($info, $result);
|
danielebarchiesi@0
|
8189 }
|
danielebarchiesi@0
|
8190 }
|
danielebarchiesi@0
|
8191 }
|
danielebarchiesi@0
|
8192 drupal_alter('filetransfer_info', $info);
|
danielebarchiesi@0
|
8193 uasort($info, 'drupal_sort_weight');
|
danielebarchiesi@0
|
8194 }
|
danielebarchiesi@0
|
8195 return $info;
|
danielebarchiesi@0
|
8196 }
|