1 <?php |
|
2 /** |
|
3 * Smarty plugin |
|
4 * |
|
5 * @package Smarty |
|
6 * @subpackage PluginsFunction |
|
7 */ |
|
8 |
|
9 /** |
|
10 * Smarty {html_image} function plugin |
|
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 * |
|
32 * @param array $params parameters |
|
33 * @param Smarty_Internal_Template $template template object |
|
34 * |
|
35 * @throws SmartyException |
|
36 * @return string |
|
37 * @uses smarty_function_escape_special_chars() |
|
38 */ |
|
39 function smarty_function_html_image($params, $template) { |
|
40 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'); |
|
41 |
|
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 |
|
62 case 'alt': |
|
63 if (!is_array($_val)) { |
|
64 $$_key = smarty_function_escape_special_chars($_val); |
|
65 } else { |
|
66 throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
|
67 } |
|
68 break; |
|
69 |
|
70 case 'link': |
|
71 case 'href': |
|
72 $prefix = '<a href="' . $_val . '">'; |
|
73 $suffix = '</a>'; |
|
74 break; |
|
75 |
|
76 default: |
|
77 if (!is_array($_val)) { |
|
78 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"'; |
|
79 } else { |
|
80 throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
|
81 } |
|
82 break; |
|
83 } |
|
84 } |
|
85 |
|
86 if (empty($file)) { |
|
87 trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE); |
|
88 |
|
89 return; |
|
90 } |
|
91 |
|
92 if ($file[0] == '/') { |
|
93 $_image_path = $basedir . $file; |
|
94 } else { |
|
95 $_image_path = $file; |
|
96 } |
|
97 |
|
98 // strip file protocol |
|
99 if (stripos($params['file'], 'file://') === 0) { |
|
100 $params['file'] = substr($params['file'], 7); |
|
101 } |
|
102 |
|
103 $protocol = strpos($params['file'], '://'); |
|
104 if ($protocol !== false) { |
|
105 $protocol = strtolower(substr($params['file'], 0, $protocol)); |
|
106 } |
|
107 |
|
108 if (isset($template->smarty->security_policy)) { |
|
109 if ($protocol) { |
|
110 // remote resource (or php stream, …) |
|
111 if (!$template->smarty->security_policy->isTrustedUri($params['file'])) { |
|
112 return; |
|
113 } |
|
114 } else { |
|
115 // local file |
|
116 if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) { |
|
117 return; |
|
118 } |
|
119 } |
|
120 } |
|
121 |
|
122 if (!isset($params['width']) || !isset($params['height'])) { |
|
123 // FIXME: (rodneyrehm) getimagesize() loads the complete file off a remote resource, use custom [jpg,png,gif]header reader! |
|
124 if (!$_image_data = @getimagesize($_image_path)) { |
|
125 if (!file_exists($_image_path)) { |
|
126 trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE); |
|
127 |
|
128 return; |
|
129 } elseif (!is_readable($_image_path)) { |
|
130 trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE); |
|
131 |
|
132 return; |
|
133 } else { |
|
134 trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE); |
|
135 |
|
136 return; |
|
137 } |
|
138 } |
|
139 |
|
140 if (!isset($params['width'])) { |
|
141 $width = $_image_data[0]; |
|
142 } |
|
143 if (!isset($params['height'])) { |
|
144 $height = $_image_data[1]; |
|
145 } |
|
146 } |
|
147 |
|
148 if (isset($params['dpi'])) { |
|
149 if (strstr($_SERVER['HTTP_USER_AGENT'], 'Mac')) { |
|
150 // FIXME: (rodneyrehm) wrong dpi assumption |
|
151 // don't know who thought this up… even if it was true in 1998, it's definitely wrong in 2011. |
|
152 $dpi_default = 72; |
|
153 } else { |
|
154 $dpi_default = 96; |
|
155 } |
|
156 $_resize = $dpi_default / $params['dpi']; |
|
157 $width = round($width * $_resize); |
|
158 $height = round($height * $_resize); |
|
159 } |
|
160 |
|
161 return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"' . $extra . ' />' . $suffix; |
|
162 } |
|