function.html_image.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {html_image} function plugin
  10. * Type: function<br>
  11. * Name: html_image<br>
  12. * Date: Feb 24, 2003<br>
  13. * Purpose: format HTML tags for the image<br>
  14. * Examples: {html_image file="/images/masthead.gif"}<br>
  15. * Output: <img src="/images/masthead.gif" width=400 height=23><br>
  16. * Params:
  17. * <pre>
  18. * - file - (required) - file (and path) of image
  19. * - height - (optional) - image height (default actual height)
  20. * - width - (optional) - image width (default actual width)
  21. * - basedir - (optional) - base directory for absolute paths, default is environment variable DOCUMENT_ROOT
  22. * - path_prefix - prefix for path output (optional, default empty)
  23. * </pre>
  24. *
  25. * @link http://www.smarty.net/manual/en/language.function.html.image.php {html_image}
  26. * (Smarty online manual)
  27. * @author Monte Ohrt <monte at ohrt dot com>
  28. * @author credits to Duda <duda@big.hu>
  29. * @version 1.0
  30. *
  31. * @param array $params parameters
  32. * @param Smarty_Internal_Template $template template object
  33. *
  34. * @throws SmartyException
  35. * @return string
  36. * @uses smarty_function_escape_special_chars()
  37. */
  38. function smarty_function_html_image($params, $template)
  39. {
  40. if (!isset($template->smarty->_cache[ '_required_sesc' ])) {
  41. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  42. $template->smarty->_cache[ '_required_sesc' ] = true;
  43. }
  44. $alt = '';
  45. $file = '';
  46. $height = '';
  47. $width = '';
  48. $extra = '';
  49. $prefix = '';
  50. $suffix = '';
  51. $path_prefix = '';
  52. $basedir = isset($_SERVER[ 'DOCUMENT_ROOT' ]) ? $_SERVER[ 'DOCUMENT_ROOT' ] : '';
  53. foreach ($params as $_key => $_val) {
  54. switch ($_key) {
  55. case 'file':
  56. case 'height':
  57. case 'width':
  58. case 'dpi':
  59. case 'path_prefix':
  60. case 'basedir':
  61. $$_key = $_val;
  62. break;
  63. case 'alt':
  64. if (!is_array($_val)) {
  65. $$_key = smarty_function_escape_special_chars($_val);
  66. } else {
  67. throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  68. }
  69. break;
  70. case 'link':
  71. case 'href':
  72. $prefix = '<a href="' . $_val . '">';
  73. $suffix = '</a>';
  74. break;
  75. default:
  76. if (!is_array($_val)) {
  77. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  78. } else {
  79. throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  80. }
  81. break;
  82. }
  83. }
  84. if (empty($file)) {
  85. trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
  86. return;
  87. }
  88. if ($file[ 0 ] == '/') {
  89. $_image_path = $basedir . $file;
  90. } else {
  91. $_image_path = $file;
  92. }
  93. // strip file protocol
  94. if (stripos($params[ 'file' ], 'file://') === 0) {
  95. $params[ 'file' ] = substr($params[ 'file' ], 7);
  96. }
  97. $protocol = strpos($params[ 'file' ], '://');
  98. if ($protocol !== false) {
  99. $protocol = strtolower(substr($params[ 'file' ], 0, $protocol));
  100. }
  101. if (isset($template->smarty->security_policy)) {
  102. if ($protocol) {
  103. // remote resource (or php stream, …)
  104. if (!$template->smarty->security_policy->isTrustedUri($params[ 'file' ])) {
  105. return;
  106. }
  107. } else {
  108. // local file
  109. if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) {
  110. return;
  111. }
  112. }
  113. }
  114. if (!isset($params[ 'width' ]) || !isset($params[ 'height' ])) {
  115. // FIXME: (rodneyrehm) getimagesize() loads the complete file off a remote resource, use custom [jpg,png,gif]header reader!
  116. if (!$_image_data = @getimagesize($_image_path)) {
  117. if (!file_exists($_image_path)) {
  118. trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
  119. return;
  120. } elseif (!is_readable($_image_path)) {
  121. trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
  122. return;
  123. } else {
  124. trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
  125. return;
  126. }
  127. }
  128. if (!isset($params[ 'width' ])) {
  129. $width = $_image_data[ 0 ];
  130. }
  131. if (!isset($params[ 'height' ])) {
  132. $height = $_image_data[ 1 ];
  133. }
  134. }
  135. if (isset($params[ 'dpi' ])) {
  136. if (strstr($_SERVER[ 'HTTP_USER_AGENT' ], 'Mac')) {
  137. // FIXME: (rodneyrehm) wrong dpi assumption
  138. // don't know who thought this up… even if it was true in 1998, it's definitely wrong in 2011.
  139. $dpi_default = 72;
  140. } else {
  141. $dpi_default = 96;
  142. }
  143. $_resize = $dpi_default / $params[ 'dpi' ];
  144. $width = round($width * $_resize);
  145. $height = round($height * $_resize);
  146. }
  147. return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' .
  148. $height . '"' . $extra . ' />' . $suffix;
  149. }