You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function.html_image.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {html_image} function plugin
  10. * Type: function
  11. * Name: html_image
  12. * Date: Feb 24, 2003
  13. * Purpose: format HTML tags for the image
  14. * Examples: {html_image file="/images/masthead.gif"}
  15. * Output: <img src="/images/masthead.gif" width=400 height=23>
  16. * Params:
  17. *
  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. *
  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, Smarty_Internal_Template $template)
  39. {
  40. $template->_checkPlugins(array(array('function' => 'smarty_function_escape_special_chars',
  41. 'file' => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php')));
  42. $alt = '';
  43. $file = '';
  44. $height = '';
  45. $width = '';
  46. $extra = '';
  47. $prefix = '';
  48. $suffix = '';
  49. $path_prefix = '';
  50. $basedir = isset($_SERVER[ 'DOCUMENT_ROOT' ]) ? $_SERVER[ 'DOCUMENT_ROOT' ] : '';
  51. foreach ($params as $_key => $_val) {
  52. switch ($_key) {
  53. case 'file':
  54. case 'height':
  55. case 'width':
  56. case 'dpi':
  57. case 'path_prefix':
  58. case 'basedir':
  59. $$_key = $_val;
  60. break;
  61. case 'alt':
  62. if (!is_array($_val)) {
  63. $$_key = smarty_function_escape_special_chars($_val);
  64. } else {
  65. throw new SmartyException ("html_image: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
  66. }
  67. break;
  68. case 'link':
  69. case 'href':
  70. $prefix = '<a href="' . $_val . '">';
  71. $suffix = '</a>';
  72. break;
  73. default:
  74. if (!is_array($_val)) {
  75. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  76. } else {
  77. throw new SmartyException ("html_image: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
  78. }
  79. break;
  80. }
  81. }
  82. if (empty($file)) {
  83. trigger_error('html_image: missing \'file\' parameter', E_USER_NOTICE);
  84. return;
  85. }
  86. if ($file[ 0 ] === '/') {
  87. $_image_path = $basedir . $file;
  88. } else {
  89. $_image_path = $file;
  90. }
  91. // strip file protocol
  92. if (stripos($params[ 'file' ], 'file://') === 0) {
  93. $params[ 'file' ] = substr($params[ 'file' ], 7);
  94. }
  95. $protocol = strpos($params[ 'file' ], '://');
  96. if ($protocol !== false) {
  97. $protocol = strtolower(substr($params[ 'file' ], 0, $protocol));
  98. }
  99. if (isset($template->smarty->security_policy)) {
  100. if ($protocol) {
  101. // remote resource (or php stream, …)
  102. if (!$template->smarty->security_policy->isTrustedUri($params[ 'file' ])) {
  103. return;
  104. }
  105. } else {
  106. // local file
  107. if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) {
  108. return;
  109. }
  110. }
  111. }
  112. if (!isset($params[ 'width' ]) || !isset($params[ 'height' ])) {
  113. // FIXME: (rodneyrehm) getimagesize() loads the complete file off a remote resource, use custom [jpg,png,gif]header reader!
  114. if (!$_image_data = @getimagesize($_image_path)) {
  115. if (!file_exists($_image_path)) {
  116. trigger_error("html_image: unable to find '{$_image_path}'", E_USER_NOTICE);
  117. return;
  118. } elseif (!is_readable($_image_path)) {
  119. trigger_error("html_image: unable to read '{$_image_path}'", E_USER_NOTICE);
  120. return;
  121. } else {
  122. trigger_error("html_image: '{$_image_path}' is not a valid image file", E_USER_NOTICE);
  123. return;
  124. }
  125. }
  126. if (!isset($params[ 'width' ])) {
  127. $width = $_image_data[ 0 ];
  128. }
  129. if (!isset($params[ 'height' ])) {
  130. $height = $_image_data[ 1 ];
  131. }
  132. }
  133. if (isset($params[ 'dpi' ])) {
  134. if (strstr($_SERVER[ 'HTTP_USER_AGENT' ], 'Mac')) {
  135. // FIXME: (rodneyrehm) wrong dpi assumption
  136. // don't know who thought this up… even if it was true in 1998, it's definitely wrong in 2011.
  137. $dpi_default = 72;
  138. } else {
  139. $dpi_default = 96;
  140. }
  141. $_resize = $dpi_default / $params[ 'dpi' ];
  142. $width = round($width * $_resize);
  143. $height = round($height * $_resize);
  144. }
  145. return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' .
  146. $height . '"' . $extra . ' />' . $suffix;
  147. }