echo.module 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @file
  4. * The echo module converts text into a fully-themed page.
  5. *
  6. * @see http://drupal.org/project/htmlmail
  7. */
  8. /**
  9. * Returns a page themed for the anonymous user.
  10. *
  11. * Generates the text of a fully-themed Drupal page. The rendered page is
  12. * returned as a text string instead of being sent to the browser. The
  13. * theme system can thus be used to style any HTML message as if it had
  14. * been generated by the live website.
  15. *
  16. * @param $title
  17. * The text to display as the page title.
  18. * @param $content
  19. * The text to display as the page body.
  20. * @param $theme
  21. * The machine-readable name of the theme to use.
  22. *
  23. * @return
  24. * A string containing the fully-themed html page.
  25. */
  26. function echo_themed_page($title, $content, $theme) {
  27. $url = url('echo', array('absolute' => TRUE));
  28. // Store a hash of the arguments in the cache, which will be checked by
  29. // _echo_access() to ensure that the request originated from this function
  30. // and not from an external source.
  31. $key = sha1($title . $content . $theme);
  32. // Thirty seconds ought to be enough for anyone.
  33. $expiration = REQUEST_TIME + max(ini_get('max_execution_time'), 30);
  34. cache_set($key, $key, 'cache', $expiration);
  35. $options = array(
  36. 'method' => 'POST',
  37. 'data' => 'title=' . rawurlencode($title)
  38. . '&content=' . rawurlencode($content)
  39. . '&theme=' . rawurlencode($theme),
  40. 'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
  41. );
  42. $return = '';
  43. // Turn off maintenance mode so that anonymous page views work.
  44. if ($maintenance_mode = variable_get('maintenance_mode', 0)) {
  45. variable_set('maintenance_mode', 0);
  46. }
  47. if ( ($response = drupal_http_request($url, $options))
  48. && isset($response->data) ) {
  49. $return = $response->data;
  50. }
  51. if ($maintenance_mode) {
  52. variable_set('maintenance_mode', $maintenance_mode);
  53. }
  54. return $return;
  55. }
  56. /**
  57. * Implements hook_menu().
  58. */
  59. function echo_menu() {
  60. return array(
  61. 'echo' => array(
  62. 'title callback' => '_echo_request',
  63. 'title arguments' => array('title'),
  64. 'page callback' => '_echo_request',
  65. 'page arguments' => array('content'),
  66. 'theme callback' => '_echo_request',
  67. 'theme arguments' => array('theme'),
  68. 'access callback' => '_echo_access',
  69. 'type' => MENU_CALLBACK,
  70. )
  71. );
  72. }
  73. /**
  74. * Returns the contents of a _REQUEST variable.
  75. */
  76. function _echo_request($key) {
  77. return isset($_REQUEST[$key]) ? $_REQUEST[$key] : '';
  78. }
  79. /**
  80. * Returns TRUE if this request originated from the echo_themed_page() function;
  81. * FALSE otherwise.
  82. */
  83. function _echo_access() {
  84. if (isset($_REQUEST['title'])) {
  85. if (isset($_REQUEST['content'])) {
  86. if (isset($_REQUEST['theme'])) {
  87. $key = sha1(
  88. $_REQUEST['title'] . $_REQUEST['content'] . $_REQUEST['theme']
  89. );
  90. if ($access = cache_get($key)) {
  91. if ($access->data == $key) {
  92. return TRUE;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. return FALSE;
  99. }