Skip to content
common.inc 58.5 KiB
Newer Older
Dries Buytaert's avatar
 
Dries Buytaert committed
<?php
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
/**
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
 * This function can be called as long the headers aren't sent.
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
/**
 * 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
/**
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
 */

/**
 * 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));
}

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.
 * 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.
 *
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) {
  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);
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() {
  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);
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;
  if ($path  && $path != $_GET['q']) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    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) {
    drupal_set_title(t('Page not found'));
    print theme('page', '');
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() {
  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']));
Dries Buytaert's avatar
 
Dries Buytaert committed

  $path = drupal_get_normal_path(variable_get('site_403', ''));
Dries Buytaert's avatar
 
Dries Buytaert committed
  $status = MENU_NOT_FOUND;
  if ($path  && $path != $_GET['q']) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    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());
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
}

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
  $result = new StdClass();

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) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $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

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
    }

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

Dries Buytaert's avatar
 
Dries Buytaert committed
function _fix_gpc_magic(&$item) {
Dries Buytaert's avatar
Dries Buytaert committed
  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
    $object = new StdClass();
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
 * @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) {
  $allowed_characters = '[a-z0-9\/:_\-_\.\?\$,~=#&%\+]';
    return preg_match("/^(http|https|ftp):\/\/". $allowed_characters ."+$/i", $url);
    return preg_match("/^". $allowed_characters ."+$/i", $url);
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * 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);
}

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

/**
 * Prepare a URL for use in an HTML attribute. Strips harmful protocols.
 *
 */
function check_url($uri) {
  return filter_xss_bad_protocol($uri, FALSE);
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";
  $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";
Dries Buytaert's avatar
 
Dries Buytaert committed
  foreach ($args as $key => $value) {
    $output .= ' <'. $key .'>'. check_plain($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";
  $output .= ' <title>'. check_plain($title) ."</title>\n";
  $output .= ' <link>'. check_url($link) ."</link>\n";
  $output .= ' <description>'. check_plain($description) ."</description>\n";
Dries Buytaert's avatar
 
Dries Buytaert committed
  foreach ($args as $key => $value) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    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";
Dries Buytaert's avatar
 
Dries Buytaert committed
    }
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) {
  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":
        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(). A backslash should be used
 *   before a character to avoid interpreting the character as part of a date
 *   format.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @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
    }
    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's avatar
 
Dries Buytaert committed
    }
Dries Buytaert's avatar
 
Dries Buytaert committed
    else {
Dries Buytaert's avatar
 
Dries Buytaert committed
    }
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  return $date;
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Format a username.
Dries Buytaert's avatar
 
Dries Buytaert committed
 *
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @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.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function format_name($object) {

  if ($object->uid && $object->name) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    // Shorten the name when it is too long or it will break many tables.
    if (strlen($object->name) > 20) {
Dries Buytaert's avatar
 
Dries Buytaert committed
      $name = truncate_utf8($object->name, 15) .'...';
    }
    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;
    }
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  else if ($object->name) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    // 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.
Dries Buytaert's avatar
 
Dries Buytaert committed
    if ($object->homepage) {
Dries Buytaert's avatar
 
Dries Buytaert committed
      $output = '<a href="'. $object->homepage .'">'. $object->name .'</a>';
Dries Buytaert's avatar
 
Dries Buytaert committed
    }
    else {
      $output = $object->name;
    }

    $output .= ' ('. t('not verified') .')';
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  else {
Dries Buytaert's avatar
Dries Buytaert committed
    $output = variable_get('anonymous', 'Anonymous');
Dries Buytaert's avatar
 
Dries Buytaert committed
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  return $output;
Dries Buytaert's avatar
 
Dries Buytaert committed
}
Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * @} End of "defgroup format".
 */
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
 * @defgroup form Form generation
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Functions to enable output of HTML forms and form elements.
Dries Buytaert's avatar
 
Dries Buytaert committed
 * 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
 * must be explicitly generated by modules.
Dries Buytaert's avatar
 
Dries Buytaert committed

/**
 * 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) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  if (!$action) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
  return '<form action="'. check_url($action) .'" method="'. $method .'"'. drupal_attributes($attributes) .">\n". $form ."\n</form>\n";
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * File an error against the form element with the specified name.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
function form_set_error($name, $message) {
  $GLOBALS['form'][$name] = $message;
  drupal_set_message($message, 'error');
}

/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Return an associative array of all errors.
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function form_get_errors() {
  if (array_key_exists('form', $GLOBALS)) {
    return $GLOBALS['form'];
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
}

/**
 * Return the error message filed against the form with the specified name.
 */
function _form_get_error($name) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  if (array_key_exists('form', $GLOBALS)) {
    return $GLOBALS['form'][$name];
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
}

function _form_get_class($name, $required, $error) {
  return $name. ($required ? ' required' : '') . ($error ? ' error' : '');
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * 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.
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function form_item($title, $value, $description = NULL, $id = NULL, $required = FALSE, $error = FALSE) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  return theme('form_element', $title, $value, $description, $id, $required, $error);
Dries Buytaert's avatar
 
Dries Buytaert committed
}
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Format a group of form items.
 *
 * @param $legend