function.html_image.php 5.2 KB

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