styling.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @file
  4. * Handles adding theme stylesheets into WYSIWYG editors.
  5. */
  6. /**
  7. * A simple page callback for a checking if a theme is active.
  8. *
  9. * A theme the user does not have permission to use can not be set active.
  10. *
  11. * @see _wysiwyg_theme_callback()
  12. * @see _wysiwyg_delivery_dummy()
  13. */
  14. function _wysiwyg_theme_check_active($theme) {
  15. global $theme_key;
  16. return $theme === $theme_key;
  17. }
  18. /**
  19. * A simple page delivery dummy implementation.
  20. *
  21. * Acts like a normal HTML delivery mechanism but only "renders" a dummy string
  22. * and prints a short string telling if the page callback result was "true".
  23. *
  24. * Useful for returning the results of an access check, performed by the page
  25. * callback. The actual page callback return value is never printed.
  26. *
  27. * Success:
  28. * - Status header: "200 OK"
  29. * - Content: "OK"
  30. *
  31. * Failure:
  32. * - Status header: "403 Forbidden"
  33. * - Content: "Forbidden"
  34. *
  35. * @see _wysiwyg_theme_check_active()
  36. */
  37. function _wysiwyg_delivery_dummy($page_callback_result) {
  38. global $theme_key;
  39. drupal_add_http_header('Content-Language', 'en');
  40. drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
  41. if ($page_callback_result) {
  42. drupal_add_http_header('Status', '200 OK');
  43. }
  44. else {
  45. drupal_add_http_header('Status', '403 Forbidden');
  46. }
  47. // Make sure the theme is always initialized.
  48. drupal_theme_initialize();
  49. // Render a completely themed empty page to catch as many stylesheets as
  50. // possible, but don't actually return anything to speed up the response.
  51. $rendered = drupal_render_page('Dummy');
  52. // In case headers aren't enough, put the status in the response body.
  53. print ($page_callback_result ? 'OK' : 'Forbidden');
  54. // Cleanup and session handling.
  55. drupal_page_footer();
  56. }
  57. /**
  58. * Theme callback to simply suggest a theme based on the page arugment.
  59. */
  60. function _wysiwyg_theme_callback($theme) {
  61. return $theme;
  62. }
  63. /**
  64. * A filtering pre render callback for style elements.
  65. *
  66. * Invokes hook_wysiwyg_editor_stules_alter() to allow other code to filter the
  67. * list of stylesheets which will be used inside the editors in WYSIWYG mode.
  68. *
  69. * Intended to run before Core sorts/groups/aggregates stylesheets.
  70. */
  71. function _wysiwyg_filter_editor_styles(&$elements) {
  72. global $theme_key;
  73. if (strpos(current_path(), 'wysiwyg_theme/') !== 0) {
  74. return $elements;
  75. }
  76. $context = array('theme' => $theme_key);
  77. drupal_alter('wysiwyg_editor_styles', $elements, $context); $css = array();
  78. return $elements;
  79. }
  80. /**
  81. * Creates a cache of the stylesheets used by the currently set theme.
  82. *
  83. * Since this is a pre render callback for the styles element, it should run
  84. * late enough to catch all the stylesheets added just before the actual markup
  85. * for them is rendered.
  86. *
  87. * The first time this runs for a theme it's too late for a module to have any
  88. * use of the cache, so wysiwyg_get_css() uses drupal_http_request() to fetch a
  89. * dummy page, filling the cache before the original response is sent.
  90. *
  91. * Intended to run after Core has sorted/grouped/aggregated stylesheets.
  92. */
  93. function _wysiwyg_pre_render_styles($elements) {
  94. global $theme_key;
  95. if (strpos(current_path(), 'wysiwyg_theme/') !== 0) {
  96. return $elements;
  97. }
  98. $cached = cache_get('wysiwyg_css');
  99. foreach (element_children($elements) as $child) {
  100. if ($elements['#groups'][$child]['group'] != CSS_THEME) {
  101. continue;
  102. }
  103. switch ($elements[$child]['#tag']) {
  104. case 'link':
  105. $css[] = $elements[$child]['#attributes']['href'];
  106. break;
  107. case 'style':
  108. if (!empty($elements[$child]['#attributes']['href'])) {
  109. $css[] = $elements[$child]['#attributes']['href'];
  110. }
  111. elseif (!empty($elements[$child]['#value'])){
  112. preg_match_all('/\@import url\("([^"]+)"\);/', $elements[$child]['#value'], $matches, PREG_SET_ORDER);
  113. foreach ($matches as $val) {
  114. $css[] = $val[1];
  115. }
  116. }
  117. break;
  118. }
  119. $all = empty($cached->data) ? array() : $cached->data;
  120. $all[$theme_key] = array('files' => $css, 'aggregated' => variable_get('preprocess_css', FALSE));
  121. }
  122. $all['_css_js_query_string'] = variable_get('css_js_query_string');
  123. cache_set('wysiwyg_css', $all);
  124. return $elements;
  125. }