Newer
Older
/**
* @file
* Common functions that many Drupal modules will need to reference.
*
* The functions that are critical and need to be available even when serving
* a cached page are instead located in bootstrap.inc.
*/
* @param $breadcrumb
* Array of links, starting with "home" and proceeding up to but not including
* the current page.
function drupal_set_breadcrumb($breadcrumb = NULL) {
static $stored_breadcrumb;
if (isset($breadcrumb)) {
$stored_breadcrumb = $breadcrumb;
}
return $stored_breadcrumb;
}
function drupal_get_breadcrumb() {
$breadcrumb = drupal_set_breadcrumb();
if (!isset($breadcrumb)) {
$breadcrumb = menu_get_active_breadcrumb();
}
return $breadcrumb;
}
*/
function drupal_set_html_head($data = NULL) {
/**
* Retrieve output to be displayed in the head tag of the HTML page.
*/
function drupal_get_html_head() {
global $base_url;
$output = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
return $output . drupal_set_html_head();
}
/**
* Regenerate the path map from the information in the database.
*/
*/
function drupal_get_normal_path($path) {
if (($map = drupal_get_path_map()) && isset($map[$path])) {
return $map[$path];
}
return conf_url_rewrite($path, 'incoming');
}
else {
return $path;
}
}
*/
function drupal_set_header($header = NULL) {
// We use an array to guarantee there are no leading or trailing delimiters.
// Otherwise, header('') could get called when serving the page later, which
// ends HTTP headers prematurely on some PHP versions.
static $stored_headers = array();
$stored_headers[] = $header;
return implode("\n", $stored_headers);
function drupal_get_headers() {
return drupal_set_header();
}
/**
* Prepare a destination query string for use in combination with
* drupal_goto(). Used to direct the user back to the referring page
* after completing a form.
*
* @see drupal_goto()
*/
function drupal_get_destination() {
$destination[] = $_GET['q'];
$params = array('from', 'sort', 'order');
foreach ($params as $param) {
if (isset($_GET[$param])) {
$destination[] = "$param=". $_GET[$param];
}
}
return 'destination='. urlencode(implode('&', $destination));
}
* This issues an on-site HTTP redirect. The function makes sure the redirected
* URL is formatted correctly.
* Usually the redirected URL is constructed from this function's input
* parameters. However you may override that behavior by setting a
* <em>destination</em> in either the $_REQUEST-array (i.e. by using
* the query string of an URI) or the $_REQUEST['edit']-array (i.e. by
* using a hidden form field). This is used to direct the user back to
* the proper page after completing a form. For example, after editing
* a post on the 'admin/node'-page or after having logged on using the
* 'user login'-block in a sidebar. The function drupal_get_destination()
* can be used to help set the destination URL.
*
* It is advised to use drupal_goto() instead of PHP's header(), because
* drupal_goto() will append the user's session ID to the URI when PHP is
* compiled with "--enable-trans-sid".
*
* This function ends the request; use it rather than a print theme('page')
* statement in your menu callback.
*
* @param $path
* A Drupal path.
* @param $query
* The query string component, if any.
* @param $fragment
* The destination fragment identifier (named anchor).
*
* @see drupal_get_destination()
if ($_REQUEST['destination']) {
extract(parse_url($_REQUEST['destination']));
}
else if ($_REQUEST['edit']['destination']) {
extract(parse_url($_REQUEST['edit']['destination']));
}
$url = url($path, $query, $fragment, TRUE);
if (ini_get('session.use_trans_sid') && session_id() && !strstr($url, session_id())) {
$sid = session_name() . '=' . session_id();
if (strstr($url, '?') && !strstr($url, $sid)) {
$url = $url .'&'. $sid;
// Before the redirect, allow modules to react to the end of the page request.
module_invoke_all('exit', $url);
header('Location: '. $url);
// The "Location" header sends a REDIRECT status code to the http
// daemon. In some cases this can go wrong, so we make sure none
// of the code below the drupal_goto() call gets executed when we redirect.
exit();
}
/**
* Generates a 404 error if the request can not be handled.
*/
drupal_set_header('HTTP/1.0 404 Not Found');
watchdog('page not found', t('%page not found.', array('%page' => theme('placeholder', $_GET['q']))), WATCHDOG_WARNING);
if ($path && $path != $_GET['q']) {
drupal_set_title(t('Page not found'));
print theme('page', '');
/**
* Generates a 403 error if the request is not allowed.
*/
function drupal_access_denied() {
drupal_set_header('HTTP/1.0 403 Forbidden');
watchdog('access denied', t('%page denied access.', array('%page' => theme('placeholder', $_GET['q']))), WATCHDOG_WARNING, l(t('view'), $_GET['q']));
if ($path && $path != $_GET['q']) {
menu_set_active_item($path);
$status = menu_execute_active_handler();
}
if ($status != MENU_FOUND) {
drupal_set_title(t('Access denied'));
print theme('page', message_access());
* This is a flexible and powerful HTTP client implementation. Correctly handles
* GET, POST, PUT or any other HTTP requests. Handles redirects.
*
* @param $url
* A string containing a fully qualified URI.
* @param $headers
* An array containing an HTTP header => value pair.
* @param $method
* A string defining the HTTP request to use.
* @param $data
* A string containing data to include in the request.
* @param $retry
* An integer representing how many times to retry the request in case of a
* redirect.
* @return
* An object containing the HTTP request headers, response code, headers,
* data, and redirect status.
*/
function drupal_http_request($url, $headers = array(), $method = 'GET', $data = NULL, $retry = 3) {
$uri = parse_url($url);
switch ($uri['scheme']) {
case 'http':
$fp = @fsockopen($uri['host'], ($uri['port'] ? $uri['port'] : 80), $errno, $errstr, 15);
break;
case 'https':
// Note: Only works for PHP 4.3 compiled with OpenSSL.
$fp = @fsockopen('ssl://'. $uri['host'], ($uri['port'] ? $uri['port'] : 443), $errno, $errstr, 20);
'User-Agent' => 'User-Agent: Drupal (+http://www.drupal.org/)',
'Content-Length' => 'Content-Length: '. strlen($data)
$request .= implode("\r\n", $defaults);
$request .= "\r\n\r\n";
if ($data) {
}
$result->request = $request;
fwrite($fp, $request);
// Fetch response.
while (!feof($fp) && $data = fread($fp, 1024)) {
list($headers, $result->data) = explode("\r\n\r\n", $response, 2);
$headers = preg_split("/\r\n|\n|\r/", $headers);
list($protocol, $code, $text) = explode(' ', trim(array_shift($headers)), 3);
while ($line = trim(array_shift($headers))) {
list($header, $value) = explode(':', $line, 2);
$result->headers[$header] = trim($value);
}
$responses = array(
100 => 'Continue', 101 => 'Switching Protocols',
200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content',
300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect',
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',
500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported'
);
// RFC 2616 states that all unknown HTTP codes must be treated the same as
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
if (!isset($responses[$code])) {
$code = floor($code / 100) * 100;
}
switch ($code) {
case 200: // OK
case 304: // Not modified
break;
case 301: // Moved permanently
case 302: // Moved temporarily
case 307: // Moved temporarily
$location = $result->headers['Location'];
if ($retry) {
$result = drupal_http_request($result->headers['Location'], $headers, $method, $data, --$retry);
$result->redirect_code = $result->code;
}
$result->redirect_url = $location;
break;
default:
$result->error = $text;
}
$result->code = $code;
return $result;
}
* Log errors as defined by administrator
* Error levels:
* 1 = Log errors to database.
* 2 = Log errors to database and to screen.
Dries Buytaert
committed
if ($errno & (E_ALL ^ E_NOTICE)) {
$types = array(1 => 'error', 2 => 'warning', 4 => 'parse error', 8 => 'notice', 16 => 'core error', 32 => 'core warning', 64 => 'compile error', 128 => 'compile warning', 256 => 'user error', 512 => 'user warning', 1024 => 'user notice', 2048 => 'strict warning');
$entry = $types[$errno] .': '. $message .' in '. $filename .' on line '. $line .'.';
watchdog('php', t('%message in %file on line %line.', array('%error' => $types[$errno], '%message' => $message, '%file' => $filename, '%line' => $line)), WATCHDOG_ERROR);
array_walk($item, '_fix_gpc_magic');
}
else {
/**
* Correct double-escaping problems caused by "magic quotes" in some PHP
* installations.
*/
array_walk($_GET, '_fix_gpc_magic');
array_walk($_POST, '_fix_gpc_magic');
array_walk($_COOKIE, '_fix_gpc_magic');
array_walk($_REQUEST, '_fix_gpc_magic');
$fixed = true;
}
/**
* @name Conversion
* @{
/**
* Convert an associative array to an anonymous object.
*/
function array2object($array) {
if (is_array($array)) {
function object2array($object) {
if (is_object($object)) {
foreach ($object as $key => $value) {
/**
* Return a string with an "access denied" message.
*
* Always consider whether to use drupal_access_denied() instead to return a
* proper (and customizable) 403 error.
*/
if (function_exists('i18n_get_lang')) {
return i18n_get_lang();
}
if (function_exists('locale')) {
$languages = locale_supported_languages();
$languages = $languages['name'];
}
else {
// Ensure the locale/language is correctly returned, even without locale.module.
// Useful for e.g. XML/HTML 'lang' attributes.
$languages = array('en' => 'English');
if ($user->uid && $languages[$user->language]) {
return $user->language;
}
else {
return key($languages);
}
* When using t(), try to put entire sentences and strings in one t() call.
* This makes it easier for translators. HTML markup within translation strings
* is acceptable, if necessary. The suggested syntax for a link embedded
* within a translation string is:
* @code
* $msg = t('You must log in below or <a href="%url">create a new
* account</a> before viewing the next page.', array('%url'
* => url('user/register')));
* We suggest the same syntax for links to other sites. This makes it easy to
* change link URLs if needed (which happens often) without requiring updates
* to translations.
* @param $args
* An associative array of replacements to make after translation. Incidences
global $locale;
if (function_exists('locale') && $locale != 'en') {
$string = locale($string);
}
Dries Buytaert
committed
Dries Buytaert
committed
/**
* Verify the syntax of the given e-mail address.
*
* Empty e-mail addresses are allowed. See RFC 2822 for details.
Dries Buytaert
committed
*
Dries Buytaert
committed
*/
Dries Buytaert
committed
$user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+';
Dries Buytaert
committed
$domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.?)+';
Dries Buytaert
committed
$ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
$ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';
return preg_match("/^$user@($domain|(\[($ipv4|$ipv6)\]))$/", $mail);
Dries Buytaert
committed
}
* Whether the URL is absolute (beginning with a scheme such as "http:").
$allowed_characters = '[a-z0-9\/:_\-_\.\?\$,~=#&%\+]';
Dries Buytaert
committed
if ($absolute) {
return preg_match("/^(http|https|ftp):\/\/". $allowed_characters ."+$/i", $url);
Dries Buytaert
committed
}
else {
return preg_match("/^". $allowed_characters ."+$/i", $url);
Dries Buytaert
committed
}
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/**
* Register an event for the current visitor (hostname/IP) to the flood control mechanism.
*
* @param $name
* The name of the event.
*/
function flood_register_event($name) {
db_query("INSERT INTO {flood} (event, hostname, timestamp) VALUES ('%s', '%s', %d)", $name, $_SERVER['REMOTE_ADDR'], time());
}
/**
* Check if the current visitor (hostname/IP) is allowed to proceed with the specified event.
* The user is allowed to proceed if he did not trigger the specified event more than
* $threshold times per hour.
*
* @param $name
* The name of the event.
* @param $number
* The maximum number of the specified event per hour (per visitor).
* @return
* True if the user did not exceed the hourly threshold. False otherwise.
*/
function flood_is_allowed($name, $threshold) {
$number = db_num_rows(db_query("SELECT event FROM {flood} WHERE event = '%s' AND hostname = '%s' AND timestamp > %d", $name, $_SERVER['REMOTE_ADDR'], time() - 3600));
return ($number < $threshold ? TRUE : FALSE);
}
Kjartan Mannes
committed
function check_file($filename) {
return is_uploaded_file($filename);
/**
* Prepare a URL for use in an HTML attribute. Strips harmful protocols.
*
*/
function check_url($uri) {
return filter_xss_bad_protocol($uri, FALSE);
/**
* Formats an RSS channel.
*
* Arbitrary elements may be added using the $args associative array.
*/
function format_rss_channel($title, $link, $description, $items, $language = 'en', $args = array()) {
// arbitrary elements may be added using the $args associative array
$output .= ' <title>'. check_plain($title) ."</title>\n";
$output .= ' <link>'. check_url($link) ."</link>\n";
$output .= ' <description>'. check_plain($description) ."</description>\n";
$output .= ' <language>'. check_plain($language) ."</language>\n";
$output .= ' <'. $key .'>'. check_plain($value) ."</$key>\n";
$output .= $items;
$output .= "</channel>\n";
return $output;
}
/**
* Format a single RSS item.
*
* Arbitrary elements may be added using the $args associative array.
*/
function format_rss_item($title, $link, $description, $args = array()) {
$output .= ' <title>'. check_plain($title) ."</title>\n";
$output .= ' <link>'. check_url($link) ."</link>\n";
$output .= ' <description>'. check_plain($description) ."</description>\n";
if (is_array($value)) {
if ($value['key']) {
$output .= ' <'. $value['key'];
if (is_array($value['attributes'])) {
$output .= drupal_attributes($value['attributes']);
}
if ($value['value']) {
$output .= '>'. $value['value'] .'</'. $value['key'] .">\n";
}
else {
$output .= " />\n";
}
}
}
else {
$output .= ' <'. $key .'>'. check_plain($value) ."</$key>\n";
* This function ensures that the string is pluralized correctly. Since t() is
* called by this function, make sure not to pass already-localized strings to it.
*
* @param $count
* The item count to display.
* @param $singular
* The string for the singular case. Please make sure it is clear this is
* singular, to ease translation (e.g. use "1 new comment" instead of "1 new").
* @param $plural
* The string for the plural case. Please make sure it is clear this is plural,
* to ease translation. Use %count in place of the item count, as in "%count
* new comments".
* @return
* A translated string.
if ($count == 1) return t($singular, array("%count" => $count));
// get the plural index through the gettext formula
$index = (function_exists('locale')) ? locale_get_plural($count) : -1;
if ($index < 0) { // backward compatibility
return t($plural, array("%count" => $count));
}
else {
switch ($index) {
case "0":
return t($singular, array("%count" => $count));
case "1":
return t($plural, array("%count" => $count));
default:
return t(strtr($plural, array("%count" => '%count['. $index .']')), array('%count['. $index .']' => $count));
}
}
* @param $size
* The size in bytes.
* @return
* A translated string representation of the size.
return t('%size %suffix', array('%size' => $size, '%suffix' => $suffix));
* @param $timestamp
* The length of the interval in seconds.
* @param $granularity
* How many different units to display in the string.
* @return
* A translated string representation of the interval.
$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);
$output .= ($output ? ' ' : '') . format_plural(floor($timestamp / $value), $key[0], $key[1]);
* Format a date with the given configured format or a custom format string.
*
* Drupal allows administrators to select formatting strings for 'small',
* 'medium' and 'large' date formats. This function can handle these formats,
* as well as any custom format.
*
* @param $timestamp
* The exact date to format, as a UNIX timestamp.
* @param $type
* The format to use. Can be "small", "medium" or "large" for the preconfigured
* date formats. If "custom" is specified, then $format is required as well.
* @param $format
Dries Buytaert
committed
* A PHP date format string as required by date(). A backslash should be used
* before a character to avoid interpreting the character as part of a date
* format.
* @param $timezone
* Time zone offset in seconds; if omitted, the user's time zone is used.
* @return
* A translated date string in the requested format.
function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL) {
if ($timezone === NULL) {
global $user;
if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
$timezone = $user->timezone;
}
else {
$timezone = variable_get('date_default_timezone', 0);
}
$timestamp += $timezone;
case 'small':
$format = variable_get('date_format_short', 'm/d/Y - H:i');
case 'large':
$format = variable_get('date_format_long', 'l, F j, Y - H:i');
$format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
$max = strlen($format);
Steven Wittens
committed
if (strpos('AaDFlM', $c) !== false) {
$date .= t(gmdate($c, $timestamp));
Steven Wittens
committed
else if (strpos('BdgGhHiIjLmnsStTUwWYyz', $c) !== false) {
$date .= gmdate($c, $timestamp);
}
else if ($c == 'r') {
$date .= format_date($timestamp - $timezone, 'custom', 'D, d M Y H:i:s O', $timezone);
else if ($c == 'O') {
$date .= sprintf('%s%02d%02d', ($timezone < 0 ? '-' : '+'), abs($timezone / 3600), abs($timezone % 3600) / 60);
}
else if ($c == 'Z') {
$date .= $timezone;
Dries Buytaert
committed
else if ($c == '\\') {
$date .= $format[++$i];
}
* @param $object
* The user object to format, usually returned from user_load().
* @return
* A string containing an HTML link to the user's page if the passed object
* suggests that this is a site user. Otherwise, only the username is returned.
function format_name($object) {
if ($object->uid && $object->name) {
if (strlen($object->name) > 20) {
}
else {
$name = $object->name;
}
if (user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
$output = $name;
}
// Sometimes modules display content composed by people who are
// not registered members of the site (e.g. mailing list or news
// aggregator modules). This clause enables modules to display
// the true author of the content.
}
else {
$output = $object->name;
}
$output .= ' ('. t('not verified') .')';
$output = variable_get('anonymous', 'Anonymous');
* Drupal uses these functions to achieve consistency in its form presentation,
* while at the same time simplifying code and reducing the amount of HTML that
Dries Buytaert
committed
* must be explicitly generated by modules.
/**
* Generate a form from a set of form elements.
*
* @param $form
* An HTML string containing one or more form elements.
* @param $method
* The query method to use ("post" or "get").
* @param $action
* The URL to send the form contents to, if not the current page.
* @param $attributes
* An associative array of attributes to add to the form tag.
* @result
* An HTML string with the contents of $form wrapped in a form tag.
*/
function form($form, $method = 'post', $action = NULL, $attributes = NULL) {
$action = request_uri();
return '<form action="'. check_url($action) .'" method="'. $method .'"'. drupal_attributes($attributes) .">\n". $form ."\n</form>\n";
*/
function form_set_error($name, $message) {
$GLOBALS['form'][$name] = $message;
drupal_set_message($message, 'error');
}
/**
if (array_key_exists('form', $GLOBALS)) {
return $GLOBALS['form'];
}
}
/**
* Return the error message filed against the form with the specified name.
*/
function _form_get_error($name) {
if (array_key_exists('form', $GLOBALS)) {
return $GLOBALS['form'][$name];
}
}
function _form_get_class($name, $required, $error) {
return $name. ($required ? ' required' : '') . ($error ? ' error' : '');
}
/**
* Format a general form item.
*
* @param $title
* The label for the form item.
* @param $value
* The contents of the form item.
* @param $description
* Explanatory text to display after the form item.
* @param $id
* A unique identifier for the form item.
* @param $required
* Whether the user must fill in this form element before submitting the form.
* @param $error
* An error message to display alongside the form element.
* @return
* A themed HTML string representing the form item.
*/
function form_item($title, $value, $description = NULL, $id = NULL, $required = FALSE, $error = FALSE) {
return theme('form_element', $title, $value, $description, $id, $required, $error);