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:  38: 
 39: function smarty_function_html_image($params, $template)
 40: {
 41:     require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
 42: 
 43:     $alt = '';
 44:     $file = '';
 45:     $height = '';
 46:     $width = '';
 47:     $extra = '';
 48:     $prefix = '';
 49:     $suffix = '';
 50:     $path_prefix = '';
 51:     $basedir = isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : '';
 52:     foreach ($params as $_key => $_val) {
 53:         switch ($_key) {
 54:             case 'file':
 55:             case 'height':
 56:             case 'width':
 57:             case 'dpi':
 58:             case 'path_prefix':
 59:             case 'basedir':
 60:                 $$_key = $_val;
 61:                 break;
 62: 
 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: 
 71:             case 'link':
 72:             case 'href':
 73:                 $prefix = '<a href="' . $_val . '">';
 74:                 $suffix = '</a>';
 75:                 break;
 76: 
 77:             default:
 78:                 if (!is_array($_val)) {
 79:                     $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
 80:                 } else {
 81:                     throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
 82:                 }
 83:                 break;
 84:         }
 85:     }
 86: 
 87:     if (empty($file)) {
 88:         trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
 89: 
 90:         return;
 91:     }
 92: 
 93:     if ($file[0] == '/') {
 94:         $_image_path = $basedir . $file;
 95:     } else {
 96:         $_image_path = $file;
 97:     }
 98: 
 99:     
100:     if (stripos($params['file'], 'file://') === 0) {
101:         $params['file'] = substr($params['file'], 7);
102:     }
103: 
104:     $protocol = strpos($params['file'], '://');
105:     if ($protocol !== false) {
106:         $protocol = strtolower(substr($params['file'], 0, $protocol));
107:     }
108: 
109:     if (isset($template->smarty->security_policy)) {
110:         if ($protocol) {
111:             
112:             if (!$template->smarty->security_policy->isTrustedUri($params['file'])) {
113:                 return;
114:             }
115:         } else {
116:             
117:             if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) {
118:                 return;
119:             }
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: 
129:                 return;
130:             } elseif (!is_readable($_image_path)) {
131:                 trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
132: 
133:                 return;
134:             } else {
135:                 trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
136: 
137:                 return;
138:             }
139:         }
140: 
141:         if (!isset($params['width'])) {
142:             $width = $_image_data[0];
143:         }
144:         if (!isset($params['height'])) {
145:             $height = $_image_data[1];
146:         }
147:     }
148: 
149:     if (isset($params['dpi'])) {
150:         if (strstr($_SERVER['HTTP_USER_AGENT'], 'Mac')) {
151:             
152:             
153:             $dpi_default = 72;
154:         } else {
155:             $dpi_default = 96;
156:         }
157:         $_resize = $dpi_default / $params['dpi'];
158:         $width = round($width * $_resize);
159:         $height = round($height * $_resize);
160:     }
161: 
162:     return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"' . $extra . ' />' . $suffix;
163: }
164: