PageRenderTime 102ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/thumb.php

https://gitlab.com/mahara2/ranginuis-mahara
PHP | 216 lines | 168 code | 21 blank | 27 comment | 42 complexity | c92818e7627809d0998b48ffeabcffe9 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, MIT, GPL-3.0
  1. <?php
  2. /**
  3. *
  4. * @package mahara
  5. * @subpackage core
  6. * @author Catalyst IT Ltd
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
  8. * @copyright For copyright information on Mahara, please see the README file distributed with this software.
  9. *
  10. */
  11. define('INTERNAL', 1);
  12. define('PUBLIC', 1);
  13. define('NOCHECKREQUIREDFIELDS', 1);
  14. require('init.php');
  15. require_once('file.php');
  16. require_once('user.php');
  17. require_once('layoutpreviewimage.php');
  18. $type = param_alpha('type');
  19. switch ($type) {
  20. case 'profileiconbyid':
  21. case 'profileicon':
  22. $id = param_integer('id', 0);
  23. $size = get_imagesize_parameters();
  24. $earlyexpiry = param_boolean('earlyexpiry');
  25. $useremail = null;
  26. if ($id) {
  27. if ($type == 'profileicon') {
  28. // Convert ID of user to the ID of a profileicon
  29. $data = get_record_sql('
  30. SELECT u.profileicon, u.email, f.filetype
  31. FROM {usr} u LEFT JOIN {artefact_file_files} f ON u.profileicon = f.artefact
  32. WHERE u.id = ?', array($id));
  33. if (!empty($data->profileicon)) {
  34. $id = $data->profileicon;
  35. $mimetype = $data->filetype;
  36. }
  37. else {
  38. if ($useremail = $data->email) {
  39. // We can use the email address for gravatar icon
  40. $notfound = get_config('wwwroot').'thumb.php?type=profileiconbyid';
  41. foreach ($_GET as $k => $v) {
  42. if ($k != 'id' && $k != 'type') {
  43. $notfound .= '&' . $k . '=' . $v;
  44. }
  45. }
  46. }
  47. $id = null;
  48. }
  49. }
  50. else {
  51. $mimetype = get_field('artefact_file_files', 'filetype', 'artefact', $id);
  52. }
  53. }
  54. if ($id && $fileid = get_field('artefact_file_files', 'fileid', 'artefact', $id)) {
  55. if ($path = get_dataroot_image_path('artefact/file/profileicons', $fileid, $size)) {
  56. if ($mimetype) {
  57. header('Content-type: ' . $mimetype);
  58. if (!get_config('nocache')) {
  59. // We can't cache 'profileicon' for as long, because the
  60. // user can change it at any time. But we can cache
  61. // 'profileiconbyid' for quite a while, because it will
  62. // never change
  63. if ($type == 'profileiconbyid' and !$earlyexpiry) {
  64. $maxage = 604800; // 1 week
  65. }
  66. else {
  67. $maxage = 600; // 10 minutes
  68. }
  69. header('Expires: '. gmdate('D, d M Y H:i:s', time() + $maxage) .' GMT');
  70. header('Cache-Control: max-age=' . $maxage);
  71. header('Pragma: public');
  72. }
  73. readfile_exit($path);
  74. }
  75. }
  76. }
  77. // Look for an appropriate image on gravatar.com
  78. if ($useremail and $gravatarurl = remote_avatar($useremail, $size, $notfound)) {
  79. redirect($gravatarurl);
  80. }
  81. // We couldn't find an image for this user. Attempt to use the 'no user
  82. // photo' image for the current theme
  83. if (!get_config('nocache')) {
  84. // We can cache such images
  85. $maxage = 604800; // 1 week
  86. if ($earlyexpiry) {
  87. $maxage = 600; // 10 minutes
  88. }
  89. header('Expires: '. gmdate('D, d M Y H:i:s', time() + $maxage) .' GMT');
  90. header('Cache-Control: max-age=' . $maxage);
  91. header('Pragma: public');
  92. }
  93. if ($path = get_dataroot_image_path('artefact/file/profileicons/no_userphoto/' . $THEME->basename, 0, $size)) {
  94. header('Content-type: ' . 'image/png');
  95. readfile_exit($path);
  96. }
  97. // If we couldn't find the no user photo picture, we put it into
  98. // dataroot if we can
  99. $nouserphotopic = $THEME->get_path('images/no_userphoto.png');
  100. if ($nouserphotopic) {
  101. // Move the file into the correct place.
  102. $directory = get_config('dataroot') . 'artefact/file/profileicons/no_userphoto/' . $THEME->basename . '/originals/0/';
  103. check_dir_exists($directory);
  104. copy($nouserphotopic, $directory . '0');
  105. // Now we can try and get the image in the correct size
  106. if ($path = get_dataroot_image_path('artefact/file/profileicons/no_userphoto/' . $THEME->basename, 0, $size)) {
  107. header('Content-type: ' . 'image/png');
  108. readfile_exit($path);
  109. }
  110. }
  111. // Emergency fallback
  112. header('Content-type: ' . 'image/png');
  113. readfile_exit($THEME->get_path('images/no_userphoto.png'));
  114. case 'logobyid':
  115. $filedata = get_record('artefact_file_files', 'artefact', param_integer('id'));
  116. if ($path = get_dataroot_image_path('artefact/file/profileicons', $filedata->fileid, get_imagesize_parameters())) {
  117. if ($filedata->filetype) {
  118. header('Content-type: ' . $filedata->filetype);
  119. if (!get_config('nocache')) {
  120. $maxage = 604800;
  121. header('Expires: '. gmdate('D, d M Y H:i:s', time() + $maxage) .' GMT');
  122. header('Cache-Control: max-age=' . $maxage);
  123. header('Pragma: public');
  124. }
  125. readfile_exit($path);
  126. }
  127. }
  128. // Nothing found, use the site logo.
  129. header('Content-type: ' . 'image/png');
  130. readfile_exit($THEME->get_path('images/site-logo.png'));
  131. case 'blocktype':
  132. $bt = param_alpha('bt'); // blocktype
  133. $ap = param_alpha('ap', null); // artefact plugin (optional)
  134. $basepath = 'blocktype/' . $bt;
  135. if (!empty($ap)) {
  136. $basepath = 'artefact/' . $ap . '/' . $basepath;
  137. }
  138. header('Content-type: image/png');
  139. if (!get_config('nocache')) {
  140. $maxage = 604800;
  141. header('Expires: '. gmdate('D, d M Y H:i:s', time() + $maxage) .' GMT');
  142. header('Cache-Control: max-age=' . $maxage);
  143. header('Pragma: public');
  144. }
  145. $path = $THEME->get_path('images/thumb.png', false, $basepath);
  146. if (is_readable($path)) {
  147. readfile_exit($path);
  148. }
  149. $path = get_config('docroot') . $basepath . '/thumb.png';
  150. if (is_readable($path)) {
  151. readfile_exit($path);
  152. }
  153. readfile_exit($THEME->get_path('images/no_thumbnail.png'));
  154. case 'viewlayout':
  155. header('Content-type: image/png');
  156. $vl = param_integer('vl');
  157. $rows = get_records_sql_assoc('
  158. SELECT vlrc.row, vlc.widths
  159. FROM {view_layout_rows_columns} vlrc
  160. INNER JOIN {view_layout_columns} vlc ON (vlrc.columns = vlc.id)
  161. WHERE vlrc.viewlayout = ?
  162. ORDER BY vlrc.row ASC',
  163. array($vl));
  164. if ($rows) {
  165. $filename = 'vl-';
  166. foreach ($rows as $key => $row) {
  167. $filename .= str_replace(',', '-', $row->widths);
  168. $filename .= ($key == count($rows))? '.png' : '_';
  169. }
  170. if (($path = get_config('dataroot') . LayoutPreviewImage::$destinationfolder . '/' . $filename)
  171. && (is_readable($path))) {
  172. readfile_exit($path);
  173. }
  174. // look in theme folder for default layout thumbs, or dataroot folder for custom layout thumbs
  175. else if (($path = $THEME->get_path('images/' . $filename))
  176. && (is_readable($path))) {
  177. readfile_exit($path);
  178. }
  179. }
  180. readfile_exit($THEME->get_path('images/no_thumbnail.png'));
  181. case 'customviewlayout':
  182. header('Content-type: image/png');
  183. $cvl = param_variable('cvl');
  184. // dataroot folder for custom layout thumbs
  185. if (($path = get_config('dataroot') . LayoutPreviewImage::$destinationfolder . '/' . $cvl . '.png')
  186. && (is_readable($path))) {
  187. readfile_exit($path);
  188. }
  189. readfile_exit($THEME->get_path('images/no_thumbnail.png'));
  190. }
  191. function readfile_exit($path) {
  192. readfile($path);
  193. perf_to_log();
  194. exit;
  195. }