| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 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: |
|
| 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: |
|
| 113: | if (!$template->smarty->security_policy->isTrustedUri($params[ 'file' ])) {
|
| 114: | return;
|
| 115: | }
|
| 116: | } else {
|
| 117: |
|
| 118: | if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) {
|
| 119: | return;
|
| 120: | }
|
| 121: | }
|
| 122: | }
|
| 123: | if (!isset($params[ 'width' ]) || !isset($params[ 'height' ])) {
|
| 124: |
|
| 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: |
|
| 147: |
|
| 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: | }
|
| 159: | |