Skip to content
common.inc 59.1 KiB
Newer Older
Dries Buytaert's avatar
 
Dries Buytaert committed
<?php
Dries Buytaert's avatar
Dries Buytaert committed
/* $Id$ */
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * @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.
 */

Dries Buytaert's avatar
 
Dries Buytaert committed
 * Set the title of the current page, for display on the page and in the title bar.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function drupal_set_title($title = NULL) {
  static $stored_title;

  if (isset($title)) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $stored_title = $title;
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
  return $stored_title;
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Get the title of the current page, for display on the page and in the title bar.
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function drupal_get_title() {
  $title = drupal_set_title();

  if (!isset($title)) {
    $title = menu_get_active_title();
  }

  return $title;
}

/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Set the breadcrumb trail for the current page.
Dries Buytaert's avatar
 
Dries Buytaert committed
 *
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @param $breadcrumb
 *   Array of links, starting with "home" and proceeding up to but not including
 *   the current page.
Dries Buytaert's avatar
 
Dries Buytaert committed
function drupal_set_breadcrumb($breadcrumb = NULL) {
  static $stored_breadcrumb;

  if (isset($breadcrumb)) {
    $stored_breadcrumb = $breadcrumb;
  }
  return $stored_breadcrumb;
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Get the breadcrumb trail for the current page.
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function drupal_get_breadcrumb() {
  $breadcrumb = drupal_set_breadcrumb();

  if (!isset($breadcrumb)) {
    $breadcrumb = menu_get_active_breadcrumb();
  }

  return $breadcrumb;
}

Dries Buytaert's avatar
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Add output to the head tag of the HTML page.
Dries Buytaert's avatar
Dries Buytaert committed
 */
function drupal_set_html_head($data = NULL) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  static $stored_head = '';
Dries Buytaert's avatar
Dries Buytaert committed

  if (!is_null($data)) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $stored_head .= $data ."\n";
Dries Buytaert's avatar
Dries Buytaert committed
  }
  return $stored_head;
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Retrieve output to be displayed in the head tag of the HTML page.
 */
Dries Buytaert's avatar
Dries Buytaert committed
function drupal_get_html_head() {
  global $base_url;

Dries Buytaert's avatar
 
Dries Buytaert committed
  $output = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
Dries Buytaert's avatar
Dries Buytaert committed
  $output .= "<base href=\"$base_url/\" />\n";
Dries Buytaert's avatar
 
Dries Buytaert committed
  $output .= theme('stylesheet_import', 'misc/drupal.css');
Dries Buytaert's avatar
Dries Buytaert committed

  return $output . drupal_set_html_head();
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Return an array mapping path aliases to their internal Drupal paths.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function drupal_get_path_map($action = '') {
Dries Buytaert's avatar
Dries Buytaert committed
  static $map = NULL;
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
  if ($action == 'rebuild') {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $map = NULL;
  }

Dries Buytaert's avatar
Dries Buytaert committed
  if (is_null($map)) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $map = array();  // Make $map non-null in case no aliases are defined.
Dries Buytaert's avatar
 
Dries Buytaert committed
    $result = db_query('SELECT * FROM {url_alias}');
Dries Buytaert's avatar
 
Dries Buytaert committed
    while ($data = db_fetch_object($result)) {
Dries Buytaert's avatar
 
Dries Buytaert committed
      $map[$data->dst] = $data->src;
Dries Buytaert's avatar
 
Dries Buytaert committed
    }
  }

  return $map;
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Regenerate the path map from the information in the database.
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function drupal_rebuild_path_map() {
Dries Buytaert's avatar
 
Dries Buytaert committed
  drupal_get_path_map('rebuild');
Dries Buytaert's avatar
 
Dries Buytaert committed
}
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Given an internal Drupal path, return the alias set by the administrator.
Dries Buytaert's avatar
 
Dries Buytaert committed
function drupal_get_path_alias($path) {
  if (($map = drupal_get_path_map()) && ($newpath = array_search($path, $map))) {
    return $newpath;
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  elseif (function_exists('conf_url_rewrite')) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    return conf_url_rewrite($path, 'outgoing');
  }
  else {
    // No alias found. Return the normal path.
    return $path;
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
}

/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Given a path alias, return the internal path it represents.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
function drupal_get_normal_path($path) {
  if (($map = drupal_get_path_map()) && isset($map[$path])) {
    return $map[$path];
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  elseif (function_exists('conf_url_rewrite')) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    return conf_url_rewrite($path, 'incoming');
  }
  else {
    return $path;
  }
}
Dries Buytaert's avatar
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Set an HTTP response header for the current page.
Dries Buytaert's avatar
Dries Buytaert committed
 */
function drupal_set_header($header = NULL) {
  // We use an array to guarantee there are no leading or trailing delimiters.
Dries Buytaert's avatar
 
Dries Buytaert committed
  // Otherwise, header('') could get called when serving the page later, which
  // ends HTTP headers prematurely on some PHP versions.
  static $stored_headers = array();
  if (strlen($header)) {
Dries Buytaert's avatar
Dries Buytaert committed
    header($header);
    $stored_headers[] = $header;
  return implode("\n", $stored_headers);
Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Get the HTTP response headers for the current page.
 */
Dries Buytaert's avatar
Dries Buytaert committed
function drupal_get_headers() {
  return drupal_set_header();
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * @name HTTP handling
 * @{
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Functions to properly handle HTTP responses.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */

Dries Buytaert's avatar
 
Dries Buytaert committed
 * Send the user to a different Drupal page.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * This issues an on-site HTTP redirect. The function makes sure the redirected
 * URL is formatted correctly.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * 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).
Dries Buytaert's avatar
 
Dries Buytaert committed
function drupal_goto($path = '', $query = NULL, $fragment = NULL) {
  // Translate &amp; to simply & in the absolute URL.
  $url = str_replace('&amp;', '&', url($path, $query, $fragment, TRUE));
Dries Buytaert's avatar
 
Dries Buytaert committed
  if (ini_get('session.use_trans_sid') && session_id() && !strstr($url, session_id())) {
    $sid = session_name() . '=' . session_id();
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
    if (strstr($url, '?') && !strstr($url, $sid)) {
      $url = $url .'&'. $sid;
Dries Buytaert's avatar
 
Dries Buytaert committed
      $url = $url .'?'. $sid;
Dries Buytaert's avatar
 
Dries Buytaert committed
  // Before the redirect, allow modules to react to the end of the page request.
  module_invoke_all('exit', $url);

  header('Location: '. $url);
Dries Buytaert's avatar
 
Dries Buytaert committed
  // 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.
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function drupal_not_found() {
Dries Buytaert's avatar
 
Dries Buytaert committed
  header('HTTP/1.0 404 Not Found');
Dries Buytaert's avatar
 
Dries Buytaert committed
  watchdog('httpd', t('404 error: %page not found.', array('%page' => '<em>'. check_query($_GET['q']) .'</em>')));
Dries Buytaert's avatar
 
Dries Buytaert committed

  $path = drupal_get_normal_path(variable_get('site_404', ''));
Dries Buytaert's avatar
 
Dries Buytaert committed
  $status = MENU_NOT_FOUND;
Dries Buytaert's avatar
 
Dries Buytaert committed
  if ($path) {
    menu_set_active_item($path);
Dries Buytaert's avatar
 
Dries Buytaert committed
    $status = menu_execute_active_handler();
Dries Buytaert's avatar
 
Dries Buytaert committed
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  if ($status != MENU_FOUND) {
    print theme('page', '', t('Page not found'));
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
}
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Generates a 403 error if the request is not allowed.
 */
function drupal_access_denied() {
  header('HTTP/1.0 403 Forbidden');

  $path = drupal_get_normal_path(variable_get('site_403', ''));
Dries Buytaert's avatar
 
Dries Buytaert committed
  $status = MENU_NOT_FOUND;
Dries Buytaert's avatar
 
Dries Buytaert committed
  if ($path) {
    menu_set_active_item($path);
    $status = menu_execute_active_handler();
  }

  if ($status != MENU_FOUND) {
    print theme('page', message_access(), t('Access denied'));
  }
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Perform an HTTP request.
Dries Buytaert's avatar
 
Dries Buytaert committed
 *
Dries Buytaert's avatar
 
Dries Buytaert committed
 * 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.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
function drupal_http_request($url, $headers = array(), $method = 'GET', $data = NULL, $retry = 3) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  // Parse the URL, and make sure we can handle the schema.
Dries Buytaert's avatar
 
Dries Buytaert committed
  $uri = parse_url($url);
  switch ($uri['scheme']) {
    case 'http':
      $fp = @fsockopen($uri['host'], ($uri['port'] ? $uri['port'] : 80), $errno, $errstr, 15);
      break;
    case 'https':
Dries Buytaert's avatar
 
Dries Buytaert committed
      // Note: Only works for PHP 4.3 compiled with OpenSSL.
      $fp = @fsockopen('ssl://'. $uri['host'], ($uri['port'] ? $uri['port'] : 443), $errno, $errstr, 20);
Dries Buytaert's avatar
 
Dries Buytaert committed
      break;
    default:
Dries Buytaert's avatar
 
Dries Buytaert committed
      $result->error = 'invalid schema '. $uri['scheme'];
Dries Buytaert's avatar
 
Dries Buytaert committed
      return $result;
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  // Make sure the socket opened properly.
Dries Buytaert's avatar
 
Dries Buytaert committed
  if (!$fp) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $result->error = trim($errno .' '. $errstr);
Dries Buytaert's avatar
 
Dries Buytaert committed
    return $result;
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  // Construct the path to act on.
Dries Buytaert's avatar
 
Dries Buytaert committed
  $path = $uri['path'] ? $uri['path'] : '/';
  if ($uri['query']) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $path .= '?'. $uri['query'];
Dries Buytaert's avatar
 
Dries Buytaert committed
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  // Create HTTP request.
Dries Buytaert's avatar
 
Dries Buytaert committed
  $defaults = array(
Dries Buytaert's avatar
 
Dries Buytaert committed
    'Host' => 'Host: '. $uri['host'],
    'User-Agent' => 'User-Agent: Drupal (+http://www.drupal.org/)',
    'Content-Length' => 'Content-Length: '. strlen($data)
Dries Buytaert's avatar
 
Dries Buytaert committed
  );

  foreach ($headers as $header => $value) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $defaults[$header] = $header .': '. $value;
Dries Buytaert's avatar
 
Dries Buytaert committed
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  $request = $method .' '. $path ." HTTP/1.0\r\n";
Dries Buytaert's avatar
 
Dries Buytaert committed
  $request .= implode("\r\n", $defaults);
  $request .= "\r\n\r\n";
  if ($data) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $request .= $data ."\r\n";
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
  $result->request = $request;

  fwrite($fp, $request);

  // Fetch response.
  while (!feof($fp) && $data = fread($fp, 1024)) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
  fclose($fp);

  // Parse response.
  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);
Dries Buytaert's avatar
 
Dries Buytaert committed
  $result->headers = array();

  // Parse headers.
  while ($line = trim(array_shift($headers))) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    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
Dries Buytaert's avatar
 
Dries Buytaert committed
  // the base code in their class.
Dries Buytaert's avatar
 
Dries Buytaert committed
  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;
}
Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * @} End of "HTTP handling".
 */
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Log errors as defined by administrator
 * Error levels:
 *  1 = Log errors to database.
 *  2 = Log errors to database and to screen.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function error_handler($errno, $message, $filename, $line, $variables) {
Dries Buytaert's avatar
 
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 .'.';
Dries Buytaert's avatar
 
Dries Buytaert committed

    watchdog('error', t('%error: %message in %file on line %line.', array('%error' => $types[$errno], '%message' => $message, '%file' => $filename, '%line' => $line)));
Dries Buytaert's avatar
 
Dries Buytaert committed

    if (variable_get('error_level', 1) == 1) {
Dries Buytaert's avatar
 
Dries Buytaert committed
      print '<pre>'. $entry .'</pre>';
Dries Buytaert's avatar
Dries Buytaert committed
    }
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
}

Dries Buytaert's avatar
Dries Buytaert committed
function _fix_gpc_magic(&$item, $key) {
  if (is_array($item)) {
Kjartan Mannes's avatar
Kjartan Mannes committed
    array_walk($item, '_fix_gpc_magic');
  }
  else {
Kjartan Mannes's avatar
Kjartan Mannes committed
    $item = stripslashes($item);
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Correct double-escaping problems caused by "magic quotes" in some PHP
 * installations.
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function fix_gpc_magic() {
  static $fixed = false;
Dries Buytaert's avatar
 
Dries Buytaert committed
  if (!$fixed && ini_get('magic_quotes_gpc')) {
Dries Buytaert's avatar
Dries Buytaert committed
    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;
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
}

/**
 * @name Conversion
 * @{
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Converts data structures to different types.
Dries Buytaert's avatar
 
Dries Buytaert committed

/**
 * Convert an associative array to an anonymous object.
 */
Dries Buytaert's avatar
Dries Buytaert committed
function array2object($array) {
  if (is_array($array)) {
Dries Buytaert's avatar
Dries Buytaert committed
    foreach ($array as $key => $value) {
Dries Buytaert's avatar
 
Dries Buytaert committed
      $object->$key = $value;
    }
  }
  else {
Dries Buytaert's avatar
Dries Buytaert committed
    $object = $array;
Dries Buytaert's avatar
 
Dries Buytaert committed
  }

  return $object;
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Convert an object to an associative array.
 */
Dries Buytaert's avatar
Dries Buytaert committed
function object2array($object) {
  if (is_object($object)) {
    foreach ($object as $key => $value) {
Dries Buytaert's avatar
 
Dries Buytaert committed
      $array[$key] = $value;
    }
  }
  else {
Dries Buytaert's avatar
Dries Buytaert committed
    $array = $object;
Dries Buytaert's avatar
 
Dries Buytaert committed
  }

  return $array;
}
Dries Buytaert's avatar
 
Dries Buytaert committed

/**
 * @} End of "Conversion".
 */
Dries Buytaert's avatar
 
Dries Buytaert committed

/**
 * @name Messages
 * @{
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Frequently used messages.
Dries Buytaert's avatar
 
Dries Buytaert committed

/**
 * 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.
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function message_access() {
Dries Buytaert's avatar
 
Dries Buytaert committed
  return t('You are not authorized to access this page.');
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Return a string with a "not applicable" message.
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function message_na() {
Dries Buytaert's avatar
 
Dries Buytaert committed
  return t('n/a');
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * @} End of "Messages".
 */
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Initialize the localization system.
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function locale_initialize() {
  global $user;
Dries Buytaert's avatar
 
Dries Buytaert committed

  if (function_exists('i18n_get_lang')) {
    return i18n_get_lang();
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  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');
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  if ($user->uid && $languages[$user->language]) {
    return $user->language;
  }
  else {
    return key($languages);
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
 * Translate strings to the current locale.
 * When using t(), try to put entire sentences and strings in one t() call.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * 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
Dries Buytaert's avatar
 
Dries Buytaert committed
 *   $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')));
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @endcode
 * 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.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @param $string
Dries Buytaert's avatar
 
Dries Buytaert committed
 *   A string containing the English string to translate.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @param $args
 *   An associative array of replacements to make after translation. Incidences
Dries Buytaert's avatar
 
Dries Buytaert committed
 *   of any key in this array are replaced with the corresponding value.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @return
 *   The translated string.
Dries Buytaert's avatar
 
Dries Buytaert committed
function t($string, $args = 0) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  global $locale;
  if (function_exists('locale') && $locale != 'en') {
    $string = locale($string);
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  if (!$args) {
    return $string;
Kjartan Mannes's avatar
Kjartan Mannes committed
  }
  else {
Dries Buytaert's avatar
 
Dries Buytaert committed
    return strtr($string, $args);
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Encode special characters in a string for display as HTML.
 *
 * Note that we'd like to use htmlspecialchars($input, $quotes, 'utf-8')
 * as outlined in the PHP manual, but we can't because there's a bug in
 * PHP < 4.3 that makes it mess up multibyte charsets if we specify the
 * charset. This will be changed later once we make PHP 4.3 a requirement.
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function drupal_specialchars($input, $quotes = ENT_NOQUOTES) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  return htmlspecialchars($input, $quotes);
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
 * @defgroup validation Input validation
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @{
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Functions to validate user input.
Dries Buytaert's avatar
 
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's avatar
 
Dries Buytaert committed
 * @param $mail
 *   A string containing an email address.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @return
Dries Buytaert's avatar
 
Dries Buytaert committed
 *   TRUE if the address is in a valid format.
Dries Buytaert's avatar
 
Dries Buytaert committed
function valid_email_address($mail) {
  $user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+';
  $domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.?)+';
  $ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
  $ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';

Dries Buytaert's avatar
Dries Buytaert committed
  return preg_match("/^$user@($domain|(\[($ipv4|$ipv6)\]))$/", $mail);
Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Verify the syntax of the given URL.
 *
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @param $url
Dries Buytaert's avatar
 
Dries Buytaert committed
 *   The URL to verify.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @param $absolute
Dries Buytaert's avatar
 
Dries Buytaert committed
 *   Whether the URL is absolute (beginning with a scheme such as "http:").
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @return
Dries Buytaert's avatar
 
Dries Buytaert committed
 *   TRUE if the URL is in a valid format.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function valid_url($url, $absolute = FALSE) {
    return preg_match("/^(http|https|ftp):\/\/[a-z0-9\/:_\-_\.\?,~=#&%\+]+$/i", $url);
    return preg_match("/^[a-z0-9\/:_\-_\.,\+]+$/i", $url);
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Validate data input by a user.
 *
 * Ensures that user data cannot be used to perform attacks on the site.
 *
 * @param $data
 *   The input to check.
 * @return
 *   TRUE if the input data is acceptable.
 */
function valid_input_data($data) {
  if (is_array($data) || is_object($data)) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    // Form data can contain a number of nested arrays.
    foreach ($data as $key => $value) {
Dries Buytaert's avatar
 
Dries Buytaert committed
      if (!valid_input_data($key) || !valid_input_data($value)) {
Dries Buytaert's avatar
 
Dries Buytaert committed
        return FALSE;
Dries Buytaert's avatar
 
Dries Buytaert committed
    // Detect dangerous input data.
Dries Buytaert's avatar
 
Dries Buytaert committed
    // Check strings:
    $match  = preg_match('/\Wjavascript\s*:/i', $data);
    $match += preg_match('/\Wexpression\s*\(/i', $data);
    $match += preg_match('/\Walert\s*\(/i', $data);
Dries Buytaert's avatar
 
Dries Buytaert committed
    // Check attributes:
    $match += preg_match("/\W(dynsrc|datasrc|data|lowsrc|on[a-z]+)\s*=[^>]+?>/i", $data);

Dries Buytaert's avatar
 
Dries Buytaert committed
    // Check tags:
    $match += preg_match("/<\s*(applet|script|object|style|embed|form|blink|meta|html|frame|iframe|layer|ilayer|head|frameset|xml)/i", $data);

    if ($match) {
Dries Buytaert's avatar
 
Dries Buytaert committed
      watchdog('warning', t('Terminated request because of suspicious input data: %data.', array('%data' => '<em>'. drupal_specialchars($data) .'</em>')));
Dries Buytaert's avatar
 
Dries Buytaert committed
      return FALSE;
Dries Buytaert's avatar
 
Dries Buytaert committed
  return TRUE;
Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * @} End of "defgroup validation".
 */

/**
 * @defgroup search Search interface
 * @{
Dries Buytaert's avatar
 
Dries Buytaert committed
 * The Drupal search interface manages a global search mechanism.
 *
 * Modules may plug into this system to provide searches of different types of
 * data. Most of the system is handled by search.module, so this must be enabled
 * for all of the search features to work.
Dries Buytaert's avatar
 
Dries Buytaert committed

Kjartan Mannes's avatar
Kjartan Mannes committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Format a single result entry of a search query.
 *
 * Modules may implement hook_search_item() in order to override this default
 * function to display search results.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @param $item
 *   A single search result as returned by hook_search(). The result should be
 *   an array with keys "count", "link", "title", "user", "date", and "keywords".
 * @param $type
 *   The type of item found, such as "user" or "comment".
Dries Buytaert's avatar
 
Dries Buytaert committed
function search_item($item, $type) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  if (module_hook($type, 'search_item')) {
    $output = module_invoke($type, 'search_item', $item);
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
  else {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $output = ' <dt class="title"><a href="'. $item['link'] .'">'. $item['title'] .'</a></dt>';
    $output .= ' <dd class="small">' . t($type) . ($item['user'] ? ' - '. $item['user'] : '') .''. ($item['date'] ? ' - '. format_date($item['date'], 'small') : '') .'</dd>';
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed

  return $output;
}

Kjartan Mannes's avatar
Kjartan Mannes committed
/**
 * Render a generic search form.
 *
Dries Buytaert's avatar
 
Dries Buytaert committed
 * This form must be usable not only within "http://example.com/search", but also
 * as a simple search box (without "Restrict search to", help text, etc.), in the
 * theme's header, and so forth. This means we must provide options to
 * conditionally render certain parts of this form.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @param $action
 *   Form action. Defaults to "search".
 * @param $keys
 *   The search string entered by the user, containing keywords for the search.
 * @param $options
 *   Whether to render the optional form fields and text ("Restrict search
 *   to", help text, etc.).
 * @return
 *   An HTML string containing the search form.
Dries Buytaert's avatar
 
Dries Buytaert committed
function search_form($action = '', $keys = '', $options = FALSE) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  $edit = $_POST['edit'];

Dries Buytaert's avatar
 
Dries Buytaert committed
  if (!$action) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $action = url('search');
Dries Buytaert's avatar
 
Dries Buytaert committed
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  $output = ' <div class="search-form"><br /><input type="text" class="form-text" size="50" value="'. check_form($keys) .'" name="keys" />';
  $output .= ' <input type="submit" class="form-submit" value="'. t('Search') ."\" />\n";
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
Dries Buytaert committed
  if ($options) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $output .= '<br />';
    $output .= t('Restrict search to') .': ';
Dries Buytaert's avatar
 
Dries Buytaert committed

    foreach (module_list() as $name) {
Dries Buytaert's avatar
 
Dries Buytaert committed
      if (module_hook($name, 'search')) {
        $output .= ' <input type="checkbox" name="edit[type]['. $name .']" '. ($edit['type'][$name] ? ' checked="checked"' : '') .' /> '. t($name);
Dries Buytaert's avatar
 
Dries Buytaert committed
      }
    }
  }
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
  return form($output, 'post', $action);
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Perform a global search on the given keys, and return the formatted results.
Dries Buytaert's avatar
 
Dries Buytaert committed
function search_data($keys = NULL) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  $edit = $_POST['edit'];
Dries Buytaert's avatar
 
Dries Buytaert committed
  $output = '';
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
  if (isset($keys)) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    foreach (module_list() as $name) {
Dries Buytaert's avatar
 
Dries Buytaert committed
      if (module_hook($name, 'search') && (!$edit['type'] || $edit['type'][$name])) {
        list($title, $results) = module_invoke($name, 'search', $keys);
Dries Buytaert's avatar
 
Dries Buytaert committed
        if ($results) {
Dries Buytaert's avatar
 
Dries Buytaert committed
          $output .= '<h2>'. $title .'</h2>';
          $output .= '<dl class="search-results">';
Dries Buytaert's avatar
 
Dries Buytaert committed
          foreach ($results as $entry) {
            $output .= search_item($entry, $name);
          }
Dries Buytaert's avatar
 
Dries Buytaert committed
          $output .= '</dl>';
Dries Buytaert's avatar
 
Dries Buytaert committed
        }
      }
    }
  }

  return $output;
}

Kjartan Mannes's avatar
Kjartan Mannes committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Display a search form for a particular type of data.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @param $type
 *   The type of content to search within.
 * @param $action
 *   Form action. Defaults to "search".
 * @param $keys
 *   The search string entered by the user, containing keywords for the search.
 * @param $options
 *   Whether to render the optional form fields and text ("Restrict search
 *   to", help text, etc.).
 * @return
 *   An HTML string containing the search form and results.
Dries Buytaert's avatar
 
Dries Buytaert committed
function search_type($type, $action = '', $keys = '', $options = FALSE) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  $_POST['edit']['type'][$type] = 'on';
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
  return search_form($action, $keys, $options) . '<br />'. search_data($keys);
Dries Buytaert's avatar
 
Dries Buytaert committed
}
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @} End of "defgroup search".
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
function check_form($text) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  return drupal_specialchars($text, ENT_QUOTES);
Dries Buytaert's avatar
 
Dries Buytaert committed
}

function check_file($filename) {
  return is_uploaded_file($filename);
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @defgroup format Formatting
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @{
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Functions to format numbers, strings, dates, etc.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * 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()) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  // arbitrary elements may be added using the $args associative array

Dries Buytaert's avatar
Dries Buytaert committed
  $output = "<channel>\n";
Dries Buytaert's avatar
 
Dries Buytaert committed
  $output .= ' <title>'. drupal_specialchars(strip_tags($title)) ."</title>\n";
  $output .= ' <link>'. drupal_specialchars(strip_tags($link)) ."</link>\n";
  $output .= ' <description>'. drupal_specialchars(strip_tags($description)) ."</description>\n";
  $output .= ' <language>'. drupal_specialchars(strip_tags($language)) ."</language>\n";
Dries Buytaert's avatar
 
Dries Buytaert committed
  foreach ($args as $key => $value) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $output .= ' <'. $key .'>'. drupal_specialchars(strip_tags($value)) ."</$key>\n";
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  $output .= $items;
  $output .= "</channel>\n";

  return $output;
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Format a single RSS item.
 *
 * Arbitrary elements may be added using the $args associative array.
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function format_rss_item($title, $link, $description, $args = array()) {
Dries Buytaert's avatar
Dries Buytaert committed
  $output = "<item>\n";
Dries Buytaert's avatar
 
Dries Buytaert committed
  $output .= ' <title>'. drupal_specialchars(strip_tags($title)) ."</title>\n";
  $output .= ' <link>'. drupal_specialchars(strip_tags($link)) ."</link>\n";
  $output .= ' <description>'. drupal_specialchars($description) ."</description>\n";
Dries Buytaert's avatar
 
Dries Buytaert committed
  foreach ($args as $key => $value) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $output .= ' <'. $key .'>'. drupal_specialchars(strip_tags($value)) ."</$key>\n";
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  $output .= "</item>\n";

  return $output;
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Format a string containing a count of items.
Dries Buytaert's avatar
 
Dries Buytaert committed
 *
Dries Buytaert's avatar
 
Dries Buytaert committed
 * 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.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function format_plural($count, $singular, $plural) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  if ($count == 1) return t($singular, array("%count" => $count));
Dries Buytaert's avatar
 
Dries Buytaert committed

  // 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":
Dries Buytaert's avatar
 
Dries Buytaert committed
        return t($singular, array("%count" => $count));
Dries Buytaert's avatar
 
Dries Buytaert committed
      case "1":
        return t($plural, array("%count" => $count));
      default:
        return t(strtr($plural, array("%count" => '%count['. $index .']')), array('%count['. $index .']' => $count));
    }
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Generate a string representation for the given byte count.
Dries Buytaert's avatar
 
Dries Buytaert committed
 *
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @param $size
 *   The size in bytes.
 * @return
 *   A translated string representation of the size.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function format_size($size) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  $suffix = t('bytes');
Dries Buytaert's avatar
 
Dries Buytaert committed
  if ($size > 1024) {
    $size = round($size / 1024, 2);
Dries Buytaert's avatar
 
Dries Buytaert committed
    $suffix = t('KB');
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
  if ($size > 1024) {
    $size = round($size / 1024, 2);
Dries Buytaert's avatar
 
Dries Buytaert committed
    $suffix = t('MB');
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  return t('%size %suffix', array('%size' => $size, '%suffix' => $suffix));
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Format a time interval with the requested granularity.
Dries Buytaert's avatar
 
Dries Buytaert committed
 *
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @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.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function format_interval($timestamp, $granularity = 2) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  $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);
Dries Buytaert's avatar
 
Dries Buytaert committed
  $output = '';
Dries Buytaert's avatar
 
Dries Buytaert committed
  foreach ($units as $key => $value) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $key = explode('|', $key);
Dries Buytaert's avatar
 
Dries Buytaert committed
    if ($timestamp >= $value) {
Dries Buytaert's avatar
 
Dries Buytaert committed
      $output .= ($output ? ' ' : '') . format_plural(floor($timestamp / $value), $key[0], $key[1]);
Dries Buytaert's avatar
 
Dries Buytaert committed
      $timestamp %= $value;
Dries Buytaert's avatar
 
Dries Buytaert committed
      $granularity--;
    }

    if ($granularity == 0) {
      break;
Dries Buytaert's avatar
 
Dries Buytaert committed
    }
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  return $output ? $output : t('0 sec');
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Format a date with the given configured format or a custom format string.
 *
Dries Buytaert's avatar
 
Dries Buytaert committed
 * 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.
 *
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @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
 *   A PHP date format string as required by date().
 * @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.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL) {
  if ($timezone === NULL) {
    global $user;
Steven Wittens's avatar
Steven Wittens committed
    if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
      $timezone = $user->timezone;
    }
    else {
      $timezone = variable_get('date_default_timezone', 0);
    }
Dries Buytaert's avatar
 
Dries Buytaert committed

  $timestamp += $timezone;
Dries Buytaert's avatar
 
Dries Buytaert committed

  switch ($type) {
    case 'small':
      $format = variable_get('date_format_short', 'm/d/Y - H:i');
Dries Buytaert's avatar
 
Dries Buytaert committed
      break;
    case 'large':
      $format = variable_get('date_format_long', 'l, F j, Y - H:i');
Dries Buytaert's avatar
 
Dries Buytaert committed
      break;
Dries Buytaert's avatar
 
Dries Buytaert committed
      // No change to format
Dries Buytaert's avatar
 
Dries Buytaert committed
      break;
Dries Buytaert's avatar
 
Dries Buytaert committed
    default:
      $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
Dries Buytaert's avatar
 
Dries Buytaert committed
  }

  $max = strlen($format);
Dries Buytaert's avatar
 
Dries Buytaert committed
  $date = '';
Dries Buytaert's avatar
 
Dries Buytaert committed
  for ($i = 0; $i < $max; $i++) {
    $c = $format{$i};
      $date .= t(gmdate($c, $timestamp));
    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);
Dries Buytaert's avatar
 
Dries Buytaert committed
    }