You are here

echo.module in Echo 8

Same filename and directory in other branches
  1. 6 echo.module
  2. 7 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 = sha1($title . $content . $theme);

  // Thirty seconds ought to be enough for anyone.
  $expiration = REQUEST_TIME + max(ini_get('max_execution_time'), 30);
  cache('cache')
    ->set($key, $key, $expiration);
  $options = array(
    'method' => 'POST',
    'data' => 'title=' . rawurlencode($title) . '&content=' . rawurlencode($content) . '&theme=' . rawurlencode($theme),
    '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;
  }
  cache('cache')
    ->delete($key);
  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() {
  if (isset($_REQUEST['title'])) {
    if (isset($_REQUEST['content'])) {
      if (isset($_REQUEST['theme'])) {
        $key = sha1($_REQUEST['title'] . $_REQUEST['content'] . $_REQUEST['theme']);
        if ($access = cache('cache')
          ->get($key)) {
          if ($access->data == $key) {
            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.