mimemail.theme.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * @file
  4. * The theme system, which controls the output of the messages.
  5. */
  6. function mimemail_theme_theme() {
  7. $path = drupal_get_path('module', 'mimemail') . '/theme';
  8. return array(
  9. 'mimemail_message' => array(
  10. 'variables' => array('module' => NULL, 'key' => NULL, 'recipient' => NULL, 'subject' => NULL, 'body' => NULL),
  11. 'template' => 'mimemail-message',
  12. 'pattern' => 'mimemail_message__',
  13. 'file' => 'mimemail.theme.inc',
  14. 'mail theme' => TRUE,
  15. 'path' => $path,
  16. )
  17. );
  18. }
  19. /**
  20. * A preprocess function for theme('mimemail_message').
  21. *
  22. * The $variables array initially contains the following arguments:
  23. * - $recipient: The recipient of the message
  24. * - $key: The mailkey associated with the message
  25. * - $subject: The message subject
  26. * - $body: The message body
  27. *
  28. * @see mimemail-message.tpl.php
  29. */
  30. function template_preprocess_mimemail_message(&$variables) {
  31. $theme = mailsystem_get_mail_theme();
  32. $themepath = drupal_get_path('theme', $theme);
  33. $sitestyle = variable_get('mimemail_sitestyle', 1);
  34. $mailstyles = file_scan_directory($themepath, '#^mail\.css*$#');
  35. // Check recursively for the existence of a mail.css file in the theme folder.
  36. if (!empty($mailstyles)) {
  37. foreach ($mailstyles as $mailstyle) {
  38. $styles = $mailstyle->uri;
  39. }
  40. }
  41. // If no mail.css was found and the site style sheets including is enabled,
  42. // gather all style sheets and embed a version of all style definitions.
  43. elseif ($sitestyle) {
  44. // Grab local.css if it exists (support for Fusion based themes).
  45. $local = $themepath . '/css/local.css';
  46. if (@file_exists($local)) {
  47. $css_all = drupal_add_css($local, array('group' => CSS_THEME));
  48. }
  49. else {
  50. $css_all = drupal_add_css();
  51. }
  52. $css_files = array();
  53. foreach ($css_all as $key => $options) {
  54. if ($options['group'] == CSS_THEME && $options['type'] == 'file' &&
  55. ($options['media'] == 'all' || $options['media'] == 'screen')) {
  56. $css_files[$key] = $options;
  57. }
  58. }
  59. if (variable_get('preprocess_css', FALSE)) {
  60. $pattern = '|<link.*href="' . $GLOBALS['base_url'] . '/([^"?]*)[?"].*|';
  61. $replacement = '\1';
  62. }
  63. else {
  64. $pattern = array(
  65. '/<([^<>]*)>/', // Remove the style tag.
  66. '/@import\s+url\("([^"]+)"\);+/', // Remove the import directive.
  67. '|' . $GLOBALS['base_url'] . '/([^"?]*)[?"].*|' // Remove the base URL.
  68. );
  69. $replacement = array('', '\1', '\1');
  70. }
  71. $styles = preg_replace($pattern, $replacement, drupal_get_css($css_files));
  72. }
  73. $css = '';
  74. if (isset($styles)) {
  75. // Process each style sheet.
  76. foreach (explode("\n", $styles) as $style) {
  77. if (!empty($style) && @file_exists($style)) {
  78. $css .= @file_get_contents($style);
  79. }
  80. }
  81. // Regexp to match comment blocks.
  82. $comment = '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/';
  83. // Regexp to match double quoted strings.
  84. $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
  85. // Regexp to match single quoted strings.
  86. $single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";
  87. // Perform some safe CSS optimizations (derived from core CSS aggregation).
  88. $css = preg_replace_callback(
  89. "<$double_quot|$single_quot|$comment>Sus", // Match all comment blocks along
  90. "_mimemail_process_comment", // with double/single quoted strings
  91. $css); // and feed them to _mimemail_process_comment().
  92. $css = preg_replace(
  93. '<\s*([@{}:;,]|\)\s|\s\()\s*[^\n\S]>S', // Remove whitespace around separators,
  94. '\1', // but keep space around parentheses
  95. $css); // and new lines between definitions.
  96. // End the file with a new line.
  97. $css .= "\n";
  98. // Wordwrap to adhere to RFC821
  99. $css = wordwrap($css, 700);
  100. }
  101. // Set styles for the message.
  102. $variables['css'] = $css;
  103. // Set template alternatives.
  104. $variables['theme_hook_suggestions'][] = 'mimemail_message__' . str_replace('-', '_', $variables['key']);
  105. // Process identifiers to be proper CSS classes.
  106. $variables['module'] = str_replace('_', '-', $variables['module']);
  107. $variables['key'] = str_replace('_', '-', $variables['key']);
  108. }
  109. /**
  110. * Process comment blocks. (derived from core CSS aggregation)
  111. *
  112. * This is the callback function for the preg_replace_callback()
  113. * used in drupal_load_stylesheet_content(). Support for comment
  114. * hacks is implemented here.
  115. */
  116. function _mimemail_process_comment($matches) {
  117. static $keep_nextone = FALSE;
  118. // Quoted string, keep it.
  119. if ($matches[0][0] == "'" || $matches[0][0] == '"') {
  120. return $matches[0];
  121. }
  122. // End of IE-Mac hack, keep it.
  123. if ($keep_nextone) {
  124. $keep_nextone = FALSE;
  125. return $matches[0];
  126. }
  127. switch (strrpos($matches[0], '\\')) {
  128. case FALSE :
  129. // No backslash, strip it.
  130. return '';
  131. case drupal_strlen($matches[0])-3 :
  132. // Ends with \*/ so is a multi line IE-Mac hack, keep the next one also.
  133. $keep_nextone = TRUE;
  134. return '/*_\*/';
  135. default :
  136. // Single line IE-Mac hack.
  137. return '/*\_*/';
  138. }
  139. }