PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/core/model/smarty/plugins/function.html_image.php

http://github.com/modxcms/revolution
PHP | 158 lines | 117 code | 0 blank | 41 comment | 25 complexity | 623a82749ea392afb1e7a7fe8e3f6c2f MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  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. * @link http://www.smarty.net/manual/en/language.function.html.image.php {html_image}
  25. * (Smarty online manual)
  26. * @author Monte Ohrt <monte at ohrt dot com>
  27. * @author credits to Duda <duda@big.hu>
  28. * @version 1.0
  29. *
  30. * @param array $params parameters
  31. * @param Smarty_Internal_Template $template template object
  32. *
  33. * @throws SmartyException
  34. * @return string
  35. * @uses smarty_function_escape_special_chars()
  36. */
  37. function smarty_function_html_image($params, Smarty_Internal_Template $template)
  38. {
  39. $template->_checkPlugins(
  40. array(
  41. array(
  42. 'function' => 'smarty_function_escape_special_chars',
  43. 'file' => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'
  44. )
  45. )
  46. );
  47. $alt = '';
  48. $file = '';
  49. $height = '';
  50. $width = '';
  51. $extra = '';
  52. $prefix = '';
  53. $suffix = '';
  54. $path_prefix = '';
  55. $basedir = isset($_SERVER[ 'DOCUMENT_ROOT' ]) ? $_SERVER[ 'DOCUMENT_ROOT' ] : '';
  56. foreach ($params as $_key => $_val) {
  57. switch ($_key) {
  58. case 'file':
  59. case 'height':
  60. case 'width':
  61. case 'dpi':
  62. case 'path_prefix':
  63. case 'basedir':
  64. $$_key = $_val;
  65. break;
  66. case 'alt':
  67. if (!is_array($_val)) {
  68. $$_key = smarty_function_escape_special_chars($_val);
  69. } else {
  70. throw new SmartyException(
  71. "html_image: extra attribute '{$_key}' cannot be an array",
  72. E_USER_NOTICE
  73. );
  74. }
  75. break;
  76. case 'link':
  77. case 'href':
  78. $prefix = '<a href="' . $_val . '">';
  79. $suffix = '</a>';
  80. break;
  81. default:
  82. if (!is_array($_val)) {
  83. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  84. } else {
  85. throw new SmartyException(
  86. "html_image: extra attribute '{$_key}' cannot be an array",
  87. E_USER_NOTICE
  88. );
  89. }
  90. break;
  91. }
  92. }
  93. if (empty($file)) {
  94. trigger_error('html_image: missing \'file\' parameter', E_USER_NOTICE);
  95. return;
  96. }
  97. if ($file[ 0 ] === '/') {
  98. $_image_path = $basedir . $file;
  99. } else {
  100. $_image_path = $file;
  101. }
  102. // strip file protocol
  103. if (stripos($params[ 'file' ], 'file://') === 0) {
  104. $params[ 'file' ] = substr($params[ 'file' ], 7);
  105. }
  106. $protocol = strpos($params[ 'file' ], '://');
  107. if ($protocol !== false) {
  108. $protocol = strtolower(substr($params[ 'file' ], 0, $protocol));
  109. }
  110. if (isset($template->smarty->security_policy)) {
  111. if ($protocol) {
  112. // remote resource (or php stream, …)
  113. if (!$template->smarty->security_policy->isTrustedUri($params[ 'file' ])) {
  114. return;
  115. }
  116. } else {
  117. // local file
  118. if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) {
  119. return;
  120. }
  121. }
  122. }
  123. if (!isset($params[ 'width' ]) || !isset($params[ 'height' ])) {
  124. // FIXME: (rodneyrehm) getimagesize() loads the complete file off a remote resource, use custom [jpg,png,gif]header reader!
  125. if (!$_image_data = @getimagesize($_image_path)) {
  126. if (!file_exists($_image_path)) {
  127. trigger_error("html_image: unable to find '{$_image_path}'", E_USER_NOTICE);
  128. return;
  129. } elseif (!is_readable($_image_path)) {
  130. trigger_error("html_image: unable to read '{$_image_path}'", E_USER_NOTICE);
  131. return;
  132. } else {
  133. trigger_error("html_image: '{$_image_path}' is not a valid image file", E_USER_NOTICE);
  134. return;
  135. }
  136. }
  137. if (!isset($params[ 'width' ])) {
  138. $width = $_image_data[ 0 ];
  139. }
  140. if (!isset($params[ 'height' ])) {
  141. $height = $_image_data[ 1 ];
  142. }
  143. }
  144. if (isset($params[ 'dpi' ])) {
  145. if (strstr($_SERVER[ 'HTTP_USER_AGENT' ], 'Mac')) {
  146. // FIXME: (rodneyrehm) wrong dpi assumption
  147. // don't know who thought this up… even if it was true in 1998, it's definitely wrong in 2011.
  148. $dpi_default = 72;
  149. } else {
  150. $dpi_default = 96;
  151. }
  152. $_resize = $dpi_default / $params[ 'dpi' ];
  153. $width = round($width * $_resize);
  154. $height = round($height * $_resize);
  155. }
  156. return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' .
  157. $height . '"' . $extra . ' />' . $suffix;
  158. }