You are here

echo.module in Echo 7

Same filename and directory in other branches
  1. 8 echo.module
  2. 6 echo.module

The echo module converts text into a fully-themed page.

File

echo.module
View source
<?php

/**
 * @file
 * The echo module converts text into a fully-themed page.
 *
 * @see http://drupal.org/project/htmlmail
 */

/**
 * Returns a page themed for the anonymous user.
 *
 * Generates the text of a fully-themed Drupal page.  The rendered page is
 * returned as a text string instead of being sent to the browser.  The
 * theme system can thus be used to style any HTML message as if it had
 * been generated by the live website.
 *
 * @param $title
 *   The text to display as the page title.
 * @param $content
 *   The text to display as the page body.
 * @param $theme
 *   The machine-readable name of the theme to use.
 *
 * @return
 *   A string containing the fully-themed html page.
 */
function echo_themed_page($title, $content, $theme) {
  $url = url('echo', array(
    'absolute' => TRUE,
  ));

  // Store a hash of the arguments in the cache, which will be checked by
  // _echo_access() to ensure that the request originated from this function
  // and not from an external source.
  $key = variable_get('echo_key');
  $token = sha1($title . $content . $theme . $key);

  // Thirty seconds ought to be enough for anyone.
  $options = array(
    'method' => 'POST',
    'data' => 'title=' . rawurlencode($title) . '&content=' . rawurlencode($content) . '&theme=' . rawurlencode($theme) . '&token=' . $token,
    'headers' => array(
      'Content-Type' => 'application/x-www-form-urlencoded',
    ),
  );
  $return = '';

  // Turn off maintenance mode so that anonymous page views work.
  if ($maintenance_mode = variable_get('maintenance_mode', 0)) {
    variable_set('maintenance_mode', 0);
  }
  if (($response = drupal_http_request($url, $options)) && isset($response->data)) {
    $return = $response->data;
  }
  if ($maintenance_mode) {
    variable_set('maintenance_mode', $maintenance_mode);
  }
  return $return;
}

/**
 * Implements hook_menu().
 */
function echo_menu() {
  return array(
    'echo' => array(
      'title callback' => '_echo_request',
      'title arguments' => array(
        'title',
      ),
      'page callback' => '_echo_request',
      'page arguments' => array(
        'content',
      ),
      'theme callback' => '_echo_request',
      'theme arguments' => array(
        'theme',
      ),
      'access callback' => '_echo_access',
      'type' => MENU_CALLBACK,
    ),
  );
}

/**
 * Returns the contents of a _REQUEST variable.
 */
function _echo_request($key) {
  return isset($_REQUEST[$key]) ? $_REQUEST[$key] : '';
}

/**
 * Returns TRUE if this request originated from the echo_themed_page() function;
 * FALSE otherwise.
 */
function _echo_access() {
  $key = variable_get('echo_key');
  if (isset($_REQUEST['title'])) {
    if (isset($_REQUEST['content'])) {
      if (isset($_REQUEST['theme'])) {
        $calculated_token = sha1($_REQUEST['title'] . $_REQUEST['content'] . $_REQUEST['theme'] . $key);
        if ($calculated_token == $_REQUEST['token']) {
          return TRUE;
        }
      }
    }
  }
  return FALSE;
}

Functions

Namesort descending Description
echo_menu Implements hook_menu().
echo_themed_page Returns a page themed for the anonymous user.
_echo_access Returns TRUE if this request originated from the echo_themed_page() function; FALSE otherwise.
_echo_request Returns the contents of a _REQUEST variable.