| 1 | | <?php |
| 2 | | // $Id: common.inc,v 1.815 2008/11/01 21:21:34 dries Exp $ |
| 3 | | |
| 4 | | /** |
| 5 | | * @file |
| 6 | | * Common functions that many Drupal modules will need to reference. |
| 7 | | * |
| 8 | | * The functions that are critical and need to be available even when
serving |
| 9 | | * a cached page are instead located in bootstrap.inc. |
| 10 | | */ |
| 11 | | |
| 12 | | /** |
| 13 | | * Return status for saving which involved creating a new item. |
| 14 | | */ |
| 15 | 2366 | define('SAVED_NEW', 1); |
| 16 | | |
| 17 | | /** |
| 18 | | * Return status for saving which involved an update to an existing item. |
| 19 | | */ |
| 20 | 2366 | define('SAVED_UPDATED', 2); |
| 21 | | |
| 22 | | /** |
| 23 | | * Return status for saving which deleted an existing item. |
| 24 | | */ |
| 25 | 2366 | define('SAVED_DELETED', 3); |
| 26 | | |
| 27 | | /** |
| 28 | | * Set content for a specified region. |
| 29 | | * |
| 30 | | * @param $region |
| 31 | | * Page region the content is assigned to. |
| 32 | | * @param $data |
| 33 | | * Content to be set. |
| 34 | | */ |
| 35 | 2366 | function drupal_set_content($region = NULL, $data = NULL) { |
| 36 | 1741 | static $content = array(); |
| 37 | | |
| 38 | 1741 | if (!is_null($region) && !is_null($data)) { |
| 39 | 22 | $content[$region][] = $data; |
| 40 | 22 | } |
| 41 | 1741 | return $content; |
| 42 | 0 | } |
| 43 | | |
| 44 | | /** |
| 45 | | * Get assigned content. |
| 46 | | * |
| 47 | | * @param $region |
| 48 | | * A specified region to fetch content for. If NULL, all regions will be |
| 49 | | * returned. |
| 50 | | * @param $delimiter |
| 51 | | * Content to be inserted between exploded array elements. |
| 52 | | */ |
| 53 | 2366 | function drupal_get_content($region = NULL, $delimiter = ' ') { |
| 54 | 1741 | $content = drupal_set_content(); |
| 55 | 1741 | if (isset($region)) { |
| 56 | 1741 | if (isset($content[$region]) && is_array($content[$region])) { |
| 57 | 22 | return implode($delimiter, $content[$region]); |
| 58 | 0 | } |
| 59 | 1719 | } |
| 60 | | else { |
| 61 | 1 | foreach (array_keys($content) as $region) { |
| 62 | 1 | if (is_array($content[$region])) { |
| 63 | 1 | $content[$region] = implode($delimiter, $content[$region]); |
| 64 | 1 | } |
| 65 | 1 | } |
| 66 | 1 | return $content; |
| 67 | | } |
| 68 | 1719 | } |
| 69 | | |
| 70 | | /** |
| 71 | | * Set the breadcrumb trail for the current page. |
| 72 | | * |
| 73 | | * @param $breadcrumb |
| 74 | | * Array of links, starting with "home" and proceeding up to but not
including |
| 75 | | * the current page. |
| 76 | | */ |
| 77 | 2366 | function drupal_set_breadcrumb($breadcrumb = NULL) { |
| 78 | 1756 | static $stored_breadcrumb; |
| 79 | | |
| 80 | 1756 | if (!is_null($breadcrumb)) { |
| 81 | 123 | $stored_breadcrumb = $breadcrumb; |
| 82 | 123 | } |
| 83 | 1756 | return $stored_breadcrumb; |
| 84 | 0 | } |
| 85 | | |
| 86 | | /** |
| 87 | | * Get the breadcrumb trail for the current page. |
| 88 | | */ |
| 89 | 2366 | function drupal_get_breadcrumb() { |
| 90 | 1740 | $breadcrumb = drupal_set_breadcrumb(); |
| 91 | | |
| 92 | 1740 | if (is_null($breadcrumb)) { |
| 93 | 1633 | $breadcrumb = menu_get_active_breadcrumb(); |
| 94 | 1633 | } |
| 95 | | |
| 96 | 1740 | return $breadcrumb; |
| 97 | 0 | } |
| 98 | | |
| 99 | | /** |
| 100 | | * Add output to the head tag of the HTML page. |
| 101 | | * |
| 102 | | * This function can be called as long the headers aren't sent. |
| 103 | | */ |
| 104 | 2366 | function drupal_set_html_head($data = NULL) { |
| 105 | 1754 | static $stored_head = ''; |
| 106 | | |
| 107 | 1754 | if (!is_null($data)) { |
| 108 | 1748 | $stored_head .= $data . "\n"; |
| 109 | 1748 | } |
| 110 | 1754 | return $stored_head; |
| 111 | 0 | } |
| 112 | | |
| 113 | | /** |
| 114 | | * Retrieve output to be displayed in the head tag of the HTML page. |
| 115 | | */ |
| 116 | 2366 | function drupal_get_html_head() { |
| 117 | 1746 | $output = "<meta http-equiv=\"Content-Type\" content=\"text/html;
charset=utf-8\" />\n"; |
| 118 | 1746 | return $output . drupal_set_html_head(); |
| 119 | 0 | } |
| 120 | | |
| 121 | | /** |
| 122 | | * Reset the static variable which holds the aliases mapped for this
request. |
| 123 | | */ |
| 124 | 2366 | function drupal_clear_path_cache() { |
| 125 | 11 | drupal_lookup_path('wipe'); |
| 126 | 11 | } |
| 127 | | |
| 128 | | /** |
| 129 | | * Set an HTTP response header for the current page. |
| 130 | | * |
| 131 | | * Note: When sending a Content-Type header, always include a 'charset'
type, |
| 132 | | * too. This is necessary to avoid security bugs (e.g. UTF-7 XSS). |
| 133 | | */ |
| 134 | 2366 | function drupal_set_header($header = NULL) { |
| 135 | | // We use an array to guarantee there are no leading or trailing
delimiters. |
| 136 | | // Otherwise, header('') could get called when serving the page later,
which |
| 137 | | // ends HTTP headers prematurely on some PHP versions. |
| 138 | 2366 | static $stored_headers = array(); |
| 139 | | |
| 140 | 2366 | if (strlen($header)) { |
| 141 | 2366 | header($header); |
| 142 | 2366 | $stored_headers[] = $header; |
| 143 | 2366 | } |
| 144 | 2366 | return implode("\n", $stored_headers); |
| 145 | 0 | } |
| 146 | | |
| 147 | | /** |
| 148 | | * Get the HTTP response headers for the current page. |
| 149 | | */ |
| 150 | 2366 | function drupal_get_headers() { |
| 151 | 1 | return drupal_set_header(); |
| 152 | 0 | } |
| 153 | | |
| 154 | | /** |
| 155 | | * Add a feed URL for the current page. |
| 156 | | * |
| 157 | | * This function can be called as long the HTML header hasn't been sent. |
| 158 | | * |
| 159 | | * @param $url |
| 160 | | * A url for the feed. |
| 161 | | * @param $title |
| 162 | | * The title of the feed. |
| 163 | | */ |
| 164 | 2366 | function drupal_add_feed($url = NULL, $title = '') { |
| 165 | 1740 | static $stored_feed_links = array(); |
| 166 | | |
| 167 | 1740 | if (!is_null($url) && !isset($stored_feed_links[$url])) { |
| 168 | 43 | $stored_feed_links[$url] = theme('feed_icon', $url, $title); |
| 169 | | |
| 170 | 43 | drupal_add_link(array('rel' => 'alternate', |
| 171 | 43 | 'type' => 'application/rss+xml', |
| 172 | 43 | 'title' => $title, |
| 173 | 43 | 'href' => $url)); |
| 174 | 43 | } |
| 175 | 1740 | return $stored_feed_links; |
| 176 | 0 | } |
| 177 | | |
| 178 | | /** |
| 179 | | * Get the feed URLs for the current page. |
| 180 | | * |
| 181 | | * @param $delimiter |
| 182 | | * A delimiter to split feeds by. |
| 183 | | */ |
| 184 | 2366 | function drupal_get_feeds($delimiter = "\n") { |
| 185 | 1740 | $feeds = drupal_add_feed(); |
| 186 | 1740 | return implode($feeds, $delimiter); |
| 187 | 0 | } |
| 188 | | |
| 189 | | /** |
| 190 | | * @name HTTP handling |
| 191 | | * @{ |
| 192 | | * Functions to properly handle HTTP responses. |
| 193 | | */ |
| 194 | | |
| 195 | | /** |
| 196 | | * Parse an array into a valid urlencoded query string. |
| 197 | | * |
| 198 | | * @param $query |
| 199 | | * The array to be processed e.g. $_GET. |
| 200 | | * @param $exclude |
| 201 | | * The array filled with keys to be excluded. Use parent[child] to
exclude |
| 202 | | * nested items. |
| 203 | | * @param $parent |
| 204 | | * Should not be passed, only used in recursive calls. |
| 205 | | * @return |
| 206 | | * An urlencoded string which can be appended to/as the URL query string. |
| 207 | | */ |
| 208 | 2366 | function drupal_query_string_encode($query, $exclude = array(), $parent =
'') { |
| 209 | 549 | $params = array(); |
| 210 | | |
| 211 | 549 | foreach ($query as $key => $value) { |
| 212 | 549 | $key = drupal_urlencode($key); |
| 213 | 549 | if ($parent) { |
| 214 | 7 | $key = $parent . '[' . $key . ']'; |
| 215 | 7 | } |
| 216 | | |
| 217 | 549 | if (in_array($key, $exclude)) { |
| 218 | 541 | continue; |
| 219 | 0 | } |
| 220 | | |
| 221 | 45 | if (is_array($value)) { |
| 222 | 7 | $params[] = drupal_query_string_encode($value, $exclude, $key); |
| 223 | 7 | } |
| 224 | | else { |
| 225 | 45 | $params[] = $key . '=' . drupal_urlencode($value); |
| 226 | | } |
| 227 | 45 | } |
| 228 | | |
| 229 | 549 | return implode('&', $params); |
| 230 | 0 | } |
| 231 | | |
| 232 | | /** |
| 233 | | * Prepare a destination query string for use in combination with
drupal_goto(). |
| 234 | | * |
| 235 | | * Used to direct the user back to the referring page after completing a
form. |
| 236 | | * By default the current URL is returned. If a destination exists in the |
| 237 | | * previous request, that destination is returned. As such, a destination
can |
| 238 | | * persist across multiple pages. |
| 239 | | * |
| 240 | | * @see drupal_goto() |
| 241 | | */ |
| 242 | 2366 | function drupal_get_destination() { |
| 243 | 325 | if (isset($_REQUEST['destination'])) { |
| 244 | 9 | return 'destination=' . urlencode($_REQUEST['destination']); |
| 245 | 0 | } |
| 246 | | else { |
| 247 | | // Use $_GET here to retrieve the original path in source form. |
| 248 | 316 | $path = isset($_GET['q']) ? $_GET['q'] : ''; |
| 249 | 316 | $query = drupal_query_string_encode($_GET, array('q')); |
| 250 | 316 | if ($query != '') { |
| 251 | 1 | $path .= '?' . $query; |
| 252 | 1 | } |
| 253 | 316 | return 'destination=' . urlencode($path); |
| 254 | | } |
| 255 | 0 | } |
| 256 | | |
| 257 | | /** |
| 258 | | * Send the user to a different Drupal page. |
| 259 | | * |
| 260 | | * This issues an on-site HTTP redirect. The function makes sure the
redirected |
| 261 | | * URL is formatted correctly. |
| 262 | | * |
| 263 | | * Usually the redirected URL is constructed from this function's input |
| 264 | | * parameters. However you may override that behavior by setting a |
| 265 | | * destination in either the $_REQUEST-array (i.e. by using |
| 266 | | * the query string of an URI) This is used to direct the user back to |
| 267 | | * the proper page after completing a form. For example, after editing |
| 268 | | * a post on the 'admin/content/node'-page or after having logged on using
the |
| 269 | | * 'user login'-block in a sidebar. The function drupal_get_destination() |
| 270 | | * can be used to help set the destination URL. |
| 271 | | * |
| 272 | | * Drupal will ensure that messages set by drupal_set_message() and other |
| 273 | | * session data are written to the database before the user is redirected. |
| 274 | | * |
| 275 | | * This function ends the request; use it rather than a print theme('page') |
| 276 | | * statement in your menu callback. |
| 277 | | * |
| 278 | | * @param $path |
| 279 | | * A Drupal path or a full URL. |
| 280 | | * @param $query |
| 281 | | * A query string component, if any. |
| 282 | | * @param $fragment |
| 283 | | * A destination fragment identifier (named anchor). |
| 284 | | * @param $http_response_code |
| 285 | | * Valid values for an actual "goto" as per RFC 2616 section 10.3 are: |
| 286 | | * - 301 Moved Permanently (the recommended value for most redirects) |
| 287 | | * - 302 Found (default in Drupal and PHP, sometimes used for spamming
search |
| 288 | | * engines) |
| 289 | | * - 303 See Other |
| 290 | | * - 304 Not Modified |
| 291 | | * - 305 Use Proxy |
| 292 | | * - 307 Temporary Redirect (alternative to "503 Site Down for
Maintenance") |
| 293 | | * Note: Other values are defined by RFC 2616, but are rarely used and
poorly |
| 294 | | * supported. |
| 295 | | * @see drupal_get_destination() |
| 296 | | */ |
| 297 | 2366 | function drupal_goto($path = '', $query = NULL, $fragment = NULL,
$http_response_code = 302) { |
| 298 | | |
| 299 | 577 | if (isset($_REQUEST['destination'])) { |
| 300 | 3 | extract(parse_url(urldecode($_REQUEST['destination']))); |
| 301 | 3 | } |
| 302 | | |
| 303 | 577 | $url = url($path, array('query' => $query, 'fragment' => $fragment,
'absolute' => TRUE)); |
| 304 | | // Remove newlines from the URL to avoid header injection attacks. |
| 305 | 577 | $url = str_replace(array("\n", "\r"), '', $url); |
| 306 | | |
| 307 | | // Allow modules to react to the end of the page request before
redirecting. |
| 308 | | // We do not want this while running update.php. |
| 309 | 577 | if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') { |
| 310 | 577 | module_invoke_all('exit', $url); |
| 311 | 577 | } |
| 312 | | |
| 313 | | // Even though session_write_close() is registered as a shutdown
function, we |
| 314 | | // need all session data written to the database before redirecting. |
| 315 | 577 | session_write_close(); |
| 316 | | |
| 317 | 577 | header('Location: ' . $url, TRUE, $http_response_code); |
| 318 | | |
| 319 | | // The "Location" header sends a redirect status code to the HTTP daemon.
In |
| 320 | | // some cases this can be wrong, so we make sure none of the code below
the |
| 321 | | // drupal_goto() call gets executed upon redirection. |
| 322 | 577 | exit(); |
| 323 | 0 | } |
| 324 | | |
| 325 | | /** |
| 326 | | * Generates a site offline message. |
| 327 | | */ |
| 328 | 2366 | function drupal_site_offline() { |
| 329 | 0 | drupal_maintenance_theme(); |
| 330 | 0 | drupal_set_header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service
unavailable'); |
| 331 | 0 | drupal_set_title(t('Site offline')); |
| 332 | 0 | print theme('maintenance_page',
filter_xss_admin(variable_get('site_offline_message', |
| 333 | 0 | t('@site is currently under maintenance. We should be back shortly.
Thank you for your patience.', array('@site' => variable_get('site_name',
'Drupal')))))); |
| 334 | 0 | } |
| 335 | | |
| 336 | | /** |
| 337 | | * Generates a 404 error if the request can not be handled. |
| 338 | | */ |
| 339 | 2366 | function drupal_not_found() { |
| 340 | 16 | drupal_set_header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found'); |
| 341 | | |
| 342 | 16 | watchdog('page not found', check_plain($_GET['q']), NULL,
WATCHDOG_WARNING); |
| 343 | | |
| 344 | | // Keep old path for reference. |
| 345 | 16 | if (!isset($_REQUEST['destination'])) { |
| 346 | 16 | $_REQUEST['destination'] = $_GET['q']; |
| 347 | 16 | } |
| 348 | | |
| 349 | 16 | $path = drupal_get_normal_path(variable_get('site_404', '')); |
| 350 | 16 | if ($path && $path != $_GET['q']) { |
| 351 | | // Set the active item in case there are tabs to display, or other |
| 352 | | // dependencies on the path. |
| 353 | 2 | menu_set_active_item($path); |
| 354 | 2 | $return = menu_execute_active_handler($path); |
| 355 | 2 | } |
| 356 | | |
| 357 | 16 | if (empty($return) || $return == MENU_NOT_FOUND || $return ==
MENU_ACCESS_DENIED) { |
| 358 | 14 | drupal_set_title(t('Page not found')); |
| 359 | 14 | $return = t('The requested page could not be found.'); |
| 360 | 14 | } |
| 361 | | |
| 362 | | // To conserve CPU and bandwidth, omit the blocks. |
| 363 | 16 | print theme('page', $return, FALSE); |
| 364 | 16 | } |
| 365 | | |
| 366 | | /** |
| 367 | | * Generates a 403 error if the request is not allowed. |
| 368 | | */ |
| 369 | 2366 | function drupal_access_denied() { |
| 370 | 51 | drupal_set_header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden'); |
| 371 | 51 | watchdog('access denied', check_plain($_GET['q']), NULL,
WATCHDOG_WARNING); |
| 372 | | |
| 373 | | // Keep old path for reference. |
| 374 | 51 | if (!isset($_REQUEST['destination'])) { |
| 375 | 51 | $_REQUEST['destination'] = $_GET['q']; |
| 376 | 51 | } |
| 377 | | |
| 378 | 51 | $path = drupal_get_normal_path(variable_get('site_403', '')); |
| 379 | 51 | if ($path && $path != $_GET['q']) { |
| 380 | | // Set the active item in case there are tabs to display or other |
| 381 | | // dependencies on the path. |
| 382 | 2 | menu_set_active_item($path); |
| 383 | 2 | $return = menu_execute_active_handler($path); |
| 384 | 2 | } |
| 385 | | |
| 386 | 51 | if (empty($return) || $return == MENU_NOT_FOUND || $return ==
MENU_ACCESS_DENIED) { |
| 387 | 49 | drupal_set_title(t('Access denied')); |
| 388 | 49 | $return = t('You are not authorized to access this page.'); |
| 389 | 49 | } |
| 390 | 51 | print theme('page', $return); |
| 391 | 51 | } |
| 392 | | |
| 393 | | /** |
| 394 | | * Perform an HTTP request. |
| 395 | | * |
| 396 | | * This is a flexible and powerful HTTP client implementation. Correctly
handles |
| 397 | | * GET, POST, PUT or any other HTTP requests. Handles redirects. |
| 398 | | * |
| 399 | | * @param $url |
| 400 | | * A string containing a fully qualified URI. |
| 401 | | * @param $headers |
| 402 | | * An array containing an HTTP header => value pair. |
| 403 | | * @param $method |
| 404 | | * A string defining the HTTP request to use. |
| 405 | | * @param $data |
| 406 | | * A string containing data to include in the request. |
| 407 | | * @param $retry |
| 408 | | * An integer representing how many times to retry the request in case of
a |
| 409 | | * redirect. |
| 410 | | * @return |
| 411 | | * An object containing the HTTP request headers, response code, headers, |
| 412 | | * data and redirect status. |
| 413 | | */ |
| 414 | 2366 | function drupal_http_request($url, $headers = array(), $method = 'GET',
$data = NULL, $retry = 3) { |
| 415 | 10 | global $db_prefix; |
| 416 | 10 | static $self_test = FALSE; |
| 417 | 10 | $result = new stdClass(); |
| 418 | | // Try to clear the drupal_http_request_fails variable if it's set. We |
| 419 | | // can't tie this call to any error because there is no surefire way to |
| 420 | | // tell whether a request has failed, so we add the check to places where |
| 421 | | // some parsing has failed. |
| 422 | 10 | if (!$self_test && variable_get('drupal_http_request_fails', FALSE)) { |
| 423 | 0 | $self_test = TRUE; |
| 424 | 0 | $works = module_invoke('system', 'check_http_request'); |
| 425 | 0 | $self_test = FALSE; |
| 426 | 0 | if (!$works) { |
| 427 | | // Do not bother with further operations if we already know that we |
| 428 | | // have no chance. |
| 429 | 0 | $result->error = t("The server can't issue HTTP requests"); |
| 430 | 0 | return $result; |
| 431 | 0 | } |
| 432 | 0 | } |
| 433 | | |
| 434 | | // Parse the URL and make sure we can handle the schema. |
| 435 | 10 | $uri = @parse_url($url); |
| 436 | | |
| 437 | 10 | if ($uri == FALSE) { |
| 438 | 1 | $result->error = 'unable to parse URL'; |
| 439 | 1 | return $result; |
| 440 | 0 | } |
| 441 | | |
| 442 | 10 | if (!isset($uri['scheme'])) { |
| 443 | 1 | $result->error = 'missing schema'; |
| 444 | 1 | return $result; |
| 445 | 0 | } |
| 446 | | |
| 447 | 10 | switch ($uri['scheme']) { |
| 448 | 10 | case 'http': |
| 449 | 10 | $port = isset($uri['port']) ? $uri['port'] : 80; |
| 450 | 10 | $host = $uri['host'] . ($port != 80 ? ':' . $port : ''); |
| 451 | 10 | $fp = @fsockopen($uri['host'], $port, $errno, $errstr, 15); |
| 452 | 10 | break; |
| 453 | 1 | case 'https': |
| 454 | | // Note: Only works for PHP 4.3 compiled with OpenSSL. |
| 455 | 0 | $port = isset($uri['port']) ? $uri['port'] : 443; |
| 456 | 0 | $host = $uri['host'] . ($port != 443 ? ':' . $port : ''); |
| 457 | 0 | $fp = @fsockopen('ssl://' . $uri['host'], $port, $errno, $errstr,
20); |
| 458 | 0 | break; |
| 459 | 1 | default: |
| 460 | 1 | $result->error = 'invalid schema ' . $uri['scheme']; |
| 461 | 1 | return $result; |
| 462 | 1 | } |
| 463 | | |
| 464 | | // Make sure the socket opened properly. |
| 465 | 10 | if (!$fp) { |
| 466 | | // When a network error occurs, we use a negative number so it does not |
| 467 | | // clash with the HTTP status codes. |
| 468 | 0 | $result->code = -$errno; |
| 469 | 0 | $result->error = trim($errstr); |
| 470 | 0 | return $result; |
| 471 | 0 | } |
| 472 | | |
| 473 | | // Construct the path to act on. |
| 474 | 10 | $path = isset($uri['path']) ? $uri['path'] : '/'; |
| 475 | 10 | if (isset($uri['query'])) { |
| 476 | 3 | $path .= '?' . $uri['query']; |
| 477 | 3 | } |
| 478 | | |
| 479 | | // Create HTTP request. |
| 480 | | $defaults = array( |
| 481 | | // RFC 2616: "non-standard ports MUST, default ports MAY be included". |
| 482 | | // We don't add the port to prevent from breaking rewrite rules
checking the |
| 483 | | // host that do not take into account the port number. |
| 484 | 10 | 'Host' => "Host: $host", |
| 485 | 10 | 'User-Agent' => 'User-Agent: Drupal (+http://drupal.org/)', |
| 486 | 10 | 'Content-Length' => 'Content-Length: ' . strlen($data) |
| 487 | 10 | ); |
| 488 | | |
| 489 | | // If the server url has a user then attempt to use basic authentication |
| 490 | 10 | if (isset($uri['user'])) { |
| 491 | 1 | $defaults['Authorization'] = 'Authorization: Basic ' .
base64_encode($uri['user'] . (!empty($uri['pass']) ? ":" . $uri['pass'] :
'')); |
| 492 | 1 | } |
| 493 | | |
| 494 | | // If the database prefix is being used by SimpleTest to run the tests in
a copied |
| 495 | | // database then set the user-agent header to the database prefix so that
any |
| 496 | | // calls to other Drupal pages will run the SimpleTest prefixed database.
The |
| 497 | | // user-agent is used to ensure that multiple testing sessions running at
the |
| 498 | | // same time won't interfere with each other as they would if the
database |
| 499 | | // prefix were stored statically in a file or database variable. |
| 500 | 10 | if (preg_match("/simpletest\d+/", $db_prefix, $matches)) { |
| 501 | 10 | $headers['User-Agent'] = $matches[0]; |
| 502 | 10 | } |
| 503 | | |
| 504 | 10 | foreach ($headers as $header => $value) { |
| 505 | 10 | $defaults[$header] = $header . ': ' . $value; |
| 506 | 10 | } |
| 507 | | |
| 508 | 10 | $request = $method . ' ' . $path . " HTTP/1.0\r\n"; |
| 509 | 10 | $request .= implode("\r\n", $defaults); |
| 510 | 10 | $request .= "\r\n\r\n"; |
| 511 | 10 | if ($data) { |
| 512 | 3 | $request .= $data . "\r\n"; |
| 513 | 3 | } |
| 514 | 10 | $result->request = $request; |
| 515 | | |
| 516 | 10 | fwrite($fp, $request); |
| 517 | | |
| 518 | | // Fetch response. |
| 519 | 10 | $response = ''; |
| 520 | 10 | while (!feof($fp) && $chunk = fread($fp, 1024)) { |
| 521 | 10 | $response .= $chunk; |
| 522 | 10 | } |
| 523 | 10 | fclose($fp); |
| 524 | | |
| 525 | | // Parse response. |
| 526 | 10 | list($split, $result->data) = explode("\r\n\r\n", $response, 2); |
| 527 | 10 | $split = preg_split("/\r\n|\n|\r/", $split); |
| 528 | | |
| 529 | 10 | list($protocol, $code, $text) = explode(' ', trim(array_shift($split)),
3); |
| 530 | 10 | $result->headers = array(); |
| 531 | | |
| 532 | | // Parse headers. |
| 533 | 10 | while ($line = trim(array_shift($split))) { |
| 534 | 10 | list($header, $value) = explode(':', $line, 2); |
| 535 | 10 | if (isset($result->headers[$header]) && $header == 'Set-Cookie') { |
| 536 | | // RFC 2109: the Set-Cookie response header comprises the token Set- |
| 537 | | // Cookie:, followed by a comma-separated list of one or more
cookies. |
| 538 | 1 | $result->headers[$header] .= ',' . trim($value); |
| 539 | 1 | } |
| 540 | | else { |
| 541 | 10 | $result->headers[$header] = trim($value); |
| 542 | | } |
| 543 | 10 | } |
| 544 | | |
| 545 | | $responses = array( |
| 546 | 10 | 100 => 'Continue', 101 => 'Switching Protocols', |
| 547 | 10 | 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 =>
'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset
Content', 206 => 'Partial Content', |
| 548 | 10 | 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found',
303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 =>
'Temporary Redirect', |
| 549 | 10 | 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required',
403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 =>
'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request
Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412
=> 'Precondition Failed', 413 => 'Request Entity Too Large', 414 =>
'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested
range not satisfiable', 417 => 'Expectation Failed', |
| 550 | 10 | 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad
Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 =>
'HTTP Version not supported' |
| 551 | 10 | ); |
| 552 | | // RFC 2616 states that all unknown HTTP codes must be treated the same
as the |
| 553 | | // base code in their class. |
| 554 | 10 | if (!isset($responses[$code])) { |
| 555 | 0 | $code = floor($code / 100) * 100; |
| 556 | 0 | } |
| 557 | | |
| 558 | | switch ($code) { |
| 559 | 10 | case 200: // OK |
| 560 | 10 | case 304: // Not modified |
| 561 | 10 | break; |
| 562 | 1 | case 301: // Moved permanently |
| 563 | 1 | case 302: // Moved temporarily |
| 564 | 1 | case 307: // Moved temporarily |
| 565 | 1 | $location = $result->headers['Location']; |
| 566 | | |
| 567 | 1 | if ($retry) { |
| 568 | 1 | $result = drupal_http_request($location, $headers, $method, $data,
--$retry); |
| 569 | 1 | $result->redirect_code = $code; |
| 570 | 1 | } |
| 571 | 1 | $result->redirect_url = $location; |
| 572 | | |
| 573 | 1 | break; |
| 574 | 0 | default: |
| 575 | 0 | $result->error = $text; |
| 576 | 0 | } |
| 577 | | |
| 578 | 10 | $result->code = $code; |
| 579 | 10 | return $result; |
| 580 | 0 | } |
| 581 | | /** |
| 582 | | * @} End of "HTTP handling". |
| 583 | | */ |
| 584 | | |
| 585 | | /** |
| 586 | | * Custom PHP error handler. |
| 587 | | * |
| 588 | | * @param $error_level |
| 589 | | * The level of the error raised. |
| 590 | | * @param $message |
| 591 | | * The error message. |
| 592 | | * @param $filename |
| 593 | | * The filename that the error was raised in. |
| 594 | | * @param $line |
| 595 | | * The line number the error was raised at. |
| 596 | | * @param $context |
| 597 | | * An array that points to the active symbol table at the point the error
occurred. |
| 598 | | */ |
| 599 | 2366 | function _drupal_error_handler($error_level, $message, $filename, $line,
$context) { |
| 600 | 159 | if ($error_level & error_reporting()) { |
| 601 | | // All these constants are documented at
http://php.net/manual/en/errorfunc.constants.php |
| 602 | | $types = array( |
| 603 | 54 | E_ERROR => 'Error', |
| 604 | 54 | E_WARNING => 'Warning', |
| 605 | 54 | E_PARSE => 'Parse error', |
| 606 | 54 | E_NOTICE => 'Notice', |
| 607 | 54 | E_CORE_ERROR => 'Core error', |
| 608 | 54 | E_CORE_WARNING => 'Core warning', |
| 609 | 54 | E_COMPILE_ERROR => 'Compile error', |
| 610 | 54 | E_COMPILE_WARNING => 'Compile warning', |
| 611 | 54 | E_USER_ERROR => 'User error', |
| 612 | 54 | E_USER_WARNING => 'User warning', |
| 613 | 54 | E_USER_NOTICE => 'User notice', |
| 614 | 54 | E_STRICT => 'Strict warning', |
| 615 | 54 | E_RECOVERABLE_ERROR => 'Recoverable fatal error' |
| 616 | 54 | ); |
| 617 | 54 | $backtrace = debug_backtrace(); |
| 618 | | // We treat recoverable errors as fatal. |
| 619 | 54 | _drupal_log_error(isset($types[$error_level]) ? $types[$error_level] :
'Unknown error', $message, $backtrace, $error_level ==
E_RECOVERABLE_ERROR); |
| 620 | 54 | } |
| 621 | 159 | } |
| 622 | | |
| 623 | | /** |
| 624 | | * Custom PHP exception handler. |
| 625 | | * |
| 626 | | * Uncaught exceptions are those not enclosed in a try/catch block. They
are |
| 627 | | * always fatal: the execution of the script will stop as soon as the
exception |
| 628 | | * handler exits. |
| 629 | | * |
| 630 | | * @param $exception |
| 631 | | * The exception object that was thrown. |
| 632 | | */ |
| 633 | 2366 | function _drupal_exception_handler($exception) { |
| 634 | 2 | $backtrace = $exception->getTrace(); |
| 635 | | // Add the line throwing the exception to the backtrace. |
| 636 | 2 | array_unshift($backtrace, array('line' => $exception->getLine(), 'file'
=> $exception->getFile())); |
| 637 | | |
| 638 | | // For PDOException errors, we try to return the initial caller, |
| 639 | | // skipping internal functions of the database layer. |
| 640 | 2 | if ($exception instanceof PDOException) { |
| 641 | | // The first element in the stack is the call, the second element gives
us the caller. |
| 642 | | // We skip calls that occurred in one of the classes of the database
layer |
| 643 | | // or in one of its global functions. |
| 644 | 1 | $db_functions = array('db_query', 'pager_query', 'db_query_range',
'db_query_temporary', 'update_sql'); |
| 645 | 1 | while (($caller = $backtrace[1]) && |
| 646 | 1 | ((isset($caller['class']) && (strpos($caller['class'], 'Query')
!== FALSE || strpos($caller['class'], 'Database') !== FALSE)) || |
| 647 | 1 | in_array($caller['function'], $db_functions))) { |
| 648 | | // We remove that call. |
| 649 | 1 | array_shift($backtrace); |
| 650 | 1 | } |
| 651 | 1 | } |
| 652 | | |
| 653 | | // Log the message to the watchdog and return an error page to the user. |
| 654 | 2 | _drupal_log_error(get_class($exception), $exception->getMessage(),
$backtrace, TRUE); |
| 655 | 0 | } |
| 656 | | |
| 657 | | /** |
| 658 | | * Log a PHP error or exception, display an error page in fatal cases. |
| 659 | | * |
| 660 | | * @param $type |
| 661 | | * The type of the error (Error, Warning, ...). |
| 662 | | * @param $message |
| 663 | | * The message associated to the error. |
| 664 | | * @param $backtrace |
| 665 | | * The backtrace of function calls that led to this error. |
| 666 | | * @param $fatal |
| 667 | | * TRUE if the error is fatal. |
| 668 | | */ |
| 669 | 2366 | function _drupal_log_error($type, $message, $backtrace, $fatal) { |
| 670 | 56 | $caller = _drupal_get_last_caller($backtrace); |
| 671 | | |
| 672 | | // Initialize a maintenance theme early if the boostrap was not complete. |
| 673 | | // Do it early because drupal_set_message() triggers an init_theme(). |
| 674 | 56 | if ($fatal && (drupal_get_bootstrap_phase() != DRUPAL_BOOTSTRAP_FULL)) { |
| 675 | 0 | unset($GLOBALS['theme']); |
| 676 | 0 | define('MAINTENANCE_MODE', 'error'); |
| 677 | 0 | drupal_maintenance_theme(); |
| 678 | 0 | } |
| 679 | | |
| 680 | | // Force display of error messages in update.php. |
| 681 | 56 | if (variable_get('error_level', 1) == 1 || (defined('MAINTENANCE_MODE')
&& MAINTENANCE_MODE == 'update')) { |
| 682 | 56 | drupal_set_message(t('@type: %message in %function (line %line of
%file).', array('@type' => $type, '%message' => $message, '%function' =>
$caller['function'], '%line' => $caller['line'], '%file' =>
$caller['file'])), 'error'); |
| 683 | 56 | } |
| 684 | | |
| 685 | 56 | watchdog('php', '%type: %message in %function (line %line of %file).',
array('%type' => $type, '%message' => $message, '%function' =>
$caller['function'], '%file' => $caller['file'], '%line' =>
$caller['line']), WATCHDOG_ERROR); |
| 686 | | |
| 687 | 56 | if ($fatal) { |
| 688 | 2 | drupal_set_header($_SERVER['SERVER_PROTOCOL'] . ' Service
unavailable'); |
| 689 | 2 | drupal_set_title(t('Error')); |
| 690 | 2 | if (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL) { |
| 691 | 2 | print theme('page', t('The website encountered an unexpected error.
Please try again later.'), FALSE); |
| 692 | 2 | } |
| 693 | | else { |
| 694 | 0 | print theme('maintenance_page', t('The website encountered an
unexpected error. Please try again later.'), FALSE); |
| 695 | | } |
| 696 | 2 | exit; |
| 697 | 0 | } |
| 698 | 54 | } |
| 699 | | |
| 700 | | /** |
| 701 | | * Gets the last caller from a backtrace. |
| 702 | | * |
| 703 | | * @param $backtrace |
| 704 | | * A standard PHP backtrace. |
| 705 | | * @return |
| 706 | | * An associative array with keys 'file', 'line' and 'function'. |
| 707 | | */ |
| 708 | 2366 | function _drupal_get_last_caller($backtrace) { |
| 709 | | // Errors that occur inside PHP internal functions |
| 710 | | // do not generate information about file and line. |
| 711 | 187 | while ($backtrace && !isset($backtrace[0]['line'])) { |
| 712 | 7 | array_shift($backtrace); |
| 713 | 7 | } |
| 714 | | |
| 715 | | // The first trace is the call itself. |
| 716 | | // It gives us the line and the file of the last call. |
| 717 | 187 | $call = $backtrace[0]; |
| 718 | | |
| 719 | | // The second call give us the function where the call originated. |
| 720 | 187 | if (isset($backtrace[1])) { |
| 721 | 187 | if (isset($backtrace[1]['class'])) { |
| 722 | 136 | $call['function'] = $backtrace[1]['class'] . $backtrace[1]['type'] .
$backtrace[1]['function'] . '()'; |
| 723 | 136 | } |
| 724 | | else { |
| 725 | 55 | $call['function'] = $backtrace[1]['function'] . '()'; |
| 726 | | } |
| 727 | 187 | } |
| 728 | | else { |
| 729 | 0 | $call['function'] = 'main()'; |
| 730 | | } |
| 731 | 187 | return $call; |
| 732 | 0 | } |
| 733 | | |
| 734 | 2366 | function _fix_gpc_magic(&$item) { |
| 735 | 0 | if (is_array($item)) { |
| 736 | 0 | array_walk($item, '_fix_gpc_magic'); |
| 737 | 0 | } |
| 738 | | else { |
| 739 | 0 | $item = stripslashes($item); |
| 740 | | } |
| 741 | 0 | } |
| 742 | | |
| 743 | | /** |
| 744 | | * Helper function to strip slashes from $_FILES skipping over the tmp_name
keys |
| 745 | | * since PHP generates single backslashes for file paths on Windows
systems. |
| 746 | | * |
| 747 | | * tmp_name does not have backslashes added see |
| 748 | | * http://php.net/manual/en/features.file-upload.php#42280 |
| 749 | | */ |
| 750 | 2366 | function _fix_gpc_magic_files(&$item, $key) { |
| 751 | 0 | if ($key != 'tmp_name') { |
| 752 | 0 | if (is_array($item)) { |
| 753 | 0 | array_walk($item, '_fix_gpc_magic_files'); |
| 754 | 0 | } |
| 755 | | else { |
| 756 | 0 | $item = stripslashes($item); |
| 757 | | } |
| 758 | 0 | } |
| 759 | 0 | } |
| 760 | | |
| 761 | | /** |
| 762 | | * Fix double-escaping problems caused by "magic quotes" in some PHP
installations. |
| 763 | | */ |
| 764 | 2366 | function fix_gpc_magic() { |
| 765 | 2366 | static $fixed = FALSE; |
| 766 | 2366 | if (!$fixed && ini_get('magic_quotes_gpc')) { |
| 767 | 0 | array_walk($_GET, '_fix_gpc_magic'); |
| 768 | 0 | array_walk($_POST, '_fix_gpc_magic'); |
| 769 | 0 | array_walk($_COOKIE, '_fix_gpc_magic'); |
| 770 | 0 | array_walk($_REQUEST, '_fix_gpc_magic'); |
| 771 | 0 | array_walk($_FILES, '_fix_gpc_magic_files'); |
| 772 | 0 | $fixed = TRUE; |
| 773 | 0 | } |
| 774 | 2366 | } |
| 775 | | |
| 776 | | /** |
| 777 | | * Translate strings to the page language or a given language. |
| 778 | | * |
| 779 | | * All human-readable text that will be displayed somewhere within a page
should |
| 780 | | * be run through the t() function. |
| 781 | | * |
| 782 | | * Examples: |
| 783 | | * @code |
| 784 | | * if (!$info || !$info['extension']) { |
| 785 | | * form_set_error('picture_upload', t('The uploaded file was not an
image.')); |
| 786 | | * } |
| 787 | | * |
| 788 | | * $form['submit'] = array( |
| 789 | | * '#type' => 'submit', |
| 790 | | * '#value' => t('Log in'), |
| 791 | | * ); |
| 792 | | * @endcode |
| 793 | | * |
| 794 | | * Any text within t() can be extracted by translators and changed into |
| 795 | | * the equivalent text in their native language. |
| 796 | | * |
| 797 | | * Special variables called "placeholders" are used to signal dynamic |
| 798 | | * information in a string which should not be translated. Placeholders |
| 799 | | * can also be used for text that may change from time to time |
| 800 | | * (such as link paths) to be changed without requiring updates to
translations. |
| 801 | | * |
| 802 | | * For example: |
| 803 | | * @code |
| 804 | | * $output = t('There are currently %members and %visitors online.',
array( |
| 805 | | * '%members' => format_plural($total_users, '1 user', '@count users'), |
| 806 | | * '%visitors' => format_plural($guests->count, '1 guest', '@count
guests'))); |
| 807 | | * @endcode |
| 808 | | * |
| 809 | | * There are three styles of placeholders: |
| 810 | | * - !variable, which indicates that the text should be inserted as-is.
This is |
| 811 | | * useful for inserting variables into things like e-mail. |
| 812 | | * @code |
| 813 | | * $message[] = t("If you don't want to receive such e-mails, you can
change your settings at !url.", array('!url' => url("user/$account->uid",
array('absolute' => TRUE)))); |
| 814 | | * @endcode |
| 815 | | * |
| 816 | | * - @variable, which indicates that the text should be run through
check_plain, |
| 817 | | * to escape HTML characters. Use this for any output that's displayed
within |
| 818 | | * a Drupal page. |
| 819 | | * @code |
| 820 | | * drupal_set_title($title = t("@name's blog", array('@name' =>
$account->name)), PASS_THROUGH); |
| 821 | | * @endcode |
| 822 | | * |
| 823 | | * - %variable, which indicates that the string should be HTML escaped and |
| 824 | | * highlighted with theme_placeholder() which shows up by default as |
| 825 | | * <em>emphasized</em>. |
| 826 | | * @code |
| 827 | | * $message = t('%name-from sent %name-to an e-mail.',
array('%name-from' => $user->name, '%name-to' => $account->name)); |
| 828 | | * @endcode |
| 829 | | * |
| 830 | | * When using t(), try to put entire sentences and strings in one t() call. |
| 831 | | * This makes it easier for translators, as it provides context as to what
each |
| 832 | | * word refers to. HTML markup within translation strings is allowed, but
should |
| 833 | | * be avoided if possible. The exception are embedded links; link titles
add a |
| 834 | | * context for translators, so should be kept in the main string. |
| 835 | | * |
| 836 | | * Here is an example of incorrect usage of t(): |
| 837 | | * @code |
| 838 | | * $output .= t('<p>Go to the @contact-page.</p>', array('@contact-page'
=> l(t('contact page'), 'contact'))); |
| 839 | | * @endcode |
| 840 | | * |
| 841 | | * Here is an example of t() used correctly: |
| 842 | | * @code |
| 843 | | * $output .= '<p>' . t('Go to the <a href="@contact-page">contact
page</a>.', array('@contact-page' => url('contact'))) . '</p>'; |
| 844 | | * @endcode |
| 845 | | * |
| 846 | | * Also avoid escaping quotation marks wherever possible. |
| 847 | | * |
| 848 | | * Incorrect: |
| 849 | | * @code |
| 850 | | * $output .= t('Don\'t click me.'); |
| 851 | | * @endcode |
| 852 | | * |
| 853 | | * Correct: |
| 854 | | * @code |
| 855 | | * $output .= t("Don't click me."); |
| 856 | | * @endcode |
| 857 | | * |
| 858 | | * @param $string |
| 859 | | * A string containing the English string to translate. |
| 860 | | * @param $args |
| 861 | | * An associative array of replacements to make after translation.
Incidences |
| 862 | | * of any key in this array are replaced with the corresponding value. |
| 863 | | * Based on the first character of the key, the value is escaped and/or
themed: |
| 864 | | * - !variable: inserted as is |
| 865 | | * - @variable: escape plain text to HTML (check_plain) |
| 866 | | * - %variable: escape text and theme as a placeholder for
user-submitted |
| 867 | | * content (check_plain + theme_placeholder) |
| 868 | | * @param $langcode |
| 869 | | * Optional language code to translate to a language other than what is
used |
| 870 | | * to display the page. |
| 871 | | * @return |
| 872 | | * The translated string. |
| 873 | | */ |
| 874 | 2366 | function t($string, $args = array(), $langcode = NULL) { |
| 875 | 2497 | global $language; |
| 876 | 2497 | static $custom_strings; |
| 877 | | |
| 878 | 2497 | if (!isset($langcode)) { |
| 879 | 2497 | $langcode = $language->language; |
| 880 | 2497 | } |
| 881 | | |
| 882 | | // First, check for an array of customized strings. If present, use the
array |
| 883 | | // *instead of* database lookups. This is a high performance way to
provide a |
| 884 | | // handful of string replacements. See settings.php for examples. |
| 885 | | // Cache the $custom_strings variable to improve performance. |
| 886 | 2497 | if (!isset($custom_strings[$langcode])) { |
| 887 | 2367 | $custom_strings[$langcode] = variable_get('locale_custom_strings_' .
$langcode, array()); |
| 888 | 2367 | } |
| 889 | | // Custom strings work for English too, even if locale module is
disabled. |
| 890 | 2497 | if (isset($custom_strings[$langcode][$string])) { |
| 891 | 0 | $string = $custom_strings[$langcode][$string]; |
| 892 | 0 | } |
| 893 | | // Translate with locale module if enabled. |
| 894 | 2497 | elseif (function_exists('locale') && $langcode != 'en') { |
| 895 | 2 | $string = locale($string, $langcode); |
| 896 | 2 | } |
| 897 | 2497 | if (empty($args)) { |
| 898 | 2485 | return $string; |
| 899 | 0 | } |
| 900 | | else { |
| 901 | | // Transform arguments before inserting them. |
| 902 | 2188 | foreach ($args as $key => $value) { |
| 903 | 2188 | switch ($key[0]) { |
| 904 | 2188 | case '@': |
| 905 | | // Escaped only. |
| 906 | 1820 | $args[$key] = check_plain($value); |
| 907 | 1820 | break; |
| 908 | | |
| 909 | 1576 | case '%': |
| 910 | 1576 | default: |
| 911 | | // Escaped and placeholder. |
| 912 | 672 | $args[$key] = theme('placeholder', $value); |
| 913 | 672 | break; |
| 914 | | |
| 915 | 1194 | case '!': |
| 916 | | // Pass-through. |
| 917 | 1194 | } |
| 918 | 2188 | } |
| 919 | 2188 | return strtr($string, $args); |
| 920 | | } |
| 921 | 0 | } |
| 922 | | |
| 923 | | /** |
| 924 | | * @defgroup validation Input validation |
| 925 | | * @{ |
| 926 | | * Functions to validate user input. |
| 927 | | */ |
| 928 | | |
| 929 | | /** |
| 930 | | * Verify the syntax of the given e-mail address. |
| 931 | | * |
| 932 | | * Empty e-mail addresses are allowed. See RFC 2822 for details. |
| 933 | | * |
| 934 | | * @param $mail |
| 935 | | * A string containing an e-mail address. |
| 936 | | * @return |
| 937 | | * TRUE if the address is in a valid format. |
| 938 | | */ |
| 939 | 2366 | function valid_email_address($mail) { |
| 940 | 37 | return (bool)filter_var($mail, FILTER_VALIDATE_EMAIL); |
| 941 | 0 | } |
| 942 | | |
| 943 | | /** |
| 944 | | * Verify the syntax of the given URL. |
| 945 | | * |
| 946 | | * This function should only be used on actual URLs. It should not be used
for |
| 947 | | * Drupal menu paths, which can contain arbitrary characters. |
| 948 | | * |
| 949 | | * @param $url |
| 950 | | * The URL to verify. |
| 951 | | * @param $absolute |
| 952 | | * Whether the URL is absolute (beginning with a scheme such as "http:"). |
| 953 | | * @return |
| 954 | | * TRUE if the URL is in a valid format. |
| 955 | | */ |
| 956 | 2366 | function valid_url($url, $absolute = FALSE) { |
| 957 | 13 | $allowed_characters = '[a-z0-9\/:_\-_\.\?\$,;~=#&%\+]'; |
| 958 | 13 | if ($absolute) { |
| 959 | 13 | return (bool)preg_match("/^(http|https|ftp):\/\/" . $allowed_characters
. "+$/i", $url); |
| 960 | 0 | } |
| 961 | | else { |
| 962 | 0 | return (bool)preg_match("/^" . $allowed_characters . "+$/i", $url); |
| 963 | | } |
| 964 | 0 | } |
| 965 | | |
| 966 | | /** |
| 967 | | * @} End of "defgroup validation". |
| 968 | | */ |
| 969 | | |
| 970 | | /** |
| 971 | | * Register an event for the current visitor (hostname/IP) to the flood
control mechanism. |
| 972 | | * |
| 973 | | * @param $name |
| 974 | | * The name of an event. |
| 975 | | */ |
| 976 | 2366 | function flood_register_event($name) { |
| 977 | 7 | db_insert('flood') |
| 978 | 7 | ->fields(array( |
| 979 | 7 | 'event' => $name, |
| 980 | 7 | 'hostname' => ip_address(), |
| 981 | 7 | 'timestamp' => REQUEST_TIME, |
| 982 | 7 | )) |
| 983 | 7 | ->execute(); |
| 984 | 7 | } |
| 985 | | |
| 986 | | /** |
| 987 | | * Check if the current visitor (hostname/IP) is allowed to proceed with
the specified event. |
| 988 | | * |
| 989 | | * The user is allowed to proceed if he did not trigger the specified event
more |
| 990 | | * than $threshold times per hour. |
| 991 | | * |
| 992 | | * @param $name |
| 993 | | * The name of the event. |
| 994 | | * @param $number |
| 995 | | * The maximum number of the specified event per hour (per visitor). |
| 996 | | * @return |
| 997 | | * True if the user did not exceed the hourly threshold. False otherwise. |
| 998 | | */ |
| 999 | 2366 | function flood_is_allowed($name, $threshold) { |
| 1000 | 32 | $number = db_query("SELECT COUNT(*) FROM {flood} WHERE event = :event AND
hostname = :hostname AND timestamp > :timestamp", array( |
| 1001 | 32 | ':event' => $name, |
| 1002 | 32 | ':hostname' => ip_address(), |
| 1003 | 32 | ':timestamp' => REQUEST_TIME - 3600)) |
| 1004 | 32 | ->fetchField(); |
| 1005 | 32 | return ($number < $threshold); |
| 1006 | 0 | } |
| 1007 | | |
| 1008 | 2366 | function check_file($filename) { |
| 1009 | 0 | return is_uploaded_file($filename); |
| 1010 | 0 | } |
| 1011 | | |
| 1012 | | /** |
| 1013 | | * Prepare a URL for use in an HTML attribute. Strips harmful protocols. |
| 1014 | | */ |
| 1015 | 2366 | function check_url($uri) { |
| 1016 | 1949 | return filter_xss_bad_protocol($uri, FALSE); |
| 1017 | 0 | } |
| 1018 | | |
| 1019 | | /** |
| 1020 | | * @defgroup format Formatting |
| 1021 | | * @{ |
| 1022 | | * Functions to format numbers, strings, dates, etc. |
| 1023 | | */ |
| 1024 | | |
| 1025 | | /** |
| 1026 | | * Formats an RSS channel. |
| 1027 | | * |
| 1028 | | * Arbitrary elements may be added using the $args associative array. |
| 1029 | | */ |
| 1030 | 2366 | function format_rss_channel($title, $link, $description, $items, $langcode
= NULL, $args = array()) { |
| 1031 | 10 | global $language; |
| 1032 | 10 | $langcode = $langcode ? $langcode : $language->language; |
| 1033 | | |
| 1034 | 10 | $output = "<channel>\n"; |
| 1035 | 10 | $output .= ' <title>' . check_plain($title) . "</title>\n"; |
| 1036 | 10 | $output .= ' <link>' . check_url($link) . "</link>\n"; |
| 1037 | | |
| 1038 | | // The RSS 2.0 "spec" doesn't indicate HTML can be used in the
description. |
| 1039 | | // We strip all HTML tags, but need to prevent double encoding from
properly |
| 1040 | | // escaped source data (such as & becoming &amp;). |
| 1041 | 10 | $output .= ' <description>' .
check_plain(decode_entities(strip_tags($description))) .
"</description>\n"; |
| 1042 | 10 | $output .= ' <language>' . check_plain($langcode) . "</language>\n"; |
| 1043 | 10 | $output .= format_xml_elements($args); |
| 1044 | 10 | $output .= $items; |
| 1045 | 10 | $output .= "</channel>\n"; |
| 1046 | | |
| 1047 | 10 | return $output; |
| 1048 | 0 | } |
| 1049 | | |
| 1050 | | /** |
| 1051 | | * Format a single RSS item. |
| 1052 | | * |
| 1053 | | * Arbitrary elements may be added using the $args associative array. |
| 1054 | | */ |
| 1055 | 2366 | function format_rss_item($title, $link, $description, $args = array()) { |
| 1056 | 0 | $output = "<item>\n"; |
| 1057 | 0 | $output .= ' <title>' . check_plain($title) . "</title>\n"; |
| 1058 | 0 | $output .= ' <link>' . check_url($link) . "</link>\n"; |
| 1059 | 0 | $output .= ' <description>' . check_plain($description) .
"</description>\n"; |
| 1060 | 0 | $output .= format_xml_elements($args); |
| 1061 | 0 | $output .= "</item>\n"; |
| 1062 | | |
| 1063 | 0 | return $output; |
| 1064 | 0 | } |
| 1065 | | |
| 1066 | | /** |
| 1067 | | * Format XML elements. |
| 1068 | | * |
| 1069 | | * @param $array |
| 1070 | | * An array where each item represent an element and is either a: |
| 1071 | | * - (key => value) pair (<key>value</key>) |
| 1072 | | * - Associative array with fields: |
| 1073 | | * - 'key': element name |
| 1074 | | * - 'value': element contents |
| 1075 | | * - 'attributes': associative array of element attributes |
| 1076 | | * |
| 1077 | | * In both cases, 'value' can be a simple string, or it can be another
array |
| 1078 | | * with the same format as $array itself for nesting. |
| 1079 | | */ |
| 1080 | 2366 | function format_xml_elements($array) { |
| 1081 | 10 | $output = ''; |
| 1082 | 10 | foreach ($array as $key => $value) { |
| 1083 | 0 | if (is_numeric($key)) { |
| 1084 | 0 | if ($value['key']) { |
| 1085 | 0 | $output .= ' <' . $value['key']; |
| 1086 | 0 | if (isset($value['attributes']) && is_array($value['attributes']))
{ |
| 1087 | 0 | $output .= drupal_attributes($value['attributes']); |
| 1088 | 0 | } |
| 1089 | | |
| 1090 | 0 | if (isset($value['value']) && $value['value'] != '') { |
| 1091 | 0 | $output .= '>' . (is_array($value['value']) ?
format_xml_elements($value['value']) : check_plain($value['value'])) . '</'
. $value['key'] . ">\n"; |
| 1092 | 0 | } |
| 1093 | | else { |
| 1094 | 0 | $output .= " />\n"; |
| 1095 | | } |
| 1096 | 0 | } |
| 1097 | 0 | } |
| 1098 | | else { |
| 1099 | 0 | $output .= ' <' . $key . '>' . (is_array($value) ?
format_xml_elements($value) : check_plain($value)) . "</$key>\n"; |
| 1100 | | } |
| 1101 | 0 | } |
| 1102 | 10 | return $output; |
| 1103 | 0 | } |
| 1104 | | |
| 1105 | | /** |
| 1106 | | * Format a string containing a count of items. |
| 1107 | | * |
| 1108 | | * This function ensures that the string is pluralized correctly. Since t()
is |
| 1109 | | * called by this function, make sure not to pass already-localized strings
to |
| 1110 | | * it. |
| 1111 | | * |
| 1112 | | * For example: |
| 1113 | | * @code |
| 1114 | | * $output = format_plural($node->comment_count, '1 comment', '@count
comments'); |
| 1115 | | * @endcode |
| 1116 | | * |
| 1117 | | * Example with additional replacements: |
| 1118 | | * @code |
| 1119 | | * $output = format_plural($update_count, |
| 1120 | | * 'Changed the content type of 1 post from %old-type to %new-type.', |
| 1121 | | * 'Changed the content type of @count posts from %old-type to
%new-type.', |
| 1122 | | * array('%old-type' => $info->old_type, '%new-type' =>
$info->new_type))); |
| 1123 | | * @endcode |
| 1124 | | * |
| 1125 | | * @param $count |
| 1126 | | * The item count to display. |
| 1127 | | * @param $singular |
| 1128 | | * The string for the singular case. Please make sure it is clear this is |
| 1129 | | * singular, to ease translation (e.g. use "1 new comment" instead of "1
new"). |
| 1130 | | * Do not use @count in the singular string. |
| 1131 | | * @param $plural |
| 1132 | | * The string for the plural case. Please make sure it is clear this is
plural, |
| 1133 | | * to ease translation. Use @count in place of the item count, as in
"@count |
| 1134 | | * new comments". |
| 1135 | | * @param $args |
| 1136 | | * An associative array of replacements to make after translation.
Incidences |
| 1137 | | * of any key in this array are replaced with the corresponding value. |
| 1138 | | * Based on the first character of the key, the value is escaped and/or
themed: |
| 1139 | | * - !variable: inserted as is |
| 1140 | | * - @variable: escape plain text to HTML (check_plain) |
| 1141 | | * - %variable: escape text and theme as a placeholder for
user-submitted |
| 1142 | | * content (check_plain + theme_placeholder) |
| 1143 | | * Note that you do not need to include @count in this array. |
| 1144 | | * This replacement is done automatically for the plural case. |
| 1145 | | * @param $langcode |
| 1146 | | * Optional language code to translate to a language other than |
| 1147 | | * what is used to display the page. |
| 1148 | | * @return |
| 1149 | | * A translated string. |
| 1150 | | */ |
| 1151 | 2366 | function format_plural($count, $singular, $plural, $args = array(),
$langcode = NULL) { |
| 1152 | 309 | $args['@count'] = $count; |
| 1153 | 309 | if ($count == 1) { |
| 1154 | 128 | return t($singular, $args, $langcode); |
| 1155 | 0 | } |
| 1156 | | |
| 1157 | | // Get the plural index through the gettext formula. |
| 1158 | 300 | $index = (function_exists('locale_get_plural')) ?
locale_get_plural($count, $langcode) : -1; |
| 1159 | | // Backwards compatibility. |
| 1160 | 300 | if ($index < 0) { |
| 1161 | 300 | return t($plural, $args, $langcode); |
| 1162 | 0 | } |
| 1163 | | else { |
| 1164 | | switch ($index) { |
| 1165 | 0 | case "0": |
| 1166 | 0 | return t($singular, $args, $langcode); |
| 1167 | 0 | case "1": |
| 1168 | 0 | return t($plural, $args, $langcode); |
| 1169 | 0 | default: |
| 1170 | 0 | unset($args['@count']); |
| 1171 | 0 | $args['@count[' . $index . ']'] = $count; |
| 1172 | 0 | return t(strtr($plural, array('@count' => '@count[' . $index .
']')), $args, $langcode); |
| 1173 | 0 | } |
| 1174 | | } |
| 1175 | 0 | } |
| 1176 | | |
| 1177 | | /** |
| 1178 | | * Parse a given byte count. |
| 1179 | | * |
| 1180 | | * @param $size |
| 1181 | | * A size expressed as a number of bytes with optional SI size and unit |
| 1182 | | * suffix (e.g. 2, 3K, 5MB, 10G). |
| 1183 | | * @return |
| 1184 | | * An integer representation of the size. |
| 1185 | | */ |
| 1186 | 2366 | function parse_size($size) { |
| 1187 | | $suffixes = array( |
| 1188 | 13 | '' => 1, |
| 1189 | 13 | 'k' => 1024, |
| 1190 | 13 | 'm' => 1048576, // 1024 * 1024 |
| 1191 | 13 | 'g' => 1073741824, // 1024 * 1024 * 1024 |
| 1192 | 13 | ); |
| 1193 | 13 | if (preg_match('/([0-9]+)\s*(k|m|g)?(b?(ytes?)?)/i', $size, $match)) { |
| 1194 | 13 | return $match[1] * $suffixes[drupal_strtolower($match[2])]; |
| 1195 | 0 | } |
| 1196 | 0 | } |
| 1197 | | |
| 1198 | | /** |
| 1199 | | * Generate a string representation for the given byte count. |
| 1200 | | * |
| 1201 | | * @param $size |
| 1202 | | * A size in bytes. |
| 1203 | | * @param $langcode |
| 1204 | | * Optional language code to translate to a language other than what is
used |
| 1205 | | * to display the page. |
| 1206 | | * @return |
| 1207 | | * A translated string representation of the size. |
| 1208 | | */ |
| 1209 | 2366 | function format_size($size, $langcode = NULL) { |
| 1210 | 27 | if ($size < 1000) { |
| 1211 | 3 | return format_plural($size, '1 byte', '@count bytes', array(),
$langcode); |
| 1212 | 0 | } |
| 1213 | | else { |
| 1214 | 26 | $size = $size / 1000; // convert bytes to kilobytes (1000 bytes) |
| 1215 | | $units = array( |
| 1216 | 26 | t('@size KB', array(), $langcode), |
| 1217 | 26 | t('@size MB', array(), $langcode), |
| 1218 | 26 | t('@size GB', array(), $langcode), |
| 1219 | 26 | t('@size TB', array(), $langcode), |
| 1220 | 26 | t('@size PB', array(), $langcode), |
| 1221 | 26 | t('@size EB', array(), $langcode), |
| 1222 | 26 | t('@size ZB', array(), $langcode), |
| 1223 | 26 | t('@size YB', array(), $langcode), |
| 1224 | 26 | ); |
| 1225 | 26 | foreach ($units as $unit) { |
| 1226 | 26 | if (round($size, 2) >= 1000) { |
| 1227 | 18 | $size = $size / 1000; |
| 1228 | 18 | } |
| 1229 | | else { |
| 1230 | 26 | break; |
| 1231 | | } |
| 1232 | 18 | } |
| 1233 | 26 | return str_replace('@size', round($size, 2), $unit); |
| 1234 | | } |
| 1235 | 0 | } |
| 1236 | | |
| 1237 | | /** |
| 1238 | | * Format a time interval with the requested granularity. |
| 1239 | | * |
| 1240 | | * @param $timestamp |
| 1241 | | * The length of the interval in seconds. |
| 1242 | | * @param $granularity |
| 1243 | | * How many different units to display in the string. |
| 1244 | | * @param $langcode |
| 1245 | | * Optional language code to translate to a language other than |
| 1246 | | * what is used to display the page. |
| 1247 | | * @return |
| 1248 | | * A translated string representation of the interval. |
| 1249 | | */ |
| 1250 | 2366 | function format_interval($timestamp, $granularity = 2, $langcode = NULL) { |
| 1251 | 279 | $units = array('1 year|@count years' => 31536000, '1 week|@count weeks'
=> 604800, '1 day|@count days' => 86400, '1 hour|@count hours' => 3600, '1
min|@count min' => 60, '1 sec|@count sec' => 1); |
| 1252 | 279 | $output = ''; |
| 1253 | 279 | foreach ($units as $key => $value) { |
| 1254 | 279 | $key = explode('|', $key); |
| 1255 | 279 | if ($timestamp >= $value) { |
| 1256 | 279 | $output .= ($output ? ' ' : '') . format_plural(floor($timestamp /
$value), $key[0], $key[1], array(), $langcode); |
| 1257 | 279 | $timestamp %= $value; |
| 1258 | 279 | $granularity--; |
| 1259 | 279 | } |
| 1260 | | |
| 1261 | 279 | if ($granularity == 0) { |
| 1262 | 104 | break; |
| 1263 | 0 | } |
| 1264 | 279 | } |
| 1265 | 279 | return $output ? $output : t('0 sec', array(), $langcode); |
| 1266 | 0 | } |
| 1267 | | |
| 1268 | | /** |
| 1269 | | * Format a date with the given configured format or a custom format
string. |
| 1270 | | * |
| 1271 | | * Drupal allows administrators to select formatting strings for 'small', |
| 1272 | | * 'medium' and 'large' date formats. This function can handle these
formats, |
| 1273 | | * as well as any custom format. |
| 1274 | | * |
| 1275 | | * @param $timestamp |
| 1276 | | * The exact date to format, as a UNIX timestamp. |
| 1277 | | * @param $type |
| 1278 | | * The format to use. Can be "small", "medium" or "large" for the
preconfigured |
| 1279 | | * date formats. If "custom" is specified, then $format is required as
well. |
| 1280 | | * @param $format |
| 1281 | | * A PHP date format string as required by date(). A backslash should be
used |
| 1282 | | * before a character to avoid interpreting the character as part of a
date |
| 1283 | | * format. |
| 1284 | | * @param $timezone |
| 1285 | | * Time zone offset in seconds; if omitted, the user's time zone is used. |
| 1286 | | * @param $langcode |
| 1287 | | * Optional language code to translate to a language other than what is
used |
| 1288 | | * to display the page. |
| 1289 | | * @return |
| 1290 | | * A translated date string in the requested format. |
| 1291 | | */ |
| 1292 | 2366 | function format_date($timestamp, $type = 'medium', $format = '', $timezone
= NULL, $langcode = NULL) { |
| 1293 | 403 | if (!isset($timezone)) { |
| 1294 | 385 | global $user; |
| 1295 | 385 | if (variable_get('configurable_timezones', 1) && $user->uid &&
strlen($user->timezone)) { |
| 1296 | 0 | $timezone = $user->timezone; |
| 1297 | 0 | } |
| 1298 | | else { |
| 1299 | 385 | $timezone = variable_get('date_default_timezone', 0); |
| 1300 | | } |
| 1301 | 385 | } |
| 1302 | | |
| 1303 | 403 | $timestamp += $timezone; |
| 1304 | | |
| 1305 | | switch ($type) { |
| 1306 | 403 | case 'small': |
| 1307 | 28 | $format = variable_get('date_format_short', 'm/d/Y - H:i'); |
| 1308 | 28 | break; |
| 1309 | 375 | case 'large': |
| 1310 | 0 | $format = variable_get('date_format_long', 'l, F j, Y - H:i'); |
| 1311 | 0 | break; |
| 1312 | 375 | case 'custom': |
| 1313 | | // No change to format. |
| 1314 | 109 | break; |
| 1315 | 269 | case 'medium': |
| 1316 | 269 | default: |
| 1317 | 269 | $format = variable_get('date_format_medium', 'D, m/d/Y - H:i'); |
| 1318 | 269 | } |
| 1319 | | |
| 1320 | 403 | $max = strlen($format); |
| 1321 | 403 | $date = ''; |
| 1322 | 403 | for ($i = 0; $i < $max; $i++) { |
| 1323 | 403 | $c = $format[$i]; |
| 1324 | 403 | if (strpos('AaDlM', $c) !== FALSE) { |
| 1325 | 289 | $date .= t(gmdate($c, $timestamp), array(), $langcode); |
| 1326 | 289 | } |
| 1327 | 400 | elseif ($c == 'F') { |
| 1328 | | // Special treatment for long month names: May is both an
abbreviation |
| 1329 | | // and a full month name in English, but other languages have |
| 1330 | | // different abbreviations. |
| 1331 | 15 | $date .= trim(t('!long-month-name ' . gmdate($c, $timestamp),
array('!long-month-name' => ''), $langcode)); |
| 1332 | 15 | } |
| 1333 | 400 | elseif (strpos('BdgGhHiIjLmnsStTUwWYyz', $c) !== FALSE) { |
| 1334 | 400 | $date .= gmdate($c, $timestamp); |
| 1335 | 400 | } |
| 1336 | 398 | elseif ($c == 'r') { |
| 1337 | 0 | $date .= format_date($timestamp - $timezone, 'custom', 'D, d M Y
H:i:s O', $timezone, $langcode); |
| 1338 | 0 | } |
| 1339 | 398 | elseif ($c == 'O') { |
| 1340 | 104 | $date .= sprintf('%s%02d%02d', ($timezone < 0 ? '-' : '+'),
abs($timezone / 3600), abs($timezone % 3600) / 60); |
| 1341 | 104 | } |
| 1342 | 398 | elseif ($c == 'Z') { |
| 1343 | 0 | $date .= $timezone; |
| 1344 | 0 | } |
| 1345 | 398 | elseif ($c == '\\') { |
| 1346 | 0 | $date .= $format[++$i]; |
| 1347 | 0 | } |
| 1348 | | else { |
| 1349 | 398 | $date .= $c; |
| 1350 | | } |
| 1351 | 403 | } |
| 1352 | | |
| 1353 | 403 | return $date; |
| 1354 | 0 | } |
| 1355 | | |
| 1356 | | /** |
| 1357 | | * @} End of "defgroup format". |
| 1358 | | */ |
| 1359 | | |
| 1360 | | /** |
| 1361 | | * Generate a URL from a Drupal menu path. Will also pass-through existing
URLs. |
| 1362 | | * |
| 1363 | | * @param $path |
| 1364 | | * The Drupal path being linked to, such as "admin/content/node", or an |
| 1365 | | * existing URL like "http://drupal.org/". The special path |
| 1366 | | * '<front>' may also be given and will generate the site's base URL. |
| 1367 | | * @param $options |
| 1368 | | * An associative array of additional options, with the following keys: |
| 1369 | | * - 'query' |
| 1370 | | * A query string to append to the link, or an array of query
key/value |
| 1371 | | * properties. |
| 1372 | | * - 'fragment' |
| 1373 | | * A fragment identifier (or named anchor) to append to the link. |
| 1374 | | * Do not include the '#' character. |
| 1375 | | * - 'absolute' (default FALSE) |
| 1376 | | * Whether to force the output to be an absolute link (beginning with |
| 1377 | | * http:). Useful for links that will be displayed outside the site,
such |
| 1378 | | * as in an RSS feed. |
| 1379 | | * - 'alias' (default FALSE) |
| 1380 | | * Whether the given path is an alias already. |
| 1381 | | * - 'external' |
| 1382 | | * Whether the given path is an external URL. |
| 1383 | | * - 'language' |
| 1384 | | * An optional language object. Used to build the URL to link to and |
| 1385 | | * look up the proper alias for the link. |
| 1386 | | * - 'base_url' |
| 1387 | | * Only used internally, to modify the base URL when a language
dependent |
| 1388 | | * URL requires so. |
| 1389 | | * - 'prefix' |
| 1390 | | * Only used internally, to modify the path when a language dependent
URL |
| 1391 | | * requires so. |
| 1392 | | * @return |
| 1393 | | * A string containing a URL to the given path. |
| 1394 | | * |
| 1395 | | * When creating links in modules, consider whether l() could be a better |
| 1396 | | * alternative than url(). |
| 1397 | | */ |
| 1398 | 2366 | function url($path = NULL, array $options = array()) { |
| 1399 | | // Merge in defaults. |
| 1400 | | $options += array( |
| 1401 | 2415 | 'fragment' => '', |
| 1402 | 2415 | 'query' => '', |
| 1403 | 2415 | 'absolute' => FALSE, |
| 1404 | 2415 | 'alias' => FALSE, |
| 1405 | | 'prefix' => '' |
| 1406 | 2415 | ); |
| 1407 | 2415 | if (!isset($options['external'])) { |
| 1408 | | // Return an external link if $path contains an allowed absolute URL. |
| 1409 | | // Only call the slow filter_xss_bad_protocol if $path contains a ':'
before |
| 1410 | | // any / ? or #. |
| 1411 | 2414 | $colonpos = strpos($path, ':'); |
| 1412 | 2414 | $options['external'] = ($colonpos !== FALSE && !preg_match('![/?#]!',
substr($path, 0, $colonpos)) && filter_xss_bad_protocol($path, FALSE) ==
check_plain($path)); |
| 1413 | 2414 | } |
| 1414 | | |
| 1415 | | // May need language dependent rewriting if language.inc is present. |
| 1416 | 2415 | if (function_exists('language_url_rewrite')) { |
| 1417 | 73 | language_url_rewrite($path, $options); |
| 1418 | 73 | } |
| 1419 | 2415 | if ($options['fragment']) { |
| 1420 | 224 | $options['fragment'] = '#' . $options['fragment']; |
| 1421 | 224 | } |
| 1422 | 2415 | if (is_array($options['query'])) { |
| 1423 | 8 | $options['query'] = drupal_query_string_encode($options['query']); |
| 1424 | 8 | } |
| 1425 | | |
| 1426 | 2415 | if ($options['external']) { |
| 1427 | | // Split off the fragment. |
| 1428 | 1760 | if (strpos($path, '#') !== FALSE) { |
| 1429 | 0 | list($path, $old_fragment) = explode('#', $path, 2); |
| 1430 | 0 | if (isset($old_fragment) && !$options['fragment']) { |
| 1431 | 0 | $options['fragment'] = '#' . $old_fragment; |
| 1432 | 0 | } |
| 1433 | 0 | } |
| 1434 | | // Append the query. |
| 1435 | 1760 | if ($options['query']) { |
| 1436 | 4 | $path .= (strpos($path, '?') !== FALSE ? '&' : '?') .
$options['query']; |
| 1437 | 4 | } |
| 1438 | | // Reassemble. |
| 1439 | 1760 | return $path . $options['fragment']; |
| 1440 | 0 | } |
| 1441 | | |
| 1442 | 2413 | global $base_url; |
| 1443 | 2413 | static $script; |
| 1444 | 2413 | static $clean_url; |
| 1445 | | |
| 1446 | 2413 | if (!isset($script)) { |
| 1447 | | // On some web servers, such as IIS, we can't omit "index.php". So, we |
| 1448 | | // generate "index.php?q=foo" instead of "?q=foo" on anything that is
not |
| 1449 | | // Apache. |
| 1450 | 2413 | $script = (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === FALSE) ?
'index.php' : ''; |
| 1451 | 2413 | } |
| 1452 | | |
| 1453 | | // Cache the clean_url variable to improve performance. |
| 1454 | 2413 | if (!isset($clean_url)) { |
| 1455 | 2413 | $clean_url = (bool)variable_get('clean_url', '0'); |
| 1456 | 2413 | } |
| 1457 | | |
| 1458 | 2413 | if (!isset($options['base_url'])) { |
| 1459 | | // The base_url might be rewritten from the language rewrite in domain
mode. |
| 1460 | 2413 | $options['base_url'] = $base_url; |
| 1461 | 2413 | } |
| 1462 | | |
| 1463 | | // Preserve the original path before aliasing. |
| 1464 | 2413 | $original_path = $path; |
| 1465 | | |
| 1466 | | // The special path '<front>' links to the default front page. |
| 1467 | 2413 | if ($path == '<front>') { |
| 1468 | 1338 | $path = ''; |
| 1469 | 1338 | } |
| 1470 | 2413 | elseif (!empty($path) && !$options['alias']) { |
| 1471 | 2317 | $path = drupal_get_path_alias($path, isset($options['language']) ?
$options['language']->language : ''); |
| 1472 | 2317 | } |
| 1473 | | |
| 1474 | 2413 | if (function_exists('custom_url_rewrite_outbound')) { |
| 1475 | | // Modules may alter outbound links by reference. |
| 1476 | 0 | custom_url_rewrite_outbound($path, $options, $original_path); |
| 1477 | 0 | } |
| 1478 | | |
| 1479 | 2413 | $base = $options['absolute'] ? $options['base_url'] . '/' : base_path(); |
| 1480 | 2413 | $prefix = empty($path) ? rtrim($options['prefix'], '/') :
$options['prefix']; |
| 1481 | 2413 | $path = drupal_urlencode($prefix . $path); |
| 1482 | | |
| 1483 | 2413 | if ($clean_url) { |
| 1484 | | // With Clean URLs. |
| 1485 | 2413 | if ($options['query']) { |
| 1486 | 383 | return $base . $path . '?' . $options['query'] .
$options['fragment']; |
| 1487 | 0 | } |
| 1488 | | else { |
| 1489 | 2411 | return $base . $path . $options['fragment']; |
| 1490 | | } |
| 1491 | 0 | } |
| 1492 | | else { |
| 1493 | | // Without Clean URLs. |
| 1494 | 0 | $variables = array(); |
| 1495 | 0 | if (!empty($path)) { |
| 1496 | 0 | $variables[] = 'q=' . $path; |
| 1497 | 0 | } |
| 1498 | 0 | if (!empty($options['query'])) { |
| 1499 | 0 | $variables[] = $options['query']; |
| 1500 | 0 | } |
| 1501 | 0 | if ($query = join('&', $variables)) { |
| 1502 | 0 | return $base . $script . '?' . $query . $options['fragment']; |
| 1503 | 0 | } |
| 1504 | | else { |
| 1505 | 0 | return $base . $options['fragment']; |
| 1506 | | } |
| 1507 | | } |
| 1508 | 0 | } |
| 1509 | | |
| 1510 | | /** |
| 1511 | | * Format an attribute string to insert in a tag. |
| 1512 | | * |
| 1513 | | * @param $attributes |
| 1514 | | * An associative array of HTML attributes. |
| 1515 | | * @return |
| 1516 | | * An HTML string ready for insertion in a tag. |
| 1517 | | */ |
| 1518 | 2366 | function drupal_attributes($attributes = array()) { |
| 1519 | 1951 | if (is_array($attributes)) { |
| 1520 | 1951 | $t = ''; |
| 1521 | 1951 | foreach ($attributes as $key => $value) { |
| 1522 | 1732 | $t .= " $key=" . '"' . check_plain($value) . '"'; |
| 1523 | 1732 | } |
| 1524 | 1951 | return $t; |
| 1525 | 0 | } |
| 1526 | 1740 | } |
| 1527 | | |
| 1528 | | /** |
| 1529 | | * Format an internal Drupal link. |
| 1530 | | * |
| 1531 | | * This function correctly handles aliased paths, and allows themes to
highlight |
| 1532 | | * links to the current page correctly, so all internal links output by
modules |
| 1533 | | * should be generated by this function if possible. |
| 1534 | | * |
| 1535 | | * @param $text |
| 1536 | | * The text to be enclosed with the anchor tag. |
| 1537 | | * @param $path |
| 1538 | | * The Drupal path being linked to, such as "admin/content/node". Can be
an |
| 1539 | | * external or internal URL. |
| 1540 | | * - If you provide the full URL, it will be considered an external
URL. |
| 1541 | | * - If you provide only the path (e.g. "admin/content/node"), it is |
| 1542 | | * considered an internal link. In this case, it must be a system URL |
| 1543 | | * as the url() function will generate the alias. |
| 1544 | | * - If you provide '<front>', it generates a link to the site's |
| 1545 | | * base URL (again via the url() function). |
| 1546 | | * - If you provide a path, and 'alias' is set to TRUE (see below), it
is |
| 1547 | | * used as is. |
| 1548 | | * @param $options |
| 1549 | | * An associative array of additional options, with the following keys: |
| 1550 | | * - 'attributes' |
| 1551 | | * An associative array of HTML attributes to apply to the anchor
tag. |
| 1552 | | * - 'query' |
| 1553 | | * A query string to append to the link, or an array of query
key/value |
| 1554 | | * properties. |
| 1555 | | * - 'fragment' |
| 1556 | | * A fragment identifier (named anchor) to append to the link. |
| 1557 | | * Do not include the '#' character. |
| 1558 | | * - 'absolute' (default FALSE) |
| 1559 | | * Whether to force the output to be an absolute link (beginning with |
| 1560 | | * http:). Useful for links that will be displayed outside the site,
such |
| 1561 | | * as in an RSS feed. |
| 1562 | | * - 'html' (default FALSE) |
| 1563 | | * Whether the title is HTML, or just plain-text. For example for
making |
| 1564 | | * an image a link, this must be set to TRUE, or else you will see
the |
| 1565 | | * escaped HTML. |
| 1566 | | * - 'alias' (default FALSE) |
| 1567 | | * Whether the given path is an alias already. |
| 1568 | | * @return |
| 1569 | | * an HTML string containing a link to the given path. |
| 1570 | | */ |
| 1571 | 2366 | function l($text, $path, array $options = array()) { |
| 1572 | | // Merge in defaults. |
| 1573 | | $options += array( |
| 1574 | 1934 | 'attributes' => array(), |
| 1575 | 1934 | 'html' => FALSE, |
| 1576 | 0 | ); |
| 1577 | | |
| 1578 | | // Append active class. |
| 1579 | 1934 | if ($path == $_GET['q'] || ($path == '<front>' &&
drupal_is_front_page())) { |
| 1580 | 1404 | if (isset($options['attributes']['class'])) { |
| 1581 | 0 | $options['attributes']['class'] .= ' active'; |
| 1582 | 0 | } |
| 1583 | | else { |
| 1584 | 1404 | $options['attributes']['class'] = 'active'; |
| 1585 | | } |
| 1586 | 1404 | } |
| 1587 | | |
| 1588 | | // Remove all HTML and PHP tags from a tooltip. For best performance, we
act only |
| 1589 | | // if a quick strpos() pre-check gave a suspicion (because strip_tags()
is expensive). |
| 1590 | 1934 | if (isset($options['attributes']['title']) &&
strpos($options['attributes']['title'], '<') !== FALSE) { |
| 1591 | 72 | $options['attributes']['title'] =
strip_tags($options['attributes']['title']); |
| 1592 | 72 | } |
| 1593 | | |
| 1594 | 1934 | return '<a href="' . check_url(url($path, $options)) . '"' .
drupal_attributes($options['attributes']) . '>' . ($options['html'] ? $text
: check_plain($text)) . '</a>'; |
| 1595 | 0 | } |
| 1596 | | |
| 1597 | | /** |
| 1598 | | * Perform end-of-request tasks. |
| 1599 | | * |
| 1600 | | * This function sets the page cache if appropriate, and allows modules to |
| 1601 | | * react to the closing of the page by calling hook_exit(). |
| 1602 | | */ |
| 1603 | 2366 | function drupal_page_footer() { |
| 1604 | | |
| 1605 | 1756 | if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED) { |
| 1606 | 1 | page_set_cache(); |
| 1607 | 1 | } |
| 1608 | | |
| 1609 | 1756 | module_invoke_all('exit'); |
| 1610 | | |
| 1611 | 1756 | module_implements(MODULE_IMPLEMENTS_WRITE_CACHE); |
| 1612 | 1756 | registry_cache_path_files(); |
| 1613 | 1756 | } |
| 1614 | | |
| 1615 | | /** |
| 1616 | | * Form an associative array from a linear array. |
| 1617 | | * |
| 1618 | | * This function walks through the provided array and constructs an
associative |
| 1619 | | * array out of it. The keys of the resulting array will be the values of
the |
| 1620 | | * input array. The values will be the same as the keys unless a function
is |
| 1621 | | * specified, in which case the output of the function is used for the
values |
| 1622 | | * instead. |
| 1623 | | * |
| 1624 | | * @param $array |
| 1625 | | * A linear array. |
| 1626 | | * @param $function |
| 1627 | | * A name of a function to apply to all values before output. |
| 1628 | | * @result |
| 1629 | | * An associative array. |
| 1630 | | */ |
| 1631 | 2366 | function drupal_map_assoc($array, $function = NULL) { |
| 1632 | 188 | if (!isset($function)) { |
| 1633 | 181 | $result = array(); |
| 1634 | 181 | foreach ($array as $value) { |
| 1635 | 178 | $result[$value] = $value; |
| 1636 | 178 | } |
| 1637 | 181 | return $result; |
| 1638 | 0 | } |
| 1639 | 61 | elseif (function_exists($function)) { |
| 1640 | 61 | $result = array(); |
| 1641 | 61 | foreach ($array as $value) { |
| 1642 | 61 | $result[$value] = $function($value); |
| 1643 | 61 | } |
| 1644 | 61 | return $result; |
| 1645 | 0 | } |
| 1646 | 0 | } |
| 1647 | | |
| 1648 | | /** |
| 1649 | | * Evaluate a string of PHP code. |
| 1650 | | * |
| 1651 | | * This is a wrapper around PHP's eval(). It uses output buffering to
capture both |
| 1652 | | * returned and printed text. Unlike eval(), we require code to be
surrounded by |
| 1653 | | * <?php ?> tags; in other words, we evaluate the code as if it were a
stand-alone |
| 1654 | | * PHP file. |
| 1655 | | * |
| 1656 | | * Using this wrapper also ensures that the PHP code which is evaluated can
not |
| 1657 | | * overwrite any variables in the calling code, unlike a regular eval()
call. |
| 1658 | | * |
| 1659 | | * @param $code |
| 1660 | | * The code to evaluate. |
| 1661 | | * @return |
| 1662 | | * A string containing the printed output of the code, followed by the
returned |
| 1663 | | * output of the code. |
| 1664 | | */ |
| 1665 | 2366 | function drupal_eval($code) { |
| 1666 | 1 | global $theme_path, $theme_info, $conf; |
| 1667 | | |
| 1668 | | // Store current theme path. |
| 1669 | 1 | $old_theme_path = $theme_path; |
| 1670 | | |
| 1671 | | // Restore theme_path to the theme, as long as drupal_eval() executes, |
| 1672 | | // so code evaluted will not see the caller module as the current theme. |
| 1673 | | // If theme info is not initialized get the path from theme_default. |
| 1674 | 1 | if (!isset($theme_info)) { |
| 1675 | 1 | $theme_path = drupal_get_path('theme', $conf['theme_default']); |
| 1676 | 1 | } |
| 1677 | | else { |
| 1678 | 0 | $theme_path = dirname($theme_info->filename); |
| 1679 | | } |
| 1680 | | |
| 1681 | 1 | ob_start(); |
| 1682 | 1 | print eval('?>' . $code); |
| 1683 | 1 | $output = ob_get_contents(); |
| 1684 | 1 | ob_end_clean(); |
| 1685 | | |
| 1686 | | // Recover original theme path. |
| 1687 | 1 | $theme_path = $old_theme_path; |
| 1688 | | |
| 1689 | 1 | return $output; |
| 1690 | 0 | } |
| 1691 | | |
| 1692 | | /** |
| 1693 | | * Returns the path to a system item (module, theme, etc.). |
| 1694 | | * |
| 1695 | | * @param $type |
| 1696 | | * The type of the item (i.e. theme, theme_engine, module). |
| 1697 | | * @param $name |
| 1698 | | * The name of the item for which the path is requested. |
| 1699 | | * |
| 1700 | | * @return |
| 1701 | | * The path to the requested item. |
| 1702 | | */ |
| 1703 | 2366 | function drupal_get_path($type, $name) { |
| 1704 | 2496 | return dirname(drupal_get_filename($type, $name)); |
| 1705 | 0 | } |
| 1706 | | |
| 1707 | | /** |
| 1708 | | * Returns the base URL path of the Drupal installation. |
| 1709 | | * At the very least, this will always default to /. |
| 1710 | | */ |
| 1711 | 2366 | function base_path() { |
| 1712 | 2039 | return $GLOBALS['base_path']; |
| 1713 | 0 | } |
| 1714 | | |
| 1715 | | /** |
| 1716 | | * Add a <link> tag to the page's HEAD. |
| 1717 | | * |
| 1718 | | * This function can be called as long the HTML header hasn't been sent. |
| 1719 | | */ |
| 1720 | 2366 | function drupal_add_link($attributes) { |
| 1721 | 64 | drupal_set_html_head('<link' . drupal_attributes($attributes) . " />\n"); |
| 1722 | 64 | } |
| 1723 | | |
| 1724 | | /** |
| 1725 | | * Adds a CSS file to the stylesheet queue. |
| 1726 | | * |
| 1727 | | * @param $path |
| 1728 | | * (optional) The path to the CSS file relative to the base_path(), e.g., |
| 1729 | | * /modules/devel/devel.css. |
| 1730 | | * |
| 1731 | | * Modules should always prefix the names of their CSS files with the
module |
| 1732 | | * name, for example: system-menus.css rather than simply menus.css.
Themes |
| 1733 | | * can override module-supplied CSS files based on their filenames, and
this |
| 1734 | | * prefixing helps prevent confusing name collisions for theme
developers. |
| 1735 | | * See drupal_get_css where the overrides are performed. |
| 1736 | | * |
| 1737 | | * If the direction of the current language is right-to-left (Hebrew, |
| 1738 | | * Arabic, etc.), the function will also look for an RTL CSS file and
append |
| 1739 | | * it to the list. The name of this file should have an '-rtl.css'
suffix. |
| 1740 | | * For example a CSS file called 'name.css' will have a 'name-rtl.css' |
| 1741 | | * file added to the list, if exists in the same directory. This CSS file |
| 1742 | | * should contain overrides for properties which should be reversed or |
| 1743 | | * otherwise different in a right-to-left display. |
| 1744 | | * @param $options |
| 1745 | | * (optional) A string defining the type of CSS that is being added in
the |
| 1746 | | * $path parameter ('module' or 'theme'), or an associative array of |
| 1747 | | * additional options, with the following keys: |
| 1748 | | * - 'type' |
| 1749 | | * The type of stylesheet that is being added. Types are: module or |
| 1750 | | * theme. Defaults to 'module'. |
| 1751 | | * - 'media' |
| 1752 | | * The media type for the stylesheet, e.g., all, print, screen.
Defaults |
| 1753 | | * to 'all'. |
| 1754 | | * - 'preprocess': |
| 1755 | | * Allow this CSS file to be aggregated and compressed if the
Optimize |
| 1756 | | * CSS feature has been turned on under the performance section.
Defaults |
| 1757 | | * to TRUE. |
| 1758 | | * |
| 1759 | | * What does this actually mean? |
| 1760 | | * CSS preprocessing is the process of aggregating a bunch of
separate CSS |
| 1761 | | * files into one file that is then compressed by removing all
extraneous |
| 1762 | | * white space. |
| 1763 | | * |
| 1764 | | * The reason for merging the CSS files is outlined quite thoroughly
here: |
| 1765 | | * http://www.die.net/musings/page_load_time/ |
| 1766 | | * "Load fewer external objects. Due to request overhead, one bigger
file |
| 1767 | | * just loads faster than two smaller ones half its size." |
| 1768 | | * |
| 1769 | | * However, you should *not* preprocess every file as this can lead
to |
| 1770 | | * redundant caches. You should set $preprocess = FALSE when your
styles |
| 1771 | | * are only used rarely on the site. This could be a special admin
page, |
| 1772 | | * the homepage, or a handful of pages that does not represent the |
| 1773 | | * majority of the pages on your site. |
| 1774 | | * |
| 1775 | | * Typical candidates for caching are for example styles for nodes
across |
| 1776 | | * the site, or used in the theme. |
| 1777 | | * @param $reset |
| 1778 | | * (optional) Resets the currently loaded cascading stylesheets. |
| 1779 | | * @return |
| 1780 | | * An array of CSS files. |
| 1781 | | */ |
| 1782 | 2366 | function drupal_add_css($path = NULL, $options = NULL, $reset = FALSE) { |
| 1783 | 2496 | static $css = array(); |
| 1784 | 2496 | global $language; |
| 1785 | | |
| 1786 | | // Request made to reset the CSS added so far. |
| 1787 | 2496 | if ($reset) { |
| 1788 | 1 | $css = array(); |
| 1789 | 1 | } |
| 1790 | | |
| 1791 | | // Create an array of CSS files for each media type first, since each
type needs to be served |
| 1792 | | // to the browser differently. |
| 1793 | 2496 | if (isset($path)) { |
| 1794 | | // Construct the options, taking the defaults into consideration. |
| 1795 | 2496 | if (isset($options)) { |
| 1796 | 2163 | if (!is_array($options)) { |
| 1797 | 0 | $options = array('type' => $options); |
| 1798 | 0 | } |
| 1799 | 2163 | } |
| 1800 | | else { |
| 1801 | 2367 | $options = array(); |
| 1802 | | } |
| 1803 | | $options += array( |
| 1804 | 2496 | 'type' => 'module', |
| 1805 | 2496 | 'media' => 'all', |
| 1806 | | 'preprocess' => TRUE |
| 1807 | 2496 | ); |
| 1808 | 2496 | $media = $options['media']; |
| 1809 | 2496 | $type = $options['type']; |
| 1810 | | |
| 1811 | | // This check is necessary to ensure proper cascading of styles and is
faster than an asort(). |
| 1812 | 2496 | if (!isset($css[$media])) { |
| 1813 | 2496 | $css[$media] = array('module' => array(), 'theme' => array()); |
| 1814 | 2496 | } |
| 1815 | 2496 | $css[$media][$type][$path] = $options['preprocess']; |
| 1816 | | |
| 1817 | | // If the current language is RTL, add the CSS file with RTL overrides. |
| 1818 | 2496 | if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) { |
| 1819 | 0 | $rtl_path = str_replace('.css', '-rtl.css', $path); |
| 1820 | 0 | if (file_exists($rtl_path)) { |
| 1821 | 0 | $css[$media][$type][$rtl_path] = $options['preprocess']; |
| 1822 | 0 | } |
| 1823 | 0 | } |
| 1824 | 2496 | } |
| 1825 | | |
| 1826 | 2496 | return $css; |
| 1827 | 0 | } |
| 1828 | | |
| 1829 | | /** |
| 1830 | | * Returns a themed representation of all stylesheets that should be
attached to the page. |
| 1831 | | * |
| 1832 | | * It loads the CSS in order, with 'module' first, then 'theme' afterwards. |
| 1833 | | * This ensures proper cascading of styles so themes can easily override |
| 1834 | | * module styles through CSS selectors. |
| 1835 | | * |
| 1836 | | * Themes may replace module-defined CSS files by adding a stylesheet with
the |
| 1837 | | * same filename. For example, themes/garland/system-menus.css would
replace |
| 1838 | | * modules/system/system-menus.css. This allows themes to override complete |
| 1839 | | * CSS files, rather than specific selectors, when necessary. |
| 1840 | | * |
| 1841 | | * If the original CSS file is being overridden by a theme, the theme is |
| 1842 | | * responsible for supplying an accompanying RTL CSS file to replace the |
| 1843 | | * module's. |
| 1844 | | * |
| 1845 | | * @param $css |
| 1846 | | * (optional) An array of CSS files. If no array is provided, the default |
| 1847 | | * stylesheets array is used instead. |
| 1848 | | * @return |
| 1849 | | * A string of XHTML CSS tags. |
| 1850 | | */ |
| 1851 | 2366 | function drupal_get_css($css = NULL) { |
| 1852 | 1741 | $output = ''; |
| 1853 | 1741 | if (!isset($css)) { |
| 1854 | 1741 | $css = drupal_add_css(); |
| 1855 | 1741 | } |
| 1856 | 1741 | $no_module_preprocess = ''; |
| 1857 | 1741 | $no_theme_preprocess = ''; |
| 1858 | | |
| 1859 | 1741 | $preprocess_css = (variable_get('preprocess_css', FALSE) &&
(!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update')); |
| 1860 | 1741 | $directory = file_directory_path(); |
| 1861 | 1741 | $is_writable = is_dir($directory) && is_writable($directory) &&
(variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) ==
FILE_DOWNLOADS_PUBLIC); |
| 1862 | | |
| 1863 | | // A dummy query-string is added to filenames, to gain control over |
| 1864 | | // browser-caching. The string changes on every update or full cache |
| 1865 | | // flush, forcing browsers to load a new copy of the files, as the |
| 1866 | | // URL changed. |
| 1867 | 1741 | $query_string = '?' . substr(variable_get('css_js_query_string', '0'), 0,
1); |
| 1868 | | |
| 1869 | 1741 | foreach ($css as $media => $types) { |
| 1870 | | // If CSS preprocessing is off, we still need to output the styles. |
| 1871 | | // Additionally, go through any remaining styles if CSS preprocessing
is on and output the non-cached ones. |
| 1872 | 1741 | foreach ($types as $type => $files) { |
| 1873 | 1741 | if ($type == 'module') { |
| 1874 | | // Setup theme overrides for module styles. |
| 1875 | 1741 | $theme_styles = array(); |
| 1876 | 1741 | foreach (array_keys($css[$media]['theme']) as $theme_style) { |
| 1877 | 1740 | $theme_styles[] = basename($theme_style); |
| 1878 | 1740 | } |
| 1879 | 1741 | } |
| 1880 | 1741 | foreach ($types[$type] as $file => $preprocess) { |
| 1881 | | // If the theme supplies its own style using the name of the module
style, skip its inclusion. |
| 1882 | | // This includes any RTL styles associated with its main LTR
counterpart. |
| 1883 | 1741 | if ($type == 'module' && in_array(str_replace('-rtl.css', '.css',
basename($file)), $theme_styles)) { |
| 1884 | | // Unset the file to prevent its inclusion when CSS aggregation
is enabled. |
| 1885 | 0 | unset($types[$type][$file]); |
| 1886 | 0 | continue; |
| 1887 | 0 | } |
| 1888 | | // Only include the stylesheet if it exists. |
| 1889 | 1741 | if (file_exists($file)) { |
| 1890 | 1741 | if (!$preprocess || !($is_writable && $preprocess_css)) { |
| 1891 | | // If a CSS file is not to be preprocessed and it's a module
CSS file, it needs to *always* appear at the *top*, |
| 1892 | | // regardless of whether preprocessing is on or off. |
| 1893 | 1741 | if (!$preprocess && $type == 'module') { |
| 1894 | 70 | $no_module_preprocess .= '<link type="text/css"
rel="stylesheet" media="' . $media . '" href="' . base_path() . $file .
$query_string . '" />' . "\n"; |
| 1895 | 70 | } |
| 1896 | | // If a CSS file is not to be preprocessed and it's a theme CSS
file, it needs to *always* appear at the *bottom*, |
| 1897 | | // regardless of whether preprocessing is on or off. |
| 1898 | 1741 | elseif (!$preprocess && $type == 'theme') { |
| 1899 | 0 | $no_theme_preprocess .= '<link type="text/css"
rel="stylesheet" media="' . $media . '" href="' . base_path() . $file .
$query_string . '" />' . "\n"; |
| 1900 | 0 | } |
| 1901 | | else { |
| 1902 | 1741 | $output .= '<link type="text/css" rel="stylesheet" media="' .
$media . '" href="' . base_path() . $file . $query_string . '" />' . "\n"; |
| 1903 | | } |
| 1904 | 1741 | } |
| 1905 | 1741 | } |
| 1906 | 1741 | } |
| 1907 | 1741 | } |
| 1908 | | |
| 1909 | 1741 | if ($is_writable && $preprocess_css) { |
| 1910 | 0 | $filename = md5(serialize($types) . $query_string) . '.css'; |
| 1911 | 0 | $preprocess_file = drupal_build_css_cache($types, $filename); |
| 1912 | 0 | $output .= '<link type="text/css" rel="stylesheet" media="' . $media
. '" href="' . base_path() . $preprocess_file . '" />' . "\n"; |
| 1913 | 0 | } |
| 1914 | 1741 | } |
| 1915 | | |
| 1916 | 1741 | return $no_module_preprocess . $output . $no_theme_preprocess; |
| 1917 | 0 | } |
| 1918 | | |
| 1919 | | /** |
| 1920 | | * Aggregate and optimize CSS files, putting them in the files directory. |
| 1921 | | * |
| 1922 | | * @param $types |
| 1923 | | * An array of types of CSS files (e.g., screen, print) to aggregate and |
| 1924 | | * compress into one file. |
| 1925 | | * @param $filename |
| 1926 | | * The name of the aggregate CSS file. |
| 1927 | | * @return |
| 1928 | | * The name of the CSS file. |
| 1929 | | */ |
| 1930 | 2366 | function drupal_build_css_cache($types, $filename) { |
| 1931 | 0 | $data = ''; |
| 1932 | | |
| 1933 | | // Create the css/ within the files folder. |
| 1934 | 0 | $csspath = file_create_path('css'); |
| 1935 | 0 | file_check_directory($csspath, FILE_CREATE_DIRECTORY); |
| 1936 | | |
| 1937 | 0 | if (!file_exists($csspath . '/' . $filename)) { |
| 1938 | | // Build aggregate CSS file. |
| 1939 | 0 | foreach ($types as $type) { |
| 1940 | 0 | foreach ($type as $file => $cache) { |
| 1941 | 0 | if ($cache) { |
| 1942 | 0 | $contents = drupal_load_stylesheet($file, TRUE); |
| 1943 | | // Return the path to where this CSS file originated from. |
| 1944 | 0 | $base = base_path() . dirname($file) . '/'; |
| 1945 | 0 | _drupal_build_css_path(NULL, $base); |
| 1946 | | // Prefix all paths within this CSS file, ignoring external and
absolute paths. |
| 1947 | 0 | $data .=
preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i',
'_drupal_build_css_path', $contents); |
| 1948 | 0 | } |
| 1949 | 0 | } |
| 1950 | 0 | } |
| 1951 | | |
| 1952 | | // Per the W3C specification at
http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, |
| 1953 | | // @import rules must proceed any other style, so we move those to the
top. |
| 1954 | 0 | $regexp = '/@import[^;]+;/i'; |
| 1955 | 0 | preg_match_all($regexp, $data, $matches); |
| 1956 | 0 | $data = preg_replace($regexp, '', $data); |
| 1957 | 0 | $data = implode('', $matches[0]) . $data; |
| 1958 | | |
| 1959 | | // Create the CSS file. |
| 1960 | 0 | file_unmanaged_save_data($data, $csspath . '/' . $filename,
FILE_EXISTS_REPLACE); |
| 1961 | 0 | } |
| 1962 | 0 | return $csspath . '/' . $filename; |
| 1963 | 0 | } |
| 1964 | | |
| 1965 | | /** |
| 1966 | | * Helper function for drupal_build_css_cache(). |
| 1967 | | * |
| 1968 | | * This function will prefix all paths within a CSS file. |
| 1969 | | */ |
| 1970 | 2366 | function _drupal_build_css_path($matches, $base = NULL) { |
| 1971 | 0 | static $_base; |
| 1972 | | // Store base path for preg_replace_callback. |
| 1973 | 0 | if (isset($base)) { |
| 1974 | 0 | $_base = $base; |
| 1975 | 0 | } |
| 1976 | | |
| 1977 | | // Prefix with base and remove '../' segments where possible. |
| 1978 | 0 | $path = $_base . $matches[1]; |
| 1979 | 0 | $last = ''; |
| 1980 | 0 | while ($path != $last) { |
| 1981 | 0 | $last = $path; |
| 1982 | 0 | $path = preg_replace('`(^|/)(?!../)([^/]+)/../`', '$1', $path); |
| 1983 | 0 | } |
| 1984 | 0 | return 'url(' . $path . ')'; |
| 1985 | 0 | } |
| 1986 | | |
| 1987 | | /** |
| 1988 | | * Loads the stylesheet and resolves all @import commands. |
| 1989 | | * |
| 1990 | | * Loads a stylesheet and replaces @import commands with the contents of
the |
| 1991 | | * imported file. Use this instead of file_get_contents when processing |
| 1992 | | * stylesheets. |
| 1993 | | * |
| 1994 | | * The returned contents are compressed removing white space and comments
only |
| 1995 | | * when CSS aggregation is enabled. This optimization will not apply for |
| 1996 | | * color.module enabled themes with CSS aggregation turned off. |
| 1997 | | * |
| 1998 | | * @param $file |
| 1999 | | * Name of the stylesheet to be processed. |
| 2000 | | * @param $optimize |
| 2001 | | * Defines if CSS contents should be compressed or not. |
| 2002 | | * @return |
| 2003 | | * Contents of the stylesheet including the imported stylesheets. |
| 2004 | | */ |
| 2005 | 2366 | function drupal_load_stylesheet($file, $optimize = NULL) { |
| 2006 | 0 | static $_optimize; |
| 2007 | | // Store optimization parameter for preg_replace_callback with nested
@import loops. |
| 2008 | 0 | if (isset($optimize)) { |
| 2009 | 0 | $_optimize = $optimize; |
| 2010 | 0 | } |
| 2011 | | |
| 2012 | 0 | $contents = ''; |
| 2013 | 0 | if (file_exists($file)) { |
| 2014 | | // Load the local CSS stylesheet. |
| 2015 | 0 | $contents = file_get_contents($file); |
| 2016 | | |
| 2017 | | // Change to the current stylesheet's directory. |
| 2018 | 0 | $cwd = getcwd(); |
| 2019 | 0 | chdir(dirname($file)); |
| 2020 | | |
| 2021 | | // Replaces @import commands with the actual stylesheet content. |
| 2022 | | // This happens recursively but omits external files. |
| 2023 | 0 | $contents =
preg_replace_callback('/@import\s*(?:url\()?[\'"]?(?![a-z]+:)([^\'"\()]+)[\'"]?\)?;/',
'_drupal_load_stylesheet', $contents); |
| 2024 | | // Remove multiple charset declarations for standards compliance (and
fixing Safari problems). |
| 2025 | 0 | $contents = preg_replace('/^@charset\s+[\'"](\S*)\b[\'"];/i', '',
$contents); |
| 2026 | | |
| 2027 | 0 | if ($_optimize) { |
| 2028 | | // Perform some safe CSS optimizations. |
| 2029 | 0 | $contents = preg_replace('< |
| 2030 | | \s*([@{}:;,]|\)\s|\s\()\s* | # Remove whitespace around
separators, but keep space around parentheses. |
| 2031 | | /\*([^*\\\\]|\*(?!/))+\*/ | # Remove comments that are not CSS
hacks. |
| 2032 | | [\n\r] # Remove line breaks. |
| 2033 | 0 | >x', '\1', $contents); |
| 2034 | 0 | } |
| 2035 | | |
| 2036 | | // Change back directory. |
| 2037 | 0 | chdir($cwd); |
| 2038 | 0 | } |
| 2039 | | |
| 2040 | 0 | return $contents; |
| 2041 | 0 | } |
| 2042 | | |
| 2043 | | /** |
| 2044 | | * Loads stylesheets recursively and returns contents with corrected paths. |
| 2045 | | * |
| 2046 | | * This function is used for recursive loading of stylesheets and |
| 2047 | | * returns the stylesheet content with all url() paths corrected. |
| 2048 | | */ |
| 2049 | 2366 | function _drupal_load_stylesheet($matches) { |
| 2050 | 0 | $filename = $matches[1]; |
| 2051 | | // Load the imported stylesheet and replace @import commands in there as
well. |
| 2052 | 0 | $file = drupal_load_stylesheet($filename); |
| 2053 | | // Alter all url() paths, but not external. |
| 2054 | 0 | return preg_replace('/url\(([\'"]?)(?![a-z]+:)([^\'")]+)[\'"]?\)?;/i',
'url(\1' . dirname($filename) . '/', $file); |
| 2055 | 0 | } |
| 2056 | | |
| 2057 | | /** |
| 2058 | | * Delete all cached CSS files. |
| 2059 | | */ |
| 2060 | 2366 | function drupal_clear_css_cache() { |
| 2061 | 6 | file_scan_directory(file_create_path('css'), '/.*/', array('.', '..',
'CVS'), 'file_unmanaged_delete', TRUE); |
| 2062 | 6 | } |
| 2063 | | |
| 2064 | | /** |
| 2065 | | * Add a JavaScript file, setting or inline code to the page. |
| 2066 | | * |
| 2067 | | * The behavior of this function depends on the parameters it is called
with. |
| 2068 | | * Generally, it handles the addition of JavaScript to the page, either as |
| 2069 | | * reference to an existing file or as inline code. The following actions
can be |
| 2070 | | * performed using this function: |
| 2071 | | * |
| 2072 | | * - Add a file ('core', 'module' and 'theme'): |
| 2073 | | * Adds a reference to a JavaScript file to the page. JavaScript files |
| 2074 | | * are placed in a certain order, from 'core' first, to 'module' and
finally |
| 2075 | | * 'theme' so that files, that are added later, can override previously
added |
| 2076 | | * files with ease. |
| 2077 | | * |
| 2078 | | * - Add inline JavaScript code ('inline'): |
| 2079 | | * Executes a piece of JavaScript code on the current page by placing the
code |
| 2080 | | * directly in the page. This can, for example, be useful to tell the
user that |
| 2081 | | * a new message arrived, by opening a pop up, alert box etc. |
| 2082 | | * |
| 2083 | | * - Add settings ('setting'): |
| 2084 | | * Adds a setting to Drupal's global storage of JavaScript settings.
Per-page |
| 2085 | | * settings are required by some modules to function properly. The
settings |
| 2086 | | * will be accessible at Drupal.settings. |
| 2087 | | * |
| 2088 | | * Examples: |
| 2089 | | * @code |
| 2090 | | * drupal_add_js('misc/collapse.js'); |
| 2091 | | * drupal_add_js('misc/collapse.js', 'module'); |
| 2092 | | * drupal_add_js('$(document).ready(function(){alert("Hello!");});', |
| 2093 | | * array('type' => 'inline', 'scope' => 'footer') |
| 2094 | | * ); |
| 2095 | | * @endcode |
| 2096 | | * |
| 2097 | | * @param $data |
| 2098 | | * (optional) If given, the value depends on the $options parameter: |
| 2099 | | * - 'core', 'module' or 'theme': Path to the file relative to
base_path(). |
| 2100 | | * - 'inline': The JavaScript code that should be placed in the given
scope. |
| 2101 | | * - 'setting': An array with configuration options as associative array.
The |
| 2102 | | * array is directly placed in Drupal.settings. You might want to
wrap your |
| 2103 | | * actual configuration settings in another variable to prevent the
pollution |
| 2104 | | * of the Drupal.settings namespace. |
| 2105 | | * @param $options |
| 2106 | | * (optional) A string defining the type of JavaScript that is being
added |
| 2107 | | * in the $data parameter ('core', 'module', 'theme', 'setting',
'inline'), |
| 2108 | | * or an array which can have any or all of the following keys (these are |
| 2109 | | * not valid with type => 'setting'): |
| 2110 | | * - type |
| 2111 | | * The type of JavaScript that should be added to the page. Allowed |
| 2112 | | * values are 'core', 'module', 'theme', 'inline' and 'setting'.
Defaults |
| 2113 | | * to 'module'. |
| 2114 | | * - scope |
| 2115 | | * The location in which you want to place the script. Possible |
| 2116 | | * values are 'header' and 'footer'. If your theme implements
different |
| 2117 | | * locations, however, you can also use these. Defaults to 'header'. |
| 2118 | | * - defer |
| 2119 | | * If set to TRUE, the defer attribute is set on the <script> tag. |
| 2120 | | * Defaults to FALSE. This parameter is not used with 'type' =>
'setting'. |
| 2121 | | * - cache |
| 2122 | | * If set to FALSE, the JavaScript file is loaded anew on every page |
| 2123 | | * call, that means, it is not cached. Used only when type references |
| 2124 | | * a JavaScript file. Defaults to TRUE. |
| 2125 | | * - preprocess |
| 2126 | | * Aggregate the JavaScript if the JavaScript optimization setting
has |
| 2127 | | * been toggled in admin/settings/performance. Defaults to TRUE. |
| 2128 | | * @param $reset |
| 2129 | | * (optional) Resets the currently loaded JavaScript. |
| 2130 | | * @return |
| 2131 | | * The contructed array of JavaScript files. |
| 2132 | | * @see drupal_get_js() |
| 2133 | | */ |
| 2134 | 2366 | function drupal_add_js($data = NULL, $options = NULL, $reset = FALSE) { |
| 2135 | 1774 | static $javascript = array(); |
| 2136 | | |
| 2137 | | // Construct the options, taking the defaults into consideration. |
| 2138 | 1774 | if (isset($options)) { |
| 2139 | 1767 | if (!is_array($options)) { |
| 2140 | 220 | $options = array('type' => $options); |
| 2141 | 220 | } |
| 2142 | 1767 | } |
| 2143 | | else { |
| 2144 | 620 | $options = array(); |
| 2145 | | } |
| 2146 | | $options += array( |
| 2147 | 1774 | 'type' => 'module', |
| 2148 | | // Default to a header scope only if we're adding some data. |
| 2149 | 1774 | 'scope' => isset($data) ? 'header' : NULL, |
| 2150 | 1774 | 'cache' => TRUE, |
| 2151 | 1774 | 'defer' => FALSE, |
| 2152 | | 'preprocess' => TRUE |
| 2153 | 1774 | ); |
| 2154 | | // Preprocess can only be set if caching is enabled. |
| 2155 | 1774 | $options['preprocess'] = $options['cache'] ? $options['preprocess'] :
FALSE; |
| 2156 | 1774 | $type = $options['type']; |
| 2157 | 1774 | $scope = $options['scope']; |
| 2158 | 1774 | unset($options['type'], $options['scope']); |
| 2159 | | |
| 2160 | | // Request made to reset the JavaScript added so far. |
| 2161 | 1774 | if ($reset) { |
| 2162 | 1 | $javascript = array(); |
| 2163 | 1 | } |
| 2164 | | |
| 2165 | 1774 | if (isset($data)) { |
| 2166 | | // Add jquery.js and drupal.js, as well as the basePath setting, the |
| 2167 | | // first time a Javascript file is added. |
| 2168 | 590 | if (empty($javascript)) { |
| 2169 | | $javascript = array( |
| 2170 | | 'header' => array( |
| 2171 | | 'core' => array( |
| 2172 | 590 | 'misc/jquery.js' => array('cache' => TRUE, 'defer' => FALSE,
'preprocess' => TRUE), |
| 2173 | 590 | 'misc/drupal.js' => array('cache' => TRUE, 'defer' => FALSE,
'preprocess' => TRUE), |
| 2174 | 590 | ), |
| 2175 | 590 | 'module' => array(), |
| 2176 | 590 | 'theme' => array(), |
| 2177 | | 'setting' => array( |
| 2178 | 590 | array('basePath' => base_path()), |
| 2179 | 590 | ), |
| 2180 | 590 | 'inline' => array(), |
| 2181 | | ) |
| 2182 | 590 | ); |
| 2183 | 590 | } |
| 2184 | | |
| 2185 | 590 | if (isset($scope) && !isset($javascript[$scope])) { |
| 2186 | 1 | $javascript[$scope] = array('core' => array(), 'module' => array(),
'theme' => array(), 'setting' => array(), 'inline' => array()); |
| 2187 | 1 | } |
| 2188 | | |
| 2189 | 590 | if (isset($type) && isset($scope) &&
!isset($javascript[$scope][$type])) { |
| 2190 | 0 | $javascript[$scope][$type] = array(); |
| 2191 | 0 | } |
| 2192 | | |
| 2193 | | switch ($type) { |
| 2194 | 590 | case 'setting': |
| 2195 | 212 | $javascript[$scope][$type][] = $data; |
| 2196 | 212 | break; |
| 2197 | 590 | case 'inline': |
| 2198 | 16 | $javascript[$scope][$type][] = array('code' => $data, 'defer' =>
$options['defer']); |
| 2199 | 16 | break; |
| 2200 | 590 | default: |
| 2201 | 590 | $javascript[$scope][$type][$data] = $options; |
| 2202 | 590 | } |
| 2203 | 590 | } |
| 2204 | | |
| 2205 | 1774 | if (isset($scope)) { |
| 2206 | 1774 | return isset($javascript[$scope]) ? $javascript[$scope] : array(); |
| 2207 | 0 | } |
| 2208 | | else { |
| 2209 | 71 | return $javascript; |
| 2210 | | } |
| 2211 | 0 | } |
| 2212 | | |
| 2213 | | /** |
| 2214 | | * Returns a themed presentation of all JavaScript code for the current
page. |
| 2215 | | * |
| 2216 | | * References to JavaScript files are placed in a certain order: first, all |
| 2217 | | * 'core' files, then all 'module' and finally all 'theme' JavaScript files |
| 2218 | | * are added to the page. Then, all settings are output, followed by
'inline' |
| 2219 | | * JavaScript code. If running update.php, all preprocessing is disabled. |
| 2220 | | * |
| 2221 | | * @param $scope |
| 2222 | | * (optional) The scope for which the JavaScript rules should be
returned. |
| 2223 | | * Defaults to 'header'. |
| 2224 | | * @param $javascript |
| 2225 | | * (optional) An array with all JavaScript code. Defaults to the default |
| 2226 | | * JavaScript array for the given scope. |
| 2227 | | * @return |
| 2228 | | * All JavaScript code segments and includes for the scope as HTML tags. |
| 2229 | | * @see drupal_add_js() |
| 2230 | | */ |
| 2231 | 2366 | function drupal_get_js($scope = 'header', $javascript = NULL) { |
| 2232 | 1741 | if ((!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') &&
function_exists('locale_update_js_files')) { |
| 2233 | 70 | locale_update_js_files(); |
| 2234 | 70 | } |
| 2235 | | |
| 2236 | 1741 | if (!isset($javascript)) { |
| 2237 | 1741 | $javascript = drupal_add_js(NULL, array('scope' => $scope)); |
| 2238 | 1741 | } |
| 2239 | | |
| 2240 | 1741 | if (empty($javascript)) { |
| 2241 | 1740 | return ''; |
| 2242 | 0 | } |
| 2243 | | |
| 2244 | 557 | $output = ''; |
| 2245 | 557 | $preprocessed = ''; |
| 2246 | 557 | $no_preprocess = array('core' => '', 'module' => '', 'theme' => ''); |
| 2247 | 557 | $files = array(); |
| 2248 | 557 | $preprocess_js = (variable_get('preprocess_js', FALSE) &&
(!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update')); |
| 2249 | 557 | $directory = file_directory_path(); |
| 2250 | 557 | $is_writable = is_dir($directory) && is_writable($directory) &&
(variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) ==
FILE_DOWNLOADS_PUBLIC); |
| 2251 | | |
| 2252 | | // A dummy query-string is added to filenames, to gain control over |
| 2253 | | // browser-caching. The string changes on every update or full cache |
| 2254 | | // flush, forcing browsers to load a new copy of the files, as the |
| 2255 | | // URL changed. Files that should not be cached (see drupal_add_js()) |
| 2256 | | // get REQUEST_TIME as query-string instead, to enforce reload on every |
| 2257 | | // page request. |
| 2258 | 557 | $query_string = '?' . substr(variable_get('css_js_query_string', '0'), 0,
1); |
| 2259 | | |
| 2260 | | // For inline Javascript to validate as XHTML, all Javascript containing |
| 2261 | | // XHTML needs to be wrapped in CDATA. To make that backwards compatible |
| 2262 | | // with HTML 4, we need to comment out the CDATA-tag. |
| 2263 | 557 | $embed_prefix = "\n<!--//--><![CDATA[//><!--\n"; |
| 2264 | 557 | $embed_suffix = "\n//--><!]]>\n"; |
| 2265 | | |
| 2266 | 557 | foreach ($javascript as $type => $data) { |
| 2267 | 557 | if (!$data) continue; |
| 2268 | | |
| 2269 | | switch ($type) { |
| 2270 | 557 | case 'setting': |
| 2271 | 557 | $output .= '<script type="text/javascript">' . $embed_prefix .
'jQuery.extend(Drupal.settings, ' .
drupal_to_js(call_user_func_array('array_merge_recursive', $data)) . ");" .
$embed_suffix . "</script>\n"; |
| 2272 | 557 | break; |
| 2273 | 557 | case 'inline': |
| 2274 | 13 | foreach ($data as $info) { |
| 2275 | 13 | $output .= '<script type="text/javascript"' . ($info['defer'] ? '
defer="defer"' : '') . '>' . $embed_prefix . $info['code'] . $embed_suffix
. "</script>\n"; |
| 2276 | 13 | } |
| 2277 | 13 | break; |
| 2278 | 557 | default: |
| 2279 | | // If JS preprocessing is off, we still need to output the scripts. |
| 2280 | | // Additionally, go through any remaining scripts if JS
preprocessing is on and output the non-cached ones. |
| 2281 | 557 | foreach ($data as $path => $info) { |
| 2282 | 557 | if (!$info['preprocess'] || !$is_writable || !$preprocess_js) { |
| 2283 | 557 | $no_preprocess[$type] .= '<script type="text/javascript"' .
($info['defer'] ? ' defer="defer"' : '') . ' src="' . base_path() . $path .
($info['cache'] ? $query_string : '?' . REQUEST_TIME) . "\"></script>\n"; |
| 2284 | 557 | } |
| 2285 | | else { |
| 2286 | 0 | $files[$path] = $info; |
| 2287 | | } |
| 2288 | 557 | } |
| 2289 | 557 | } |
| 2290 | 557 | } |
| 2291 | | |
| 2292 | | // Aggregate any remaining JS files that haven't already been output. |
| 2293 | 557 | if ($is_writable && $preprocess_js && count($files) > 0) { |
| 2294 | 0 | $filename = md5(serialize($files) . $query_string) . '.js'; |
| 2295 | 0 | $preprocess_file = drupal_build_js_cache($files, $filename); |
| 2296 | 0 | $preprocessed .= '<script type="text/javascript" src="' . base_path() .
$preprocess_file . '"></script>' . "\n"; |
| 2297 | 0 | } |
| 2298 | | |
| 2299 | | // Keep the order of JS files consistent as some are preprocessed and
others are not. |
| 2300 | | // Make sure any inline or JS setting variables appear last after
libraries have loaded. |
| 2301 | 557 | $output = $preprocessed . implode('', $no_preprocess) . $output; |
| 2302 | | |
| 2303 | 557 | return $output; |
| 2304 | 0 | } |
| 2305 | | |
| 2306 | | /** |
| 2307 | | * Assist in adding the tableDrag JavaScript behavior to a themed table. |
| 2308 | | * |
| 2309 | | * Draggable tables should be used wherever an outline or list of sortable
items |
| 2310 | | * needs to be arranged by an end-user. Draggable tables are very flexible
and |
| 2311 | | * can manipulate the value of form elements placed within individual
columns. |
| 2312 | | * |
| 2313 | | * To set up a table to use drag and drop in place of weight select-lists
or |
| 2314 | | * in place of a form that contains parent relationships, the form must be |
| 2315 | | * themed into a table. The table must have an id attribute set. If using |
| 2316 | | * theme_table(), the id may be set as such: |
| 2317 | | * @code |
| 2318 | | * $output = theme('table', $header, $rows, array('id' =>
'my-module-table')); |
| 2319 | | * return $output; |
| 2320 | | * @endcode |
| 2321 | | * |
| 2322 | | * In the theme function for the form, a special class must be added to
each |
| 2323 | | * form element within the same column, "grouping" them together. |
| 2324 | | * |
| 2325 | | * In a situation where a single weight column is being sorted in the
table, the |
| 2326 | | * classes could be added like this (in the theme function): |
| 2327 | | * @code |
| 2328 | | * $form['my_elements'][$delta]['weight']['#attributes']['class'] =
"my-elements-weight"; |
| 2329 | | * @endcode |
| 2330 | | * |
| 2331 | | * Each row of the table must also have a class of "draggable" in order to
enable the |
| 2332 | | * drag handles: |
| 2333 | | * @code |
| 2334 | | * $row = array(...); |
| 2335 | | * $rows[] = array( |
| 2336 | | * 'data' => $row, |
| 2337 | | * 'class' => 'draggable', |
| 2338 | | * ); |
| 2339 | | * @endcode |
| 2340 | | * |
| 2341 | | * When tree relationships are present, the two additional classes |
| 2342 | | * 'tabledrag-leaf' and 'tabledrag-root' can be used to refine the
behavior: |
| 2343 | | * - Rows with the 'tabledrag-leaf' class cannot have child rows. |
| 2344 | | * - Rows with the 'tabledrag-root' class cannot be nested under a parent
row. |
| 2345 | | * |
| 2346 | | * Calling drupal_add_tabledrag() would then be written as such: |
| 2347 | | * @code |
| 2348 | | * drupal_add_tabledrag('my-module-table', 'order', 'sibling',
'my-elements-weight'); |
| 2349 | | * @endcode |
| 2350 | | * |
| 2351 | | * In a more complex case where there are several groups in one column
(such as |
| 2352 | | * the block regions on the admin/build/block page), a separate subgroup
class |
| 2353 | | * must also be added to differentiate the groups. |
| 2354 | | * @code |
| 2355 | | * $form['my_elements'][$region][$delta]['weight']['#attributes']['class']
= "my-elements-weight my-elements-weight-" . $region; |
| 2356 | | * @endcode |
| 2357 | | * |
| 2358 | | * $group is still 'my-element-weight', and the additional $subgroup
variable |
| 2359 | | * will be passed in as 'my-elements-weight-' . $region. This also means
that |
| 2360 | | * you'll need to call drupal_add_tabledrag() once for every region added. |
| 2361 | | * |
| 2362 | | * @code |
| 2363 | | * foreach ($regions as $region) { |
| 2364 | | * drupal_add_tabledrag('my-module-table', 'order', 'sibling',
'my-elements-weight', 'my-elements-weight-' . $region); |
| 2365 | | * } |
| 2366 | | * @endcode |
| 2367 | | * |
| 2368 | | * In a situation where tree relationships are present, adding multiple |
| 2369 | | * subgroups is not necessary, because the table will contain indentations
that |
| 2370 | | * provide enough information about the sibling and parent relationships. |
| 2371 | | * See theme_menu_overview_form() for an example creating a table
containing |
| 2372 | | * parent relationships. |
| 2373 | | * |
| 2374 | | * Please note that this function should be called from the theme layer,
such as |
| 2375 | | * in a .tpl.php file, theme_ function, or in a template_preprocess
function, |
| 2376 | | * not in a form declartion. Though the same JavaScript could be added to
the |
| 2377 | | * page using drupal_add_js() directly, this function helps keep template
files |
| 2378 | | * clean and readable. It also prevents tabledrag.js from being added twice |
| 2379 | | * accidentally. |
| 2380 | | * |
| 2381 | | * @param $table_id |
| 2382 | | * String containing the target table's id attribute. If the table does
not |
| 2383 | | * have an id, one will need to be set, such as <table
id="my-module-table">. |
| 2384 | | * @param $action |
| 2385 | | * String describing the action to be done on the form item. Either
'match' |
| 2386 | | * 'depth', or 'order'. Match is typically used for parent relationships. |
| 2387 | | * Order is typically used to set weights on other form elements with the
same |
| 2388 | | * group. Depth updates the target element with the current indentation. |
| 2389 | | * @param $relationship |
| 2390 | | * String describing where the $action variable should be performed.
Either |
| 2391 | | * 'parent', 'sibling', 'group', or 'self'. Parent will only look for
fields |
| 2392 | | * up the tree. Sibling will look for fields in the same group in rows
above |
| 2393 | | * and below it. Self affects the dragged row itself. Group affects the |
| 2394 | | * dragged row, plus any children below it (the entire dragged group). |
| 2395 | | * @param $group |
| 2396 | | * A class name applied on all related form elements for this action. |
| 2397 | | * @param $subgroup |
| 2398 | | * (optional) If the group has several subgroups within it, this string
should |
| 2399 | | * contain the class name identifying fields in the same subgroup. |
| 2400 | | * @param $source |
| 2401 | | * (optional) If the $action is 'match', this string should contain the
class |
| 2402 | | * name identifying what field will be used as the source value when
matching |
| 2403 | | * the value in $subgroup. |
| 2404 | | * @param $hidden |
| 2405 | | * (optional) The column containing the field elements may be entirely
hidden |
| 2406 | | * from view dynamically when the JavaScript is loaded. Set to FALSE if
the |
| 2407 | | * column should not be hidden. |
| 2408 | | * @param $limit |
| 2409 | | * (optional) Limit the maximum amount of parenting in this table. |
| 2410 | | * @see block-admin-display-form.tpl.php |
| 2411 | | * @see theme_menu_overview_form() |
| 2412 | | */ |
| 2413 | 2366 | function drupal_add_tabledrag($table_id, $action, $relationship, $group,
$subgroup = NULL, $source = NULL, $hidden = TRUE, $limit = 0) { |
| 2414 | 90 | static $js_added = FALSE; |
| 2415 | 90 | if (!$js_added) { |
| 2416 | 90 | drupal_add_js('misc/tabledrag.js', 'core'); |
| 2417 | 90 | $js_added = TRUE; |
| 2418 | 90 | } |
| 2419 | | |
| 2420 | | // If a subgroup or source isn't set, assume it is the same as the group. |
| 2421 | 90 | $target = isset($subgroup) ? $subgroup : $group; |
| 2422 | 90 | $source = isset($source) ? $source : $target; |
| 2423 | 90 | $settings['tableDrag'][$table_id][$group][] = array( |
| 2424 | 90 | 'target' => $target, |
| 2425 | 90 | 'source' => $source, |
| 2426 | 90 | 'relationship' => $relationship, |
| 2427 | 90 | 'action' => $action, |
| 2428 | 90 | 'hidden' => $hidden, |
| 2429 | 90 | 'limit' => $limit, |
| 2430 | | ); |
| 2431 | 90 | drupal_add_js($settings, 'setting'); |
| 2432 | 90 | } |
| 2433 | | |
| 2434 | | /** |
| 2435 | | * Aggregate JS files, putting them in the files directory. |
| 2436 | | * |
| 2437 | | * @param $files |
| 2438 | | * An array of JS files to aggregate and compress into one file. |
| 2439 | | * @param $filename |
| 2440 | | * The name of the aggregate JS file. |
| 2441 | | * @return |
| 2442 | | * The name of the JS file. |
| 2443 | | */ |
| 2444 | 2366 | function drupal_build_js_cache($files, $filename) { |
| 2445 | 0 | $contents = ''; |
| 2446 | | |
| 2447 | | // Create the js/ within the files folder. |
| 2448 | 0 | $jspath = file_create_path('js'); |
| 2449 | 0 | file_check_directory($jspath, FILE_CREATE_DIRECTORY); |
| 2450 | | |
| 2451 | 0 | if (!file_exists($jspath . '/' . $filename)) { |
| 2452 | | // Build aggregate JS file. |
| 2453 | 0 | foreach ($files as $path => $info) { |
| 2454 | 0 | if ($info['preprocess']) { |
| 2455 | | // Append a ';' after each JS file to prevent them from running
together. |
| 2456 | 0 | $contents .= file_get_contents($path) . ';'; |
| 2457 | 0 | } |
| 2458 | 0 | } |
| 2459 | | |
| 2460 | | // Create the JS file. |
| 2461 | 0 | file_unmanaged_save_data($contents, $jspath . '/' . $filename,
FILE_EXISTS_REPLACE); |
| 2462 | 0 | } |
| 2463 | | |
| 2464 | 0 | return $jspath . '/' . $filename; |
| 2465 | 0 | } |
| 2466 | | |
| 2467 | | /** |
| 2468 | | * Delete all cached JS files. |
| 2469 | | */ |
| 2470 | 2366 | function drupal_clear_js_cache() { |
| 2471 | 3 | file_scan_directory(file_create_path('js'), '/.*/', array('.', '..',
'CVS'), 'file_unmanaged_delete', TRUE); |
| 2472 | 3 | variable_set('javascript_parsed', array()); |
| 2473 | 3 | } |
| 2474 | | |
| 2475 | | /** |
| 2476 | | * Converts a PHP variable into its Javascript equivalent. |
| 2477 | | * |
| 2478 | | * We use HTML-safe strings, i.e. with <, > and & escaped. |
| 2479 | | */ |
| 2480 | 2366 | function drupal_to_js($var) { |
| 2481 | | // json_encode() does not escape <, > and &, so we do it with
str_replace() |
| 2482 | 559 | return str_replace(array("<", ">", "&"), array('\x3c', '\x3e', '\x26'),
json_encode($var)); |
| 2483 | 0 | } |
| 2484 | | |
| 2485 | | /** |
| 2486 | | * Return data in JSON format. |
| 2487 | | * |
| 2488 | | * This function should be used for JavaScript callback functions returning |
| 2489 | | * data in JSON format. It sets the header for JavaScript output. |
| 2490 | | * |
| 2491 | | * @param $var |
| 2492 | | * (optional) If set, the variable will be converted to JSON and output. |
| 2493 | | */ |
| 2494 | 2366 | function drupal_json($var = NULL) { |
| 2495 | | // We are returning JavaScript, so tell the browser. |
| 2496 | 2 | drupal_set_header('Content-Type: text/javascript; charset=utf-8'); |
| 2497 | | |
| 2498 | 2 | if (isset($var)) { |
| 2499 | 2 | echo drupal_to_js($var); |
| 2500 | 2 | } |
| 2501 | 2 | } |
| 2502 | | |
| 2503 | | /** |
| 2504 | | * Wrapper around urlencode() which avoids Apache quirks. |
| 2505 | | * |
| 2506 | | * Should be used when placing arbitrary data in an URL. Note that Drupal
paths |
| 2507 | | * are urlencoded() when passed through url() and do not require
urlencoding() |
| 2508 | | * of individual components. |
| 2509 | | * |
| 2510 | | * Notes: |
| 2511 | | * - For esthetic reasons, we do not escape slashes. This also avoids a
'feature' |
| 2512 | | * in Apache where it 404s on any path containing '%2F'. |
| 2513 | | * - mod_rewrite unescapes %-encoded ampersands, hashes, and slashes when
clean |
| 2514 | | * URLs are used, which are interpreted as delimiters by PHP. These |
| 2515 | | * characters are double escaped so PHP will still see the encoded
version. |
| 2516 | | * - With clean URLs, Apache changes '//' to '/', so every second slash is |
| 2517 | | * double escaped. |
| 2518 | | * |
| 2519 | | * @param $text |
| 2520 | | * String to encode |
| 2521 | | */ |
| 2522 | 2366 | function drupal_urlencode($text) { |
| 2523 | 2413 | if (variable_get('clean_url', '0')) { |
| 2524 | 2413 | return str_replace(array('%2F', '%26', '%23', '//'), |
| 2525 | 2413 | array('/', '%2526', '%2523', '/%252F'), |
| 2526 | 2413 | rawurlencode($text)); |
| 2527 | 0 | } |
| 2528 | | else { |
| 2529 | 0 | return str_replace('%2F', '/', rawurlencode($text)); |
| 2530 | | } |
| 2531 | 0 | } |
| 2532 | | |
| 2533 | | /** |
| 2534 | | * Returns a string of highly randomized bytes (over the full 8-bit range). |
| 2535 | | * |
| 2536 | | * This function is better than simply calling mt_rand() or any other
built-in |
| 2537 | | * PHP function because it can return a long string of bytes (compared to <
4 |
| 2538 | | * bytes normally from mt_rand()) and uses the best available pseudo-random
source. |
| 2539 | | * |
| 2540 | | * @param $count |
| 2541 | | * The number of characters (bytes) to return in the string. |
| 2542 | | */ |
| 2543 | 2366 | function drupal_random_bytes($count) { |
| 2544 | 157 | static $random_state; |
| 2545 | | // We initialize with the somewhat random PHP process ID on the first
call. |
| 2546 | 157 | if (empty($random_state)) { |
| 2547 | 157 | $random_state = getmypid(); |
| 2548 | 157 | } |
| 2549 | 157 | $output = ''; |
| 2550 | | // /dev/urandom is available on many *nix systems and is considered the
best |
| 2551 | | // commonly available pseudo-random source. |
| 2552 | 157 | if ($fh = @fopen('/dev/urandom', 'rb')) { |
| 2553 | 157 | $output = fread($fh, $count); |
| 2554 | 157 | fclose($fh); |
| 2555 | 157 | } |
| 2556 | | // If /dev/urandom is not available or returns no bytes, this loop will |
| 2557 | | // generate a good set of pseudo-random bytes on any system. |
| 2558 | | // Note that it may be important that our $random_state is passed |
| 2559 | | // through md5() prior to being rolled into $output, that the two md5() |
| 2560 | | // invocations are different, and that the extra input into the first one
- |
| 2561 | | // the microtime() - is prepended rather than appended. This is to avoid |
| 2562 | | // directly leaking $random_state via the $output stream, which could |
| 2563 | | // allow for trivial prediction of further "random" numbers. |
| 2564 | 157 | while (strlen($output) < $count) { |
| 2565 | 0 | $random_state = md5(microtime() . mt_rand() . $random_state); |
| 2566 | 0 | $output .= md5(mt_rand() . $random_state, TRUE); |
| 2567 | 0 | } |
| 2568 | 157 | return substr($output, 0, $count); |
| 2569 | 0 | } |
| 2570 | | |
| 2571 | | /** |
| 2572 | | * Ensure the private key variable used to generate tokens is set. |
| 2573 | | * |
| 2574 | | * @return |
| 2575 | | * The private key. |
| 2576 | | */ |
| 2577 | 2366 | function drupal_get_private_key() { |
| 2578 | 848 | if (!($key = variable_get('drupal_private_key', 0))) { |
| 2579 | 77 | $key = md5(drupal_random_bytes(64)); |
| 2580 | 77 | variable_set('drupal_private_key', $key); |
| 2581 | 77 | } |
| 2582 | 848 | return $key; |
| 2583 | 0 | } |
| 2584 | | |
| 2585 | | /** |
| 2586 | | * Generate a token based on $value, the current user session and private
key. |
| 2587 | | * |
| 2588 | | * @param $value |
| 2589 | | * An additional value to base the token on. |
| 2590 | | */ |
| 2591 | 2366 | function drupal_get_token($value = '') { |
| 2592 | 848 | $private_key = drupal_get_private_key(); |
| 2593 | 848 | return md5(session_id() . $value . $private_key); |
| 2594 | 0 | } |
| 2595 | | |
| 2596 | | /** |
| 2597 | | * Validate a token based on $value, the current user session and private
key. |
| 2598 | | * |
| 2599 | | * @param $token |
| 2600 | | * The token to be validated. |
| 2601 | | * @param $value |
| 2602 | | * An additional value to base the token on. |
| 2603 | | * @param $skip_anonymous |
| 2604 | | * Set to true to skip token validation for anonymous users. |
| 2605 | | * @return |
| 2606 | | * True for a valid token, false for an invalid token. When
$skip_anonymous |
| 2607 | | * is true, the return value will always be true for anonymous users. |
| 2608 | | */ |
| 2609 | 2366 | function drupal_valid_token($token, $value = '', $skip_anonymous = FALSE) { |
| 2610 | 338 | global $user; |
| 2611 | 338 | return (($skip_anonymous && $user->uid == 0) || ($token ==
md5(session_id() . $value . variable_get('drupal_private_key', '')))); |
| 2612 | 0 | } |
| 2613 | | |
| 2614 | | /** |
| 2615 | | * Performs one or more XML-RPC request(s). |
| 2616 | | * |
| 2617 | | * @param $url |
| 2618 | | * An absolute URL of the XML-RPC endpoint. |
| 2619 | | * Example: |
| 2620 | | * http://www.example.com/xmlrpc.php |
| 2621 | | * @param ... |
| 2622 | | * For one request: |
| 2623 | | * The method name followed by a variable number of arguments to the
method. |
| 2624 | | * For multiple requests (system.multicall): |
| 2625 | | * An array of call arrays. Each call array follows the pattern of the
single |
| 2626 | | * request: method name followed by the arguments to the method. |
| 2627 | | * @return |
| 2628 | | * For one request: |
| 2629 | | * Either the return value of the method on success, or FALSE. |
| 2630 | | * If FALSE is returned, see xmlrpc_errno() and xmlrpc_error_msg(). |
| 2631 | | * For multiple requests: |
| 2632 | | * An array of results. Each result will either be the result |
| 2633 | | * returned by the method called, or an xmlrpc_error object if the call |
| 2634 | | * failed. See xmlrpc_error(). |
| 2635 | | */ |
| 2636 | 2366 | function xmlrpc($url) { |
| 2637 | 3 | require_once DRUPAL_ROOT . '/includes/xmlrpc.inc'; |
| 2638 | 3 | $args = func_get_args(); |
| 2639 | 3 | return call_user_func_array('_xmlrpc', $args); |
| 2640 | 0 | } |
| 2641 | | |
| 2642 | 2366 | function _drupal_bootstrap_full() { |
| 2643 | 2366 | static $called; |
| 2644 | | |
| 2645 | 2366 | if ($called) { |
| 2646 | 0 | return; |
| 2647 | 0 | } |
| 2648 | 2366 | $called = 1; |
| 2649 | 2366 | require_once DRUPAL_ROOT . '/includes/theme.inc'; |
| 2650 | 2366 | require_once DRUPAL_ROOT . '/includes/pager.inc'; |
| 2651 | 2366 | require_once DRUPAL_ROOT . '/includes/menu.inc'; |
| 2652 | 2366 | require_once DRUPAL_ROOT . '/includes/tablesort.inc'; |
| 2653 | 2366 | require_once DRUPAL_ROOT . '/includes/file.inc'; |
| 2654 | 2366 | require_once DRUPAL_ROOT . '/includes/unicode.inc'; |
| 2655 | 2366 | require_once DRUPAL_ROOT . '/includes/image.inc'; |
| 2656 | 2366 | require_once DRUPAL_ROOT . '/includes/form.inc'; |
| 2657 | 2366 | require_once DRUPAL_ROOT . '/includes/mail.inc'; |
| 2658 | 2366 | require_once DRUPAL_ROOT . '/includes/actions.inc'; |
| 2659 | | // Set the Drupal custom error handler. |
| 2660 | 2366 | set_error_handler('_drupal_error_handler'); |
| 2661 | 2366 | set_exception_handler('_drupal_exception_handler'); |
| 2662 | | |
| 2663 | | // Emit the correct charset HTTP header. |
| 2664 | 2366 | drupal_set_header('Content-Type: text/html; charset=utf-8'); |
| 2665 | | // Detect string handling method |
| 2666 | 2366 | unicode_check(); |
| 2667 | | // Undo magic quotes |
| 2668 | 2366 | fix_gpc_magic(); |
| 2669 | | // Load all enabled modules |
| 2670 | 2366 | module_load_all(); |
| 2671 | | |
| 2672 | | // Let all modules take action before menu system handles the request |
| 2673 | | // We do not want this while running update.php. |
| 2674 | 2366 | if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') { |
| 2675 | 2366 | module_invoke_all('init'); |
| 2676 | 2366 | } |
| 2677 | 2366 | } |
| 2678 | | |
| 2679 | | /** |
| 2680 | | * Store the current page in the cache. |
| 2681 | | * |
| 2682 | | * We try to store a gzipped version of the cache. This requires the |
| 2683 | | * PHP zlib extension (http://php.net/manual/en/ref.zlib.php). |
| 2684 | | * Presence of the extension is checked by testing for the function |
| 2685 | | * gzencode. There are two compression algorithms: gzip and deflate. |
| 2686 | | * The majority of all modern browsers support gzip or both of them. |
| 2687 | | * We thus only deal with the gzip variant and unzip the cache in case |
| 2688 | | * the browser does not accept gzip encoding. |
| 2689 | | * |
| 2690 | | * @see drupal_page_header |
| 2691 | | */ |
| 2692 | 2366 | function page_set_cache() { |
| 2693 | 1 | global $user, $base_root; |
| 2694 | | |
| 2695 | 1 | if (!$user->uid && ($_SERVER['REQUEST_METHOD'] == 'GET' ||
$_SERVER['REQUEST_METHOD'] == 'HEAD') && count(drupal_get_messages(NULL,
FALSE)) == 0) { |
| 2696 | | // This will fail in some cases, see page_get_cache() for the
explanation. |
| 2697 | 1 | if ($data = ob_get_contents()) { |
| 2698 | 1 | $cache = TRUE; |
| 2699 | 1 | if (variable_get('page_compression', TRUE) &&
function_exists('gzencode')) { |
| 2700 | | // We do not store the data in case the zlib mode is deflate. |
| 2701 | | // This should be rarely happening. |
| 2702 | 1 | if (zlib_get_coding_type() == 'deflate') { |
| 2703 | 0 | $cache = FALSE; |
| 2704 | 0 | } |
| 2705 | 1 | elseif (zlib_get_coding_type() == FALSE) { |
| 2706 | 1 | $data = gzencode($data, 9, FORCE_GZIP); |
| 2707 | 1 | } |
| 2708 | | // The remaining case is 'gzip' which means the data is |
| 2709 | | // already compressed and nothing left to do but to store it. |
| 2710 | 1 | } |
| 2711 | 1 | ob_end_flush(); |
| 2712 | 1 | if ($cache && $data) { |
| 2713 | 1 | cache_set($base_root . request_uri(), $data, 'cache_page',
CACHE_TEMPORARY, drupal_get_headers()); |
| 2714 | 1 | } |
| 2715 | 1 | } |
| 2716 | 1 | } |
| 2717 | 1 | } |
| 2718 | | |
| 2719 | | /** |
| 2720 | | * Executes a cron run when called |
| 2721 | | * @return |
| 2722 | | * Returns TRUE if ran successfully |
| 2723 | | */ |
| 2724 | 2366 | function drupal_cron_run() { |
| 2725 | | // Allow execution to continue even if the request gets canceled. |
| 2726 | 2 | @ignore_user_abort(TRUE); |
| 2727 | | |
| 2728 | | // Increase the maximum execution time. |
| 2729 | 2 | @set_time_limit(240); |
| 2730 | | |
| 2731 | | // Fetch the cron semaphore |
| 2732 | 2 | $semaphore = variable_get('cron_semaphore', FALSE); |
| 2733 | | |
| 2734 | 2 | if ($semaphore) { |
| 2735 | 0 | if (REQUEST_TIME - $semaphore > 3600) { |
| 2736 | | // Either cron has been running for more than an hour or the
semaphore |
| 2737 | | // was not reset due to a database error. |
| 2738 | 0 | watchdog('cron', 'Cron has been running for more than an hour and is
most likely stuck.', array(), WATCHDOG_ERROR); |
| 2739 | | |
| 2740 | | // Release cron semaphore |
| 2741 | 0 | variable_del('cron_semaphore'); |
| 2742 | 0 | } |
| 2743 | | else { |
| 2744 | | // Cron is still running normally. |
| 2745 | 0 | watchdog('cron', 'Attempting to re-run cron while it is already
running.', array(), WATCHDOG_WARNING); |
| 2746 | | } |
| 2747 | 0 | } |
| 2748 | | else { |
| 2749 | | // Register shutdown callback |
| 2750 | 2 | register_shutdown_function('drupal_cron_cleanup'); |
| 2751 | | |
| 2752 | | // Lock cron semaphore |
| 2753 | 2 | variable_set('cron_semaphore', REQUEST_TIME); |
| 2754 | | |
| 2755 | | // Iterate through the modules calling their cron handlers (if any): |
| 2756 | 2 | module_invoke_all('cron'); |
| 2757 | | |
| 2758 | | // Record cron time |
| 2759 | 2 | variable_set('cron_last', REQUEST_TIME); |
| 2760 | 2 | watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE); |
| 2761 | | |
| 2762 | | // Release cron semaphore |
| 2763 | 2 | variable_del('cron_semaphore'); |
| 2764 | | |
| 2765 | | // Return TRUE so other functions can check if it did run successfully |
| 2766 | 2 | return TRUE; |
| 2767 | | } |
| 2768 | 0 | } |
| 2769 | | |
| 2770 | | /** |
| 2771 | | * Shutdown function for cron cleanup. |
| 2772 | | */ |
| 2773 | 2366 | function drupal_cron_cleanup() { |
| 2774 | | // See if the semaphore is still locked. |
| 2775 | 0 | if (variable_get('cron_semaphore', FALSE)) { |
| 2776 | 0 | watchdog('cron', 'Cron run exceeded the time limit and was aborted.',
array(), WATCHDOG_WARNING); |
| 2777 | | |
| 2778 | | // Release cron semaphore |
| 2779 | 0 | variable_del('cron_semaphore'); |
| 2780 | 0 | } |
| 2781 | 0 | } |
| 2782 | | |
| 2783 | | /** |
| 2784 | | * Return an array of system file objects. |
| 2785 | | * |
| 2786 | | * Returns an array of file objects of the given type from the site-wide |
| 2787 | | * directory (i.e. modules/), the all-sites directory (i.e. |
| 2788 | | * sites/all/modules/), the profiles directory, and site-specific directory |
| 2789 | | * (i.e. sites/somesite/modules/). The returned array will be keyed using
the |
| 2790 | | * key specified (name, basename, filename). Using name or basename will
cause |
| 2791 | | * site-specific files to be prioritized over similar files in the default |
| 2792 | | * directories. That is, if a file with the same name appears in both the |
| 2793 | | * site-wide directory and site-specific directory, only the site-specific |
| 2794 | | * version will be included. |
| 2795 | | * |
| 2796 | | * @param $mask |
| 2797 | | * The preg_match() regular expression of the files to find. |
| 2798 | | * @param $directory |
| 2799 | | * The subdirectory name in which the files are found. For example, |
| 2800 | | * 'modules' will search in both modules/ and |
| 2801 | | * sites/somesite/modules/. |
| 2802 | | * @param $key |
| 2803 | | * The key to be passed to file_scan_directory(). |
| 2804 | | * @param $min_depth |
| 2805 | | * Minimum depth of directories to return files from. |
| 2806 | | * |
| 2807 | | * @return |
| 2808 | | * An array of file objects of the specified type. |
| 2809 | | */ |
| 2810 | 2366 | function drupal_system_listing($mask, $directory, $key = 'name', $min_depth
= 1) { |
| 2811 | 193 | global $profile; |
| 2812 | 193 | $config = conf_path(); |
| 2813 | | |
| 2814 | | // When this function is called during Drupal's initial installation
process, |
| 2815 | | // the name of the profile that's about to be installed is stored in the
global |
| 2816 | | // $profile variable. At all other times, the standard Drupal systems
variable |
| 2817 | | // table contains the name of the current profile, and we can call
variable_get() |
| 2818 | | // to determine what one is active. |
| 2819 | 193 | if (!isset($profile)) { |
| 2820 | 63 | $profile = variable_get('install_profile', 'default'); |
| 2821 | 63 | } |
| 2822 | 193 | $searchdir = array($directory); |
| 2823 | 193 | $files = array(); |
| 2824 | | |
| 2825 | | // Always search sites/all/* as well as the global directories |
| 2826 | 193 | $searchdir[] = 'sites/all/' . $directory; |
| 2827 | | |
| 2828 | | // The 'profiles' directory contains pristine collections of modules and |
| 2829 | | // themes as organized by a distribution. It is pristine in the same way |
| 2830 | | // that /modules is pristine for core; users should avoid changing
anything |
| 2831 | | // there in favor of sites/all or sites/<domain> directories. |
| 2832 | 193 | if (file_exists("profiles/$profile/$directory")) { |
| 2833 | 0 | $searchdir[] = "profiles/$profile/$directory"; |
| 2834 | 0 | } |
| 2835 | | |
| 2836 | 193 | if (file_exists("$config/$directory")) { |
| 2837 | 0 | $searchdir[] = "$config/$directory"; |
| 2838 | 0 | } |
| 2839 | | |
| 2840 | | // Get current list of items |
| 2841 | 193 | foreach ($searchdir as $dir) { |
| 2842 | 193 | $files = array_merge($files, file_scan_directory($dir, $mask,
array('.', '..', 'CVS'), 0, TRUE, $key, $min_depth)); |
| 2843 | 193 | } |
| 2844 | | |
| 2845 | 193 | return $files; |
| 2846 | 0 | } |
| 2847 | | |
| 2848 | | /** |
| 2849 | | * Hands off structured Drupal arrays to type-specific *_alter
implementations. |
| 2850 | | * |
| 2851 | | * This dispatch function hands off structured Drupal arrays to
type-specific |
| 2852 | | * *_alter implementations. It ensures a consistent interface for all
altering |
| 2853 | | * operations. |
| 2854 | | * |
| 2855 | | * @param $type |
| 2856 | | * The data type of the structured array. 'form', 'links', |
| 2857 | | * 'node_content', and so on are several examples. |
| 2858 | | * @param $data |
| 2859 | | * The structured array to be altered. |
| 2860 | | * @param ... |
| 2861 | | * Any additional params will be passed on to the called |
| 2862 | | * hook_$type_alter functions. |
| 2863 | | */ |
| 2864 | 2366 | function drupal_alter($type, &$data) { |
| 2865 | | // PHP's func_get_args() always returns copies of params, not references,
so |
| 2866 | | // drupal_alter() can only manipulate data that comes in via the required
first |
| 2867 | | // param. For the edge case functions that must pass in an arbitrary
number of |
| 2868 | | // alterable parameters (hook_form_alter() being the best example), an
array of |
| 2869 | | // those params can be placed in the __drupal_alter_by_ref key of the
$data |
| 2870 | | // array. This is somewhat ugly, but is an unavoidable consequence of a
flexible |
| 2871 | | // drupal_alter() function, and the limitations of func_get_args(). |
| 2872 | | // @todo: Remove this in Drupal 7. |
| 2873 | 2354 | if (is_array($data) && isset($data['__drupal_alter_by_ref'])) { |
| 2874 | 1575 | $by_ref_parameters = $data['__drupal_alter_by_ref']; |
| 2875 | 1575 | unset($data['__drupal_alter_by_ref']); |
| 2876 | 1575 | } |
| 2877 | | |
| 2878 | | // Hang onto a reference to the data array so that it isn't blown away
later. |
| 2879 | | // Also, merge in any parameters that need to be passed by reference. |
| 2880 | 2354 | $args = array(&$data); |
| 2881 | 2354 | if (isset($by_ref_parameters)) { |
| 2882 | 1575 | $args = array_merge($args, $by_ref_parameters); |
| 2883 | 1575 | } |
| 2884 | | |
| 2885 | | // Now, use func_get_args() to pull in any additional parameters passed
into |
| 2886 | | // the drupal_alter() call. |
| 2887 | 2354 | $additional_args = func_get_args(); |
| 2888 | 2354 | array_shift($additional_args); |
| 2889 | 2354 | array_shift($additional_args); |
| 2890 | 2354 | $args = array_merge($args, $additional_args); |
| 2891 | | |
| 2892 | 2354 | foreach (module_implements($type . '_alter') as $module) { |
| 2893 | 1595 | $function = $module . '_' . $type . '_alter'; |
| 2894 | 1595 | call_user_func_array($function, $args); |
| 2895 | 1595 | } |
| 2896 | 2354 | } |
| 2897 | | |
| 2898 | | /** |
| 2899 | | * Renders HTML given a structured array tree. |
| 2900 | | * |
| 2901 | | * Recursively iterates over each of the array elements, generating HTML
code. |
| 2902 | | * This function is usually called from within a another function, like |
| 2903 | | * drupal_get_form() or node_view(). |
| 2904 | | * |
| 2905 | | * @param $elements |
| 2906 | | * The structured array describing the data to be rendered. |
| 2907 | | * @return |
| 2908 | | * The rendered HTML. |
| 2909 | | */ |
| 2910 | 2366 | function drupal_render(&$elements) { |
| 2911 | 1560 | if (!isset($elements) || (isset($elements['#access']) &&
!$elements['#access'])) { |
| 2912 | 108 | return NULL; |
| 2913 | 0 | } |
| 2914 | | |
| 2915 | | // If the default values for this element haven't been loaded yet,
populate |
| 2916 | | // them. |
| 2917 | 1560 | if (!isset($elements['#defaults_loaded']) ||
!$elements['#defaults_loaded']) { |
| 2918 | 468 | if ((!empty($elements['#type'])) && ($info =
_element_info($elements['#type']))) { |
| 2919 | 191 | $elements += $info; |
| 2920 | 191 | } |
| 2921 | 468 | } |
| 2922 | | |
| 2923 | | // Make any final changes to the element before it is rendered. This
means |
| 2924 | | // that the $element or the children can be altered or corrected before
the |
| 2925 | | // element is rendered into the final text. |
| 2926 | 1560 | if (isset($elements['#pre_render'])) { |
| 2927 | 0 | foreach ($elements['#pre_render'] as $function) { |
| 2928 | 0 | if (drupal_function_exists($function)) { |
| 2929 | 0 | $elements = $function($elements); |
| 2930 | 0 | } |
| 2931 | 0 | } |
| 2932 | 0 | } |
| 2933 | | |
| 2934 | 1560 | $content = ''; |
| 2935 | | // Either the elements did not go through form_builder or one of the
children |
| 2936 | | // has a #weight. |
| 2937 | 1560 | if (!isset($elements['#sorted'])) { |
| 2938 | 929 | uasort($elements, "element_sort"); |
| 2939 | 929 | } |
| 2940 | 1560 | $elements += array('#title' => NULL, '#description' => NULL); |
| 2941 | 1560 | if (!isset($elements['#children'])) { |
| 2942 | 1560 | $children = element_children($elements); |
| 2943 | | // Render all the children that use a theme function. |
| 2944 | 1560 | if (isset($elements['#theme']) && empty($elements['#theme_used'])) { |
| 2945 | 406 | $elements['#theme_used'] = TRUE; |
| 2946 | | |
| 2947 | 406 | $previous = array(); |
| 2948 | 406 | foreach (array('#type', '#prefix', '#suffix') as $key) { |
| 2949 | 406 | $previous[$key] = isset($elements[$key]) ? $elements[$key] : NULL; |
| 2950 | 406 | } |
| 2951 | | // If we rendered a single element, then we will skip the renderer. |
| 2952 | 406 | if (empty($children)) { |
| 2953 | 30 | $elements['#printed'] = TRUE; |
| 2954 | 30 | } |
| 2955 | | else { |
| 2956 | 399 | $elements['#markup'] = ''; |
| 2957 | | } |
| 2958 | | |
| 2959 | 406 | unset($elements['#prefix'], $elements['#suffix']); |
| 2960 | 406 | $content = theme($elements['#theme'], $elements); |
| 2961 | | |
| 2962 | 406 | foreach (array('#type', '#prefix', '#suffix') as $key) { |
| 2963 | 406 | $elements[$key] = isset($previous[$key]) ? $previous[$key] : NULL; |
| 2964 | 406 | } |
| 2965 | 406 | } |
| 2966 | | // Render each of the children using drupal_render and concatenate
them. |
| 2967 | 1560 | if (!isset($content) || $content === '') { |
| 2968 | 1560 | foreach ($children as $key) { |
| 2969 | 1560 | $content .= drupal_render($elements[$key]); |
| 2970 | 1560 | } |
| 2971 | 1560 | } |
| 2972 | 1560 | } |
| 2973 | 1560 | if (isset($content) && $content !== '') { |
| 2974 | 1560 | $elements['#children'] = $content; |
| 2975 | 1560 | } |
| 2976 | | |
| 2977 | | // Until now, we rendered the children, here we render the element itself |
| 2978 | 1560 | if (!isset($elements['#printed'])) { |
| 2979 | 1560 | $content = theme(!empty($elements['#type']) ? $elements['#type'] :
'markup', $elements); |
| 2980 | 1560 | $elements['#printed'] = TRUE; |
| 2981 | 1560 | } |
| 2982 | | |
| 2983 | 1560 | if (isset($content) && $content !== '') { |
| 2984 | | // Filter the outputted content and make any last changes before the |
| 2985 | | // content is sent to the browser. The changes are made on $content |
| 2986 | | // which allows the output'ed text to be filtered. |
| 2987 | 1560 | if (isset($elements['#post_render'])) { |
| 2988 | 0 | foreach ($elements['#post_render'] as $function) { |
| 2989 | 0 | if (drupal_function_exists($function)) { |
| 2990 | 0 | $content = $function($content, $elements); |
| 2991 | 0 | } |
| 2992 | 0 | } |
| 2993 | 0 | } |
| 2994 | 1560 | $prefix = isset($elements['#prefix']) ? $elements['#prefix'] : ''; |
| 2995 | 1560 | $suffix = isset($elements['#suffix']) ? $elements['#suffix'] : ''; |
| 2996 | 1560 | return $prefix . $content . $suffix; |
| 2997 | 0 | } |
| 2998 | 713 | } |
| 2999 | | |
| 3000 | | /** |
| 3001 | | * Function used by uasort to sort structured arrays by weight. |
| 3002 | | */ |
| 3003 | 2366 | function element_sort($a, $b) { |
| 3004 | 916 | $a_weight = (is_array($a) && isset($a['#weight'])) ? $a['#weight'] : 0; |
| 3005 | 916 | $b_weight = (is_array($b) && isset($b['#weight'])) ? $b['#weight'] : 0; |
| 3006 | 916 | if ($a_weight == $b_weight) { |
| 3007 | 913 | return 0; |
| 3008 | 0 | } |
| 3009 | 761 | return ($a_weight < $b_weight) ? -1 : 1; |
| 3010 | 0 | } |
| 3011 | | |
| 3012 | | /** |
| 3013 | | * Check if the key is a property. |
| 3014 | | */ |
| 3015 | 2366 | function element_property($key) { |
| 3016 | 0 | return $key[0] == '#'; |
| 3017 | 0 | } |
| 3018 | | |
| 3019 | | /** |
| 3020 | | * Get properties of a structured array element. Properties begin with '#'. |
| 3021 | | */ |
| 3022 | 2366 | function element_properties($element) { |
| 3023 | 0 | return array_filter(array_keys((array) $element), 'element_property'); |
| 3024 | 0 | } |
| 3025 | | |
| 3026 | | /** |
| 3027 | | * Check if the key is a child. |
| 3028 | | */ |
| 3029 | 2366 | function element_child($key) { |
| 3030 | 2020 | return !isset($key[0]) || $key[0] != '#'; |
| 3031 | 0 | } |
| 3032 | | |
| 3033 | | /** |
| 3034 | | * Get keys of a structured array tree element that are not properties
(i.e., do not begin with '#'). |
| 3035 | | */ |
| 3036 | 2366 | function element_children($element) { |
| 3037 | 2020 | return array_filter(array_keys((array) $element), 'element_child'); |
| 3038 | 0 | } |
| 3039 | | |
| 3040 | | /** |
| 3041 | | * Provide theme registration for themes across .inc files. |
| 3042 | | */ |
| 3043 | 2366 | function drupal_common_theme() { |
| 3044 | | return array( |
| 3045 | | // theme.inc |
| 3046 | | 'placeholder' => array( |
| 3047 | 178 | 'arguments' => array('text' => NULL) |
| 3048 | 178 | ), |
| 3049 | | 'page' => array( |
| 3050 | 178 | 'arguments' => array('content' => NULL, 'show_blocks' => TRUE,
'show_messages' => TRUE), |
| 3051 | 178 | 'template' => 'page', |
| 3052 | 178 | ), |
| 3053 | | 'maintenance_page' => array( |
| 3054 | 178 | 'arguments' => array('content' => NULL, 'show_blocks' => TRUE,
'show_messages' => TRUE), |
| 3055 | 178 | 'template' => 'maintenance-page', |
| 3056 | 178 | ), |
| 3057 | | 'update_page' => array( |
| 3058 | 178 | 'arguments' => array('content' => NULL, 'show_messages' => TRUE), |
| 3059 | 178 | ), |
| 3060 | | 'install_page' => array( |
| 3061 | 178 | 'arguments' => array('content' => NULL), |
| 3062 | 178 | ), |
| 3063 | | 'task_list' => array( |
| 3064 | 178 | 'arguments' => array('items' => NULL, 'active' => NULL), |
| 3065 | 178 | ), |
| 3066 | | 'status_messages' => array( |
| 3067 | 178 | 'arguments' => array('display' => NULL), |
| 3068 | 178 | ), |
| 3069 | | 'links' => array( |
| 3070 | 178 | 'arguments' => array('links' => NULL, 'attributes' => array('class'
=> 'links')), |
| 3071 | 178 | ), |
| 3072 | | 'image' => array( |
| 3073 | 178 | 'arguments' => array('path' => NULL, 'alt' => '', 'title' => '',
'attributes' => NULL, 'getsize' => TRUE), |
| 3074 | 178 | ), |
| 3075 | | 'breadcrumb' => array( |
| 3076 | 178 | 'arguments' => array('breadcrumb' => NULL), |
| 3077 | 178 | ), |
| 3078 | | 'help' => array( |
| 3079 | 178 | 'arguments' => array(), |
| 3080 | 178 | ), |
| 3081 | | 'submenu' => array( |
| 3082 | 178 | 'arguments' => array('links' => NULL), |
| 3083 | 178 | ), |
| 3084 | | 'table' => array( |
| 3085 | 178 | 'arguments' => array('header' => NULL, 'rows' => NULL, 'attributes'
=> array(), 'caption' => NULL), |
| 3086 | 178 | ), |
| 3087 | | 'table_select_header_cell' => array( |
| 3088 | 178 | 'arguments' => array(), |
| 3089 | 178 | ), |
| 3090 | | 'tablesort_indicator' => array( |
| 3091 | 178 | 'arguments' => array('style' => NULL), |
| 3092 | 178 | ), |
| 3093 | | 'box' => array( |
| 3094 | 178 | 'arguments' => array('title' => NULL, 'content' => NULL, 'region' =>
'main'), |
| 3095 | 178 | 'template' => 'box', |
| 3096 | 178 | ), |
| 3097 | | 'block' => array( |
| 3098 | 178 | 'arguments' => array('block' => NULL), |
| 3099 | 178 | 'template' => 'block', |
| 3100 | 178 | ), |
| 3101 | | 'mark' => array( |
| 3102 | 178 | 'arguments' => array('type' => MARK_NEW), |
| 3103 | 178 | ), |
| 3104 | | 'item_list' => array( |
| 3105 | 178 | 'arguments' => array('items' => array(), 'title' => NULL, 'type' =>
'ul', 'attributes' => NULL), |
| 3106 | 178 | ), |
| 3107 | | 'more_help_link' => array( |
| 3108 | 178 | 'arguments' => array('url' => NULL), |
| 3109 | 178 | ), |
| 3110 | | 'xml_icon' => array( |
| 3111 | 178 | 'arguments' => array('url' => NULL), |
| 3112 | 178 | ), |
| 3113 | | 'feed_icon' => array( |
| 3114 | 178 | 'arguments' => array('url' => NULL, 'title' => NULL), |
| 3115 | 178 | ), |
| 3116 | | 'more_link' => array( |
| 3117 | 178 | 'arguments' => array('url' => NULL, 'title' => NULL) |
| 3118 | 178 | ), |
| 3119 | | 'closure' => array( |
| 3120 | 178 | 'arguments' => array('main' => 0), |
| 3121 | 178 | ), |
| 3122 | | 'blocks' => array( |
| 3123 | 178 | 'arguments' => array('region' => NULL), |
| 3124 | 178 | ), |
| 3125 | | 'username' => array( |
| 3126 | 178 | 'arguments' => array('object' => NULL), |
| 3127 | 178 | ), |
| 3128 | | 'progress_bar' => array( |
| 3129 | 178 | 'arguments' => array('percent' => NULL, 'message' => NULL), |
| 3130 | 178 | ), |
| 3131 | | 'indentation' => array( |
| 3132 | 178 | 'arguments' => array('size' => 1), |
| 3133 | 178 | ), |
| 3134 | | // from pager.inc |
| 3135 | | 'pager' => array( |
| 3136 | 178 | 'arguments' => array('tags' => array(), 'limit' => 10, 'element' =>
0, 'parameters' => array()), |
| 3137 | 178 | ), |
| 3138 | | 'pager_first' => array( |
| 3139 | 178 | 'arguments' => array('text' => NULL, 'limit' => NULL, 'element' => 0,
'parameters' => array()), |
| 3140 | 178 | ), |
| 3141 | | 'pager_previous' => array( |
| 3142 | 178 | 'arguments' => array('text' => NULL, 'limit' => NULL, 'element' => 0,
'interval' => 1, 'parameters' => array()), |
| 3143 | 178 | ), |
| 3144 | | 'pager_next' => array( |
| 3145 | 178 | 'arguments' => array('text' => NULL, 'limit' => NULL, 'element' => 0,
'interval' => 1, 'parameters' => array()), |
| 3146 | 178 | ), |
| 3147 | | 'pager_last' => array( |
| 3148 | 178 | 'arguments' => array('text' => NULL, 'limit' => NULL, 'element' => 0,
'parameters' => array()), |
| 3149 | 178 | ), |
| 3150 | | 'pager_link' => array( |
| 3151 | 178 | 'arguments' => array('text' => NULL, 'page_new' => NULL, 'element' =>
NULL, 'parameters' => array(), 'attributes' => array()), |
| 3152 | 178 | ), |
| 3153 | | // from locale.inc |
| 3154 | | 'locale_admin_manage_screen' => array( |
| 3155 | 178 | 'arguments' => array('form' => NULL), |
| 3156 | 178 | ), |
| 3157 | | // from menu.inc |
| 3158 | | 'menu_item_link' => array( |
| 3159 | 178 | 'arguments' => array('item' => NULL), |
| 3160 | 178 | ), |
| 3161 | | 'menu_tree' => array( |
| 3162 | 178 | 'arguments' => array('tree' => NULL), |
| 3163 | 178 | ), |
| 3164 | | 'menu_item' => array( |
| 3165 | 178 | 'arguments' => array('link' => NULL, 'has_children' => NULL, 'menu'
=> ''), |
| 3166 | 178 | ), |
| 3167 | | 'menu_local_task' => array( |
| 3168 | 178 | 'arguments' => array('link' => NULL, 'active' => FALSE), |
| 3169 | 178 | ), |
| 3170 | | 'menu_local_tasks' => array( |
| 3171 | 178 | 'arguments' => array(), |
| 3172 | 178 | ), |
| 3173 | | // from form.inc |
| 3174 | | 'select' => array( |
| 3175 | 178 | 'arguments' => array('element' => NULL), |
| 3176 | 178 | ), |
| 3177 | | 'fieldset' => array( |
| 3178 | 178 | 'arguments' => array('element' => NULL), |
| 3179 | 178 | ), |
| 3180 | | 'radio' => array( |
| 3181 | 178 | 'arguments' => array('element' => NULL), |
| 3182 | 178 | ), |
| 3183 | | 'radios' => array( |
| 3184 | 178 | 'arguments' => array('element' => NULL), |
| 3185 | 178 | ), |
| 3186 | | 'password_confirm' => array( |
| 3187 | 178 | 'arguments' => array('element' => NULL), |
| 3188 | 178 | ), |
| 3189 | | 'date' => array( |
| 3190 | 178 | 'arguments' => array('element' => NULL), |
| 3191 | 178 | ), |
| 3192 | | 'item' => array( |
| 3193 | 178 | 'arguments' => array('element' => NULL), |
| 3194 | 178 | ), |
| 3195 | | 'checkbox' => array( |
| 3196 | 178 | 'arguments' => array('element' => NULL), |
| 3197 | 178 | ), |
| 3198 | | 'checkboxes' => array( |
| 3199 | 178 | 'arguments' => array('element' => NULL), |
| 3200 | 178 | ), |
| 3201 | | 'submit' => array( |
| 3202 | 178 | 'arguments' => array('element' => NULL), |
| 3203 | 178 | ), |
| 3204 | | 'button' => array( |
| 3205 | 178 | 'arguments' => array('element' => NULL), |
| 3206 | 178 | ), |
| 3207 | | 'image_button' => array( |
| 3208 | 178 | 'arguments' => array('element' => NULL), |
| 3209 | 178 | ), |
| 3210 | | 'hidden' => array( |
| 3211 | 178 | 'arguments' => array('element' => NULL), |
| 3212 | 178 | ), |
| 3213 | | 'token' => array( |
| 3214 | 178 | 'arguments' => array('element' => NULL), |
| 3215 | 178 | ), |
| 3216 | | 'textfield' => array( |
| 3217 | 178 | 'arguments' => array('element' => NULL), |
| 3218 | 178 | ), |
| 3219 | | 'form' => array( |
| 3220 | 178 | 'arguments' => array('element' => NULL), |
| 3221 | 178 | ), |
| 3222 | | 'textarea' => array( |
| 3223 | 178 | 'arguments' => array('element' => NULL), |
| 3224 | 178 | ), |
| 3225 | | 'markup' => array( |
| 3226 | 178 | 'arguments' => array('element' => NULL), |
| 3227 | 178 | ), |
| 3228 | | 'password' => array( |
| 3229 | 178 | 'arguments' => array('element' => NULL), |
| 3230 | 178 | ), |
| 3231 | | 'file' => array( |
| 3232 | 178 | 'arguments' => array('element' => NULL), |
| 3233 | 178 | ), |
| 3234 | | 'form_element' => array( |
| 3235 | 178 | 'arguments' => array('element' => NULL, 'value' => NULL), |
| 3236 | 178 | ), |
| 3237 | 178 | ); |
| 3238 | 0 | } |
| 3239 | | |
| 3240 | | /** |
| 3241 | | * @ingroup schemaapi |
| 3242 | | * @{ |
| 3243 | | */ |
| 3244 | | |
| 3245 | | /** |
| 3246 | | * Create all tables that a module defines in its hook_schema(). |
| 3247 | | * |
| 3248 | | * Note: This function does not pass the module's schema through |
| 3249 | | * hook_schema_alter(). The module's tables will be created exactly as the |
| 3250 | | * module defines them. |
| 3251 | | * |
| 3252 | | * @param $module |
| 3253 | | * The module for which the tables will be created. |
| 3254 | | * @return |
| 3255 | | * An array of arrays with the following key/value pairs: |
| 3256 | | * - success: a boolean indicating whether the query succeeded. |
| 3257 | | * - query: the SQL query(s) executed, passed through check_plain(). |
| 3258 | | */ |
| 3259 | 2366 | function drupal_install_schema($module) { |
| 3260 | 136 | $schema = drupal_get_schema_unprocessed($module); |
| 3261 | 136 | _drupal_initialize_schema($module, $schema); |
| 3262 | | |
| 3263 | 136 | $ret = array(); |
| 3264 | 136 | foreach ($schema as $name => $table) { |
| 3265 | 136 | db_create_table($ret, $name, $table); |
| 3266 | 136 | } |
| 3267 | 136 | return $ret; |
| 3268 | 0 | } |
| 3269 | | |
| 3270 | | /** |
| 3271 | | * Remove all tables that a module defines in its hook_schema(). |
| 3272 | | * |
| 3273 | | * Note: This function does not pass the module's schema through |
| 3274 | | * hook_schema_alter(). The module's tables will be created exactly as the |
| 3275 | | * module defines them. |
| 3276 | | * |
| 3277 | | * @param $module |
| 3278 | | * The module for which the tables will be removed. |
| 3279 | | * @return |
| 3280 | | * An array of arrays with the following key/value pairs: |
| 3281 | | * - success: a boolean indicating whether the query succeeded. |
| 3282 | | * - query: the SQL query(s) executed, passed through check_plain(). |
| 3283 | | */ |
| 3284 | 2366 | function drupal_uninstall_schema($module) { |
| 3285 | 1 | $schema = drupal_get_schema_unprocessed($module); |
| 3286 | 1 | _drupal_initialize_schema($module, $schema); |
| 3287 | | |
| 3288 | 1 | $ret = array(); |
| 3289 | 1 | foreach ($schema as $table) { |
| 3290 | 1 | if (db_table_exists($table['name'])) { |
| 3291 | 1 | db_drop_table($ret, $table['name']); |
| 3292 | 1 | } |
| 3293 | 1 | } |
| 3294 | 1 | return $ret; |
| 3295 | 0 | } |
| 3296 | | |
| 3297 | | /** |
| 3298 | | * Returns the unprocessed and unaltered version of a module's schema. |
| 3299 | | * |
| 3300 | | * Use this function only if you explicitly need the original |
| 3301 | | * specification of a schema, as it was defined in a module's |
| 3302 | | * hook_schema(). No additional default values will be set, |
| 3303 | | * hook_schema_alter() is not invoked and these unprocessed |
| 3304 | | * definitions won't be cached. |
| 3305 | | * |
| 3306 | | * This function can be used to retrieve a schema specification in |
| 3307 | | * hook_schema(), so it allows you to derive your tables from existing |
| 3308 | | * specifications. |
| 3309 | | * |
| 3310 | | * It is also used by drupal_install_schema() and |
| 3311 | | * drupal_uninstall_schema() to ensure that a module's tables are |
| 3312 | | * created exactly as specified without any changes introduced by a |
| 3313 | | * module that implements hook_schema_alter(). |
| 3314 | | * |
| 3315 | | * @param $module |
| 3316 | | * The module to which the table belongs. |
| 3317 | | * @param $table |
| 3318 | | * The name of the table. If not given, the module's complete schema |
| 3319 | | * is returned. |
| 3320 | | */ |
| 3321 | 2366 | function drupal_get_schema_unprocessed($module, $table = NULL) { |
| 3322 | | // Load the .install file to get hook_schema. |
| 3323 | 137 | module_load_install($module); |
| 3324 | 137 | $schema = module_invoke($module, 'schema'); |
| 3325 | | |
| 3326 | 137 | if (!is_null($table) && isset($schema[$table])) { |
| 3327 | 134 | return $schema[$table]; |
| 3328 | 0 | } |
| 3329 | | else { |
| 3330 | 137 | return $schema; |
| 3331 | | } |
| 3332 | 0 | } |
| 3333 | | |
| 3334 | | /** |
| 3335 | | * Fill in required default values for table definitions returned by
hook_schema(). |
| 3336 | | * |
| 3337 | | * @param $module |
| 3338 | | * The module for which hook_schema() was invoked. |
| 3339 | | * @param $schema |
| 3340 | | * The schema definition array as it was returned by the module's |
| 3341 | | * hook_schema(). |
| 3342 | | */ |
| 3343 | 2366 | function _drupal_initialize_schema($module, &$schema) { |
| 3344 | | // Set the name and module key for all tables. |
| 3345 | 137 | foreach ($schema as $name => $table) { |
| 3346 | 137 | if (empty($table['module'])) { |
| 3347 | 137 | $schema[$name]['module'] = $module; |
| 3348 | 137 | } |
| 3349 | 137 | if (!isset($table['name'])) { |
| 3350 | 137 | $schema[$name]['name'] = $name; |
| 3351 | 137 | } |
| 3352 | 137 | } |
| 3353 | 137 | } |
| 3354 | | |
| 3355 | | /** |
| 3356 | | * Retrieve a list of fields from a table schema. The list is suitable for
use in a SQL query. |
| 3357 | | * |
| 3358 | | * @param $table |
| 3359 | | * The name of the table from which to retrieve fields. |
| 3360 | | * @param |
| 3361 | | * An optional prefix to to all fields. |
| 3362 | | * |
| 3363 | | * @return An array of fields. |
| 3364 | | **/ |
| 3365 | 2366 | function drupal_schema_fields_sql($table, $prefix = NULL) { |
| 3366 | 441 | $schema = drupal_get_schema($table); |
| 3367 | 441 | $fields = array_keys($schema['fields']); |
| 3368 | 441 | if ($prefix) { |
| 3369 | 441 | $columns = array(); |
| 3370 | 441 | foreach ($fields as $field) { |
| 3371 | 441 | $columns[] = "$prefix.$field"; |
| 3372 | 441 | } |
| 3373 | 441 | return $columns; |
| 3374 | 0 | } |
| 3375 | | else { |
| 3376 | 0 | return $fields; |
| 3377 | | } |
| 3378 | 0 | } |
| 3379 | | |
| 3380 | | /** |
| 3381 | | * Save a record to the database based upon the schema. |
| 3382 | | * |
| 3383 | | * Default values are filled in for missing items, and 'serial' (auto
increment) |
| 3384 | | * types are filled in with IDs. |
| 3385 | | * |
| 3386 | | * @param $table |
| 3387 | | * The name of the table; this must exist in schema API. |
| 3388 | | * @param $object |
| 3389 | | * The object to write. This is a reference, as defaults according to |
| 3390 | | * the schema may be filled in on the object, as well as ID on the serial |
| 3391 | | * type(s). Both array an object types may be passed. |
| 3392 | | * @param $primary_keys |
| 3393 | | * If this is an update, specify the primary keys' field names. It is the |
| 3394 | | * caller's responsibility to know if a record for this object already |
| 3395 | | * exists in the database. If there is only 1 key, you may pass a simple
string. |
| 3396 | | * @return |
| 3397 | | * Failure to write a record will return FALSE. Otherwise SAVED_NEW or |
| 3398 | | * SAVED_UPDATED is returned depending on the operation performed. The |
| 3399 | | * $object parameter contains values for any serial fields defined by |
| 3400 | | * the $table. For example, $object->nid will be populated after
inserting |
| 3401 | | * a new node. |
| 3402 | | */ |
| 3403 | 2366 | function drupal_write_record($table, &$object, $primary_keys = array()) { |
| 3404 | | // Standardize $primary_keys to an array. |
| 3405 | 203 | if (is_string($primary_keys)) { |
| 3406 | 58 | $primary_keys = array($primary_keys); |
| 3407 | 58 | } |
| 3408 | | |
| 3409 | 203 | $schema = drupal_get_schema($table); |
| 3410 | 203 | if (empty($schema)) { |
| 3411 | 0 | return FALSE; |
| 3412 | 0 | } |
| 3413 | | |
| 3414 | | // Convert to an object if needed. |
| 3415 | 203 | if (is_array($object)) { |
| 3416 | 116 | $object = (object) $object; |
| 3417 | 116 | $array = TRUE; |
| 3418 | 116 | } |
| 3419 | | else { |
| 3420 | 106 | $array = FALSE; |
| 3421 | | } |
| 3422 | | |
| 3423 | 203 | $fields = array(); |
| 3424 | | |
| 3425 | | // Go through our schema, build SQL, and when inserting, fill in defaults
for |
| 3426 | | // fields that are not set. |
| 3427 | 203 | foreach ($schema['fields'] as $field => $info) { |
| 3428 | | // Special case -- skip serial types if we are updating. |
| 3429 | 203 | if ($info['type'] == 'serial' && !empty($primary_keys)) { |
| 3430 | 58 | continue; |
| 3431 | 0 | } |
| 3432 | | |
| 3433 | | // For inserts, populate defaults from schema if not already provided. |
| 3434 | 203 | if (!isset($object->$field) && empty($primary_keys) &&
isset($info['default'])) { |
| 3435 | 126 | $object->$field = $info['default']; |
| 3436 | 126 | } |
| 3437 | | |
| 3438 | | // Track serial field so we can helpfully populate them after the
query. |
| 3439 | | // NOTE: Each table should come with one serial field only. |
| 3440 | 203 | if ($info['type'] == 'serial') { |
| 3441 | 162 | $serial = $field; |
| 3442 | | // Ignore values for serial when inserting data. Unsupported. |
| 3443 | 162 | unset($object->$field); |
| 3444 | 162 | } |
| 3445 | | |
| 3446 | | // Build arrays for the fields and values in our query. |
| 3447 | 203 | if (isset($object->$field)) { |
| 3448 | 203 | if (empty($info['serialize'])) { |
| 3449 | 193 | $fields[$field] = $object->$field; |
| 3450 | 193 | } |
| 3451 | 19 | elseif (!empty($object->$field)) { |
| 3452 | 18 | $fields[$field] = serialize($object->$field); |
| 3453 | 18 | } |
| 3454 | | else { |
| 3455 | 1 | $fields[$field] = ''; |
| 3456 | | } |
| 3457 | 203 | } |
| 3458 | | |
| 3459 | | // We don't need to care about type casting if value does not exist. |
| 3460 | 203 | if (!isset($fields[$field])) { |
| 3461 | 200 | continue; |
| 3462 | 0 | } |
| 3463 | | |
| 3464 | | // Special case -- skip null value if field allows null. |
| 3465 | 203 | if ($fields[$field] == NULL && $info['not null'] == FALSE) { |
| 3466 | 84 | continue; |
| 3467 | 0 | } |
| 3468 | | |
| 3469 | | // Type cast if field does not allow null. Required by DB API. |
| 3470 | 203 | if ($info['type'] == 'int' || $info['type'] == 'serial') { |
| 3471 | 192 | $fields[$field] = (int) $fields[$field]; |
| 3472 | 192 | } |
| 3473 | 202 | elseif ($info['type'] == 'float') { |
| 3474 | 0 | $fields[$field] = (float) $fields[$field]; |
| 3475 | 0 | } |
| 3476 | | else { |
| 3477 | 202 | $fields[$field] = (string) $fields[$field]; |
| 3478 | | } |
| 3479 | 203 | } |
| 3480 | | |
| 3481 | 203 | if (empty($fields)) { |
| 3482 | | // |