]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/plugins/function.html_image.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / plugins / function.html_image.php
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 {
41     if (!is_callable('smarty_function_escape_special_chars')) {
42         require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
43     }
44
45     $alt = '';
46     $file = '';
47     $height = '';
48     $width = '';
49     $extra = '';
50     $prefix = '';
51     $suffix = '';
52     $path_prefix = '';
53     $basedir = isset($_SERVER[ 'DOCUMENT_ROOT' ]) ? $_SERVER[ 'DOCUMENT_ROOT' ] : '';
54     foreach ($params as $_key => $_val) {
55         switch ($_key) {
56             case 'file':
57             case 'height':
58             case 'width':
59             case 'dpi':
60             case 'path_prefix':
61             case 'basedir':
62                 $$_key = $_val;
63                 break;
64
65             case 'alt':
66                 if (!is_array($_val)) {
67                     $$_key = smarty_function_escape_special_chars($_val);
68                 } else {
69                     throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
70                 }
71                 break;
72
73             case 'link':
74             case 'href':
75                 $prefix = '<a href="' . $_val . '">';
76                 $suffix = '</a>';
77                 break;
78
79             default:
80                 if (!is_array($_val)) {
81                     $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
82                 } else {
83                     throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
84                 }
85                 break;
86         }
87     }
88
89     if (empty($file)) {
90         trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
91
92         return;
93     }
94
95     if ($file[ 0 ] == '/') {
96         $_image_path = $basedir . $file;
97     } else {
98         $_image_path = $file;
99     }
100
101     // strip file protocol
102     if (stripos($params[ 'file' ], 'file://') === 0) {
103         $params[ 'file' ] = substr($params[ 'file' ], 7);
104     }
105
106     $protocol = strpos($params[ 'file' ], '://');
107     if ($protocol !== false) {
108         $protocol = strtolower(substr($params[ 'file' ], 0, $protocol));
109     }
110
111     if (isset($template->smarty->security_policy)) {
112         if ($protocol) {
113             // remote resource (or php stream, …)
114             if (!$template->smarty->security_policy->isTrustedUri($params[ 'file' ])) {
115                 return;
116             }
117         } else {
118             // local file
119             if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) {
120                 return;
121             }
122         }
123     }
124
125     if (!isset($params[ 'width' ]) || !isset($params[ 'height' ])) {
126         // FIXME: (rodneyrehm) getimagesize() loads the complete file off a remote resource, use custom [jpg,png,gif]header reader!
127         if (!$_image_data = @getimagesize($_image_path)) {
128             if (!file_exists($_image_path)) {
129                 trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
130
131                 return;
132             } elseif (!is_readable($_image_path)) {
133                 trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
134
135                 return;
136             } else {
137                 trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
138
139                 return;
140             }
141         }
142
143         if (!isset($params[ 'width' ])) {
144             $width = $_image_data[ 0 ];
145         }
146         if (!isset($params[ 'height' ])) {
147             $height = $_image_data[ 1 ];
148         }
149     }
150
151     if (isset($params[ 'dpi' ])) {
152         if (strstr($_SERVER[ 'HTTP_USER_AGENT' ], 'Mac')) {
153             // FIXME: (rodneyrehm) wrong dpi assumption
154             // don't know who thought this up… even if it was true in 1998, it's definitely wrong in 2011.
155             $dpi_default = 72;
156         } else {
157             $dpi_default = 96;
158         }
159         $_resize = $dpi_default / $params[ 'dpi' ];
160         $width = round($width * $_resize);
161         $height = round($height * $_resize);
162     }
163
164     return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' .
165            $height . '"' . $extra . ' />' . $suffix;
166 }