PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/theme/image.php

https://bitbucket.org/ngmares/moodle
PHP | 243 lines | 164 code | 37 blank | 42 comment | 33 complexity | fa3c775ad74ed58176bd6883dd6cdea0 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
  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 is responsible for serving the one theme and plugin images.
  18. *
  19. * @package moodlecore
  20. * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. // disable moodle specific debug messages and any errors in output,
  24. // comment out when debugging or better look into error log!
  25. define('NO_DEBUG_DISPLAY', true);
  26. // we need just the values from config.php and minlib.php
  27. define('ABORT_AFTER_CONFIG', true);
  28. require('../config.php'); // this stops immediately at the beginning of lib/setup.php
  29. if ($slashargument = min_get_slash_argument()) {
  30. $slashargument = ltrim($slashargument, '/');
  31. if (substr_count($slashargument, '/') < 3) {
  32. image_not_found();
  33. }
  34. // image must be last because it may contain "/"
  35. list($themename, $component, $rev, $image) = explode('/', $slashargument, 4);
  36. $themename = min_clean_param($themename, 'SAFEDIR');
  37. $component = min_clean_param($component, 'SAFEDIR');
  38. $rev = min_clean_param($rev, 'INT');
  39. $image = min_clean_param($image, 'SAFEPATH');
  40. } else {
  41. $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
  42. $component = min_optional_param('component', 'core', 'SAFEDIR');
  43. $rev = min_optional_param('rev', -1, 'INT');
  44. $image = min_optional_param('image', '', 'SAFEPATH');
  45. }
  46. if (empty($component) or $component === 'moodle' or $component === 'core') {
  47. $component = 'moodle';
  48. }
  49. if (empty($image)) {
  50. image_not_found();
  51. }
  52. if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
  53. // exists
  54. } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
  55. // exists
  56. } else {
  57. image_not_found();
  58. }
  59. $candidatelocation = "$CFG->cachedir/theme/$themename/pix/$component";
  60. $etag = sha1("$themename/$component/$rev/$image");
  61. if ($rev > -1) {
  62. if (file_exists("$candidatelocation/$image.error")) {
  63. // this is a major speedup if there are multiple missing images,
  64. // the only problem is that random requests may pollute our cache.
  65. image_not_found();
  66. }
  67. $cacheimage = false;
  68. if (file_exists("$candidatelocation/$image.gif")) {
  69. $cacheimage = "$candidatelocation/$image.gif";
  70. $ext = 'gif';
  71. } else if (file_exists("$candidatelocation/$image.png")) {
  72. $cacheimage = "$candidatelocation/$image.png";
  73. $ext = 'png';
  74. } else if (file_exists("$candidatelocation/$image.jpg")) {
  75. $cacheimage = "$candidatelocation/$image.jpg";
  76. $ext = 'jpg';
  77. } else if (file_exists("$candidatelocation/$image.jpeg")) {
  78. $cacheimage = "$candidatelocation/$image.jpeg";
  79. $ext = 'jpeg';
  80. } else if (file_exists("$candidatelocation/$image.ico")) {
  81. $cacheimage = "$candidatelocation/$image.ico";
  82. $ext = 'ico';
  83. }
  84. if ($cacheimage) {
  85. if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  86. // we do not actually need to verify the etag value because our files
  87. // never change in cache because we increment the rev parameter
  88. $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
  89. $mimetype = get_contenttype_from_ext($ext);
  90. header('HTTP/1.1 304 Not Modified');
  91. header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
  92. header('Cache-Control: public, max-age='.$lifetime);
  93. header('Content-Type: '.$mimetype);
  94. header('Etag: '.$etag);
  95. die;
  96. }
  97. send_cached_image($cacheimage, $etag);
  98. }
  99. }
  100. //=================================================================================
  101. // ok, now we need to start normal moodle script, we need to load all libs and $DB
  102. define('ABORT_AFTER_CONFIG_CANCEL', true);
  103. define('NO_MOODLE_COOKIES', true); // Session not used here
  104. define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
  105. require("$CFG->dirroot/lib/setup.php");
  106. $theme = theme_config::load($themename);
  107. $imagefile = $theme->resolve_image_location($image, $component);
  108. $rev = theme_get_revision();
  109. $etag = sha1("$themename/$component/$rev/$image");
  110. if (empty($imagefile) or !is_readable($imagefile)) {
  111. if ($rev > -1) {
  112. if (!file_exists($candidatelocation)) {
  113. @mkdir($candidatelocation, $CFG->directorypermissions, true);
  114. }
  115. // make note we can not find this file
  116. $cacheimage = "$candidatelocation/$image.error";
  117. $fp = fopen($cacheimage, 'w');
  118. fclose($fp);
  119. }
  120. image_not_found();
  121. }
  122. if ($rev > -1) {
  123. $pathinfo = pathinfo($imagefile);
  124. $cacheimage = "$candidatelocation/$image.".$pathinfo['extension'];
  125. clearstatcache();
  126. if (!file_exists(dirname($cacheimage))) {
  127. @mkdir(dirname($cacheimage), $CFG->directorypermissions, true);
  128. }
  129. // Prevent serving of incomplete file from concurrent request,
  130. // the rename() should be more atomic than copy().
  131. ignore_user_abort(true);
  132. if (@copy($imagefile, $cacheimage.'.tmp')) {
  133. rename($cacheimage.'.tmp', $cacheimage);
  134. @chmod($cacheimage, $CFG->filepermissions);
  135. @unlink($cacheimage.'.tmp'); // just in case anything fails
  136. }
  137. ignore_user_abort(false);
  138. if (connection_aborted()) {
  139. die;
  140. }
  141. // make sure nothing failed
  142. clearstatcache();
  143. if (file_exists($cacheimage)) {
  144. send_cached_image($cacheimage, $etag);
  145. }
  146. }
  147. send_uncached_image($imagefile);
  148. //=================================================================================
  149. //=== utility functions ==
  150. // we are not using filelib because we need to fine tune all header
  151. // parameters to get the best performance.
  152. function send_cached_image($imagepath, $etag) {
  153. global $CFG;
  154. require("$CFG->dirroot/lib/xsendfilelib.php");
  155. $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
  156. $pathinfo = pathinfo($imagepath);
  157. $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
  158. $mimetype = get_contenttype_from_ext($pathinfo['extension']);
  159. header('Etag: '.$etag);
  160. header('Content-Disposition: inline; filename="'.$imagename.'"');
  161. header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
  162. header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
  163. header('Pragma: ');
  164. header('Cache-Control: public, max-age='.$lifetime);
  165. header('Accept-Ranges: none');
  166. header('Content-Type: '.$mimetype);
  167. header('Content-Length: '.filesize($imagepath));
  168. if (xsendfile($imagepath)) {
  169. die;
  170. }
  171. // no need to gzip already compressed images ;-)
  172. readfile($imagepath);
  173. die;
  174. }
  175. function send_uncached_image($imagepath) {
  176. $pathinfo = pathinfo($imagepath);
  177. $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
  178. $mimetype = get_contenttype_from_ext($pathinfo['extension']);
  179. header('Content-Disposition: inline; filename="'.$imagename.'"');
  180. header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
  181. header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT');
  182. header('Pragma: ');
  183. header('Accept-Ranges: none');
  184. header('Content-Type: '.$mimetype);
  185. header('Content-Length: '.filesize($imagepath));
  186. readfile($imagepath);
  187. die;
  188. }
  189. function image_not_found() {
  190. header('HTTP/1.0 404 not found');
  191. die('Image was not found, sorry.');
  192. }
  193. function get_contenttype_from_ext($ext) {
  194. switch ($ext) {
  195. case 'gif':
  196. return 'image/gif';
  197. case 'png':
  198. return 'image/png';
  199. case 'jpg':
  200. case 'jpeg':
  201. return 'image/jpeg';
  202. case 'ico':
  203. return 'image/vnd.microsoft.icon';
  204. }
  205. return 'document/unknown';
  206. }