echo.module in Echo 6
Same filename and directory in other branches
The echo module converts text into a fully-themed page.
See also
File
echo.moduleView 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_set($key, $key, 'cache', $expiration);
$headers = array(
'Content-Type' => 'application/x-www-form-urlencoded',
);
$data = 'title=' . rawurlencode($title) . '&content=' . rawurlencode($content) . '&theme=' . rawurlencode($theme);
$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, $headers, 'POST', $data)) && 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_page',
'access callback' => '_echo_access',
'type' => MENU_CALLBACK,
),
);
}
/**
* Returns the contents of a _REQUEST variable.
*/
function _echo_request($key) {
return isset($_REQUEST[$key]) ? $_REQUEST[$key] : '';
}
/**
* Displays a page with the selected title, comment, and theme.
*/
function _echo_page() {
global $theme, $custom_theme;
if ($custom_theme = _echo_request('theme')) {
$theme = NULL;
init_theme();
}
return _echo_request('content');
}
/**
* 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_get($key)) {
if ($access->data == $key) {
return TRUE;
}
}
}
}
}
return FALSE;
}
Functions
Name | 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_page | Displays a page with the selected title, comment, and theme. |
_echo_request | Returns the contents of a _REQUEST variable. |