PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/csslib.php

https://bitbucket.org/moodle/moodle
PHP | 209 lines | 100 code | 21 blank | 88 comment | 9 complexity | ccc704d5c9d965bebd822a757dbe7259 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This file contains CSS file serving functions.
  18. *
  19. * NOTE: these functions are not expected to be used from any addons.
  20. *
  21. * @package core
  22. * @copyright 2012 Sam Hemelryk
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
  27. // This can be also set in config.php file,
  28. // it needs to be higher than the time it takes to generate all CSS content.
  29. define('THEME_DESIGNER_CACHE_LIFETIME', 10);
  30. }
  31. /**
  32. * Stores CSS in a file at the given path.
  33. *
  34. * This function either succeeds or throws an exception.
  35. *
  36. * @param theme_config $theme The theme that the CSS belongs to.
  37. * @param string $csspath The path to store the CSS at.
  38. * @param string $csscontent the complete CSS in one string.
  39. */
  40. function css_store_css(theme_config $theme, $csspath, $csscontent) {
  41. global $CFG;
  42. clearstatcache();
  43. if (!file_exists(dirname($csspath))) {
  44. @mkdir(dirname($csspath), $CFG->directorypermissions, true);
  45. }
  46. // Prevent serving of incomplete file from concurrent request,
  47. // the rename() should be more atomic than fwrite().
  48. ignore_user_abort(true);
  49. // First up write out the single file for all those using decent browsers.
  50. css_write_file($csspath, $csscontent);
  51. ignore_user_abort(false);
  52. if (connection_aborted()) {
  53. die;
  54. }
  55. }
  56. /**
  57. * Writes a CSS file.
  58. *
  59. * @param string $filename
  60. * @param string $content
  61. */
  62. function css_write_file($filename, $content) {
  63. global $CFG;
  64. if ($fp = fopen($filename.'.tmp', 'xb')) {
  65. fwrite($fp, $content);
  66. fclose($fp);
  67. rename($filename.'.tmp', $filename);
  68. @chmod($filename, $CFG->filepermissions);
  69. @unlink($filename.'.tmp'); // Just in case anything fails.
  70. }
  71. }
  72. /**
  73. * Sends a cached CSS file
  74. *
  75. * This function sends the cached CSS file. Remember it is generated on the first
  76. * request, then optimised/minified, and finally cached for serving.
  77. *
  78. * @param string $csspath The path to the CSS file we want to serve.
  79. * @param string $etag The revision to make sure we utilise any caches.
  80. */
  81. function css_send_cached_css($csspath, $etag) {
  82. // 90 days only - based on Moodle point release cadence being every 3 months.
  83. $lifetime = 60 * 60 * 24 * 90;
  84. header('Etag: "'.$etag.'"');
  85. header('Content-Disposition: inline; filename="styles.php"');
  86. header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($csspath)) .' GMT');
  87. header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
  88. header('Pragma: ');
  89. header('Cache-Control: public, max-age='.$lifetime.', immutable');
  90. header('Accept-Ranges: none');
  91. header('Content-Type: text/css; charset=utf-8');
  92. if (!min_enable_zlib_compression()) {
  93. header('Content-Length: '.filesize($csspath));
  94. }
  95. readfile($csspath);
  96. die;
  97. }
  98. /**
  99. * Sends a cached CSS content
  100. *
  101. * @param string $csscontent The actual CSS markup.
  102. * @param string $etag The revision to make sure we utilise any caches.
  103. */
  104. function css_send_cached_css_content($csscontent, $etag) {
  105. // 90 days only - based on Moodle point release cadence being every 3 months.
  106. $lifetime = 60 * 60 * 24 * 90;
  107. header('Etag: "'.$etag.'"');
  108. header('Content-Disposition: inline; filename="styles.php"');
  109. header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
  110. header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
  111. header('Pragma: ');
  112. header('Cache-Control: public, max-age='.$lifetime.', immutable');
  113. header('Accept-Ranges: none');
  114. header('Content-Type: text/css; charset=utf-8');
  115. if (!min_enable_zlib_compression()) {
  116. header('Content-Length: '.strlen($csscontent));
  117. }
  118. echo($csscontent);
  119. die;
  120. }
  121. /**
  122. * Sends CSS directly and disables all caching.
  123. * The Content-Length of the body is also included, but the script is not ended.
  124. *
  125. * @param string $css The CSS content to send
  126. */
  127. function css_send_temporary_css($css) {
  128. header('Cache-Control: no-cache, no-store, must-revalidate');
  129. header('Pragma: no-cache');
  130. header('Expires: 0');
  131. header('Content-Disposition: inline; filename="styles_debug.php"');
  132. header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
  133. header('Accept-Ranges: none');
  134. header('Content-Type: text/css; charset=utf-8');
  135. header('Content-Length: ' . strlen($css));
  136. echo $css;
  137. }
  138. /**
  139. * Sends CSS directly without caching it.
  140. *
  141. * This function takes a raw CSS string, optimises it if required, and then
  142. * serves it.
  143. * Turning both themedesignermode and CSS optimiser on at the same time is awful
  144. * for performance because of the optimiser running here. However it was done so
  145. * that theme designers could utilise the optimised output during development to
  146. * help them optimise their CSS... not that they should write lazy CSS.
  147. *
  148. * @param string $css
  149. */
  150. function css_send_uncached_css($css) {
  151. header('Content-Disposition: inline; filename="styles_debug.php"');
  152. header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
  153. header('Expires: '. gmdate('D, d M Y H:i:s', time() + THEME_DESIGNER_CACHE_LIFETIME) .' GMT');
  154. header('Pragma: ');
  155. header('Accept-Ranges: none');
  156. header('Content-Type: text/css; charset=utf-8');
  157. if (is_array($css)) {
  158. $css = implode("\n\n", $css);
  159. }
  160. echo $css;
  161. die;
  162. }
  163. /**
  164. * Send file not modified headers
  165. *
  166. * @param int $lastmodified
  167. * @param string $etag
  168. */
  169. function css_send_unmodified($lastmodified, $etag) {
  170. // 90 days only - based on Moodle point release cadence being every 3 months.
  171. $lifetime = 60 * 60 * 24 * 90;
  172. header('HTTP/1.1 304 Not Modified');
  173. header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
  174. header('Cache-Control: public, max-age='.$lifetime);
  175. header('Content-Type: text/css; charset=utf-8');
  176. header('Etag: "'.$etag.'"');
  177. if ($lastmodified) {
  178. header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
  179. }
  180. die;
  181. }
  182. /**
  183. * Sends a 404 message about CSS not being found.
  184. */
  185. function css_send_css_not_found() {
  186. header('HTTP/1.0 404 not found');
  187. die('CSS was not found, sorry.');
  188. }