smarty_function_html_image()   F
last analyzed

Complexity

Conditions 31
Paths 9882

Size

Total Lines 121
Code Lines 89

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 31
eloc 89
c 1
b 0
f 0
nc 9882
nop 2
dl 0
loc 121
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Smarty plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsFunction
7
 */
8
/**
9
 * Smarty {html_image} function plugin
10
 * Type:     function
11
 * Name:     html_image
12
 * Date:     Feb 24, 2003
13
 * Purpose:  format HTML tags for the image
14
 * Examples: {html_image file="/images/masthead.gif"}
15
 * Output:   <img src="/images/masthead.gif" width=400 height=23>
16
 * Params:
17
 *
18
 * - file        - (required) - file (and path) of image
19
 * - height      - (optional) - image height (default actual height)
20
 * - width       - (optional) - image width (default actual width)
21
 * - basedir     - (optional) - base directory for absolute paths, default is environment variable DOCUMENT_ROOT
22
 * - path_prefix - prefix for path output (optional, default empty)
23
 *
24
 * @link    http://www.smarty.net/manual/en/language.function.html.image.php {html_image}
25
 *          (Smarty online manual)
26
 * @author  Monte Ohrt <monte at ohrt dot com>
27
 * @author  credits to Duda <[email protected]>
28
 * @version 1.0
29
 *
30
 * @param array                    $params   parameters
31
 * @param Smarty_Internal_Template $template template object
32
 *
33
 * @throws SmartyException
34
 * @return string
35
 * @uses    smarty_function_escape_special_chars()
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)) {
0 ignored issues
show
introduced by
The condition empty($file) is always true.
Loading history...
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
    // strip file protocol
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
            // remote resource (or php stream, …)
113
            if (!$template->smarty->security_policy->isTrustedUri($params[ 'file' ])) {
114
                return;
115
            }
116
        } else {
117
            // local file
118
            if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) {
119
                return;
120
            }
121
        }
122
    }
123
    if (!isset($params[ 'width' ]) || !isset($params[ 'height' ])) {
124
        // FIXME: (rodneyrehm) getimagesize() loads the complete file off a remote resource, use custom [jpg,png,gif]header reader!
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
            // FIXME: (rodneyrehm) wrong dpi assumption
147
            // don't know who thought this up… even if it was true in 1998, it's definitely wrong in 2011.
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