source: trunk/include/smarty/libs/plugins/function.html_image.php @ 23384

Last change on this file since 23384 was 23384, checked in by rvelices, 11 years ago

smarty 3 - first pass for tests

  • Property svn:eol-style set to LF
File size: 5.2 KB
Line 
1<?php
2/**
3 * Smarty plugin
4 *
5 * @package Smarty
6 * @subpackage PluginsFunction
7 */
8
9/**
10 * Smarty {html_image} function plugin
11 *
12 * Type:     function<br>
13 * Name:     html_image<br>
14 * Date:     Feb 24, 2003<br>
15 * Purpose:  format HTML tags for the image<br>
16 * Examples: {html_image file="/images/masthead.gif"}<br>
17 * Output:   <img src="/images/masthead.gif" width=400 height=23><br>
18 * Params:
19 * <pre>
20 * - file        - (required) - file (and path) of image
21 * - height      - (optional) - image height (default actual height)
22 * - width       - (optional) - image width (default actual width)
23 * - basedir     - (optional) - base directory for absolute paths, default is environment variable DOCUMENT_ROOT
24 * - path_prefix - prefix for path output (optional, default empty)
25 * </pre>
26 *
27 * @link http://www.smarty.net/manual/en/language.function.html.image.php {html_image}
28 *      (Smarty online manual)
29 * @author Monte Ohrt <monte at ohrt dot com>
30 * @author credits to Duda <duda@big.hu>
31 * @version 1.0
32 * @param array                    $params   parameters
33 * @param Smarty_Internal_Template $template template object
34 * @return string
35 * @uses smarty_function_escape_special_chars()
36 */
37function smarty_function_html_image($params, $template)
38{
39    require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
40 
41    $alt = '';
42    $file = '';
43    $height = '';
44    $width = '';
45    $extra = '';
46    $prefix = '';
47    $suffix = '';
48    $path_prefix = '';
49    $basedir = isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : '';
50    foreach($params as $_key => $_val) {
51        switch ($_key) {
52            case 'file':
53            case 'height':
54            case 'width':
55            case 'dpi':
56            case 'path_prefix':
57            case 'basedir':
58                $$_key = $_val;
59                break;
60
61            case 'alt':
62                if (!is_array($_val)) {
63                    $$_key = smarty_function_escape_special_chars($_val);
64                } else {
65                    throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
66                } 
67                break;
68
69            case 'link':
70            case 'href':
71                $prefix = '<a href="' . $_val . '">';
72                $suffix = '</a>';
73                break;
74
75            default:
76                if (!is_array($_val)) {
77                    $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
78                } else {
79                    throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
80                } 
81                break;
82        } 
83    } 
84
85    if (empty($file)) {
86        trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
87        return;
88    } 
89
90    if ($file[0] == '/') {
91        $_image_path = $basedir . $file;
92    } else {
93        $_image_path = $file;
94    }
95   
96    // strip file protocol
97    if (stripos($params['file'], 'file://') === 0) {
98        $params['file'] = substr($params['file'], 7);
99    }
100   
101    $protocol = strpos($params['file'], '://');
102    if ($protocol !== false) {
103        $protocol = strtolower(substr($params['file'], 0, $protocol));
104    }
105   
106    if (isset($template->smarty->security_policy)) {
107        if ($protocol) {
108            // remote resource (or php stream, …)
109            if(!$template->smarty->security_policy->isTrustedUri($params['file'])) {
110                return;
111            }
112        } else {
113            // local file
114            if(!$template->smarty->security_policy->isTrustedResourceDir($params['file'])) {
115                return;
116            }
117        }
118    }
119
120    if (!isset($params['width']) || !isset($params['height'])) {
121        // FIXME: (rodneyrehm) getimagesize() loads the complete file off a remote resource, use custom [jpg,png,gif]header reader!
122        if (!$_image_data = @getimagesize($_image_path)) {
123            if (!file_exists($_image_path)) {
124                trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
125                return;
126            } else if (!is_readable($_image_path)) {
127                trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
128                return;
129            } else {
130                trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
131                return;
132            } 
133        }
134
135        if (!isset($params['width'])) {
136            $width = $_image_data[0];
137        } 
138        if (!isset($params['height'])) {
139            $height = $_image_data[1];
140        } 
141    } 
142
143    if (isset($params['dpi'])) {
144        if (strstr($_SERVER['HTTP_USER_AGENT'], 'Mac')) {
145            // FIXME: (rodneyrehm) wrong dpi assumption
146            // don't know who thought this up… even if it was true in 1998, it's definitely wrong in 2011.
147            $dpi_default = 72;
148        } else {
149            $dpi_default = 96;
150        } 
151        $_resize = $dpi_default / $params['dpi'];
152        $width = round($width * $_resize);
153        $height = round($height * $_resize);
154    } 
155
156    return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"' . $extra . ' />' . $suffix;
157} 
158
159?>
Note: See TracBrowser for help on using the repository browser.