]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/plugins/function.html_image.php
Release 6.5.0
[Github/sugarcrm.git] / include / Smarty / plugins / function.html_image.php
1 <?php
2
3 /*
4
5 Modification information for LGPL compliance
6
7 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
8
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
10
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
12
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
14
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
16
17 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
18
19 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
20
21 r10971 - 2006-01-12 14:58:30 -0800 (Thu, 12 Jan 2006) - chris - Bug 4128: updating Smarty templates to 2.6.11, a version supposedly that plays better with PHP 5.1
22
23 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
24
25
26 */
27
28
29 /**
30  * Smarty plugin
31  * @package Smarty
32  * @subpackage plugins
33  */
34
35
36 /**
37  * Smarty {html_image} function plugin
38  *
39  * Type:     function<br>
40  * Name:     html_image<br>
41  * Date:     Feb 24, 2003<br>
42  * Purpose:  format HTML tags for the image<br>
43  * Input:<br>
44  *         - file = file (and path) of image (required)
45  *         - height = image height (optional, default actual height)
46  *         - width = image width (optional, default actual width)
47  *         - basedir = base directory for absolute paths, default
48  *                     is environment variable DOCUMENT_ROOT
49  *         - path_prefix = prefix for path output (optional, default empty)
50  *
51  * Examples: {html_image file="/images/masthead.gif"}
52  * Output:   <img src="/images/masthead.gif" width=400 height=23>
53  * @link http://smarty.php.net/manual/en/language.function.html.image.php {html_image}
54  *      (Smarty online manual)
55  * @author   Monte Ohrt <monte at ohrt dot com>
56  * @author credits to Duda <duda@big.hu> - wrote first image function
57  *           in repository, helped with lots of functionality
58  * @version  1.0
59  * @param array
60  * @param Smarty
61  * @return string
62  * @uses smarty_function_escape_special_chars()
63  */
64 function smarty_function_html_image($params, &$smarty)
65 {
66     require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
67     
68     $alt = '';
69     $file = '';
70     $height = '';
71     $width = '';
72     $extra = '';
73     $prefix = '';
74     $suffix = '';
75     $path_prefix = '';
76     $server_vars = ($smarty->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
77     $basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : '';
78     foreach($params as $_key => $_val) {
79         switch($_key) {
80             case 'file':
81             case 'height':
82             case 'width':
83             case 'dpi':
84             case 'path_prefix':
85             case 'basedir':
86                 $$_key = $_val;
87                 break;
88
89             case 'alt':
90                 if(!is_array($_val)) {
91                     $$_key = smarty_function_escape_special_chars($_val);
92                 } else {
93                     $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
94                 }
95                 break;
96
97             case 'link':
98             case 'href':
99                 $prefix = '<a href="' . $_val . '">';
100                 $suffix = '</a>';
101                 break;
102
103             default:
104                 if(!is_array($_val)) {
105                     $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
106                 } else {
107                     $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
108                 }
109                 break;
110         }
111     }
112
113     if (empty($file)) {
114         $smarty->trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
115         return;
116     }
117
118     if (substr($file,0,1) == '/') {
119         $_image_path = $basedir . $file;
120     } else {
121         $_image_path = $file;
122     }
123     
124     if(!isset($params['width']) || !isset($params['height'])) {
125         if(!$_image_data = @getimagesize($_image_path)) {
126             if(!file_exists($_image_path)) {
127                 $smarty->trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
128                 return;
129             } else if(!is_readable($_image_path)) {
130                 $smarty->trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
131                 return;
132             } else {
133                 $smarty->trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
134                 return;
135             }
136         }
137         if ($smarty->security &&
138             ($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) &&
139             (require_once(SMARTY_CORE_DIR . 'core.is_secure.php')) &&
140             (!smarty_core_is_secure($_params, $smarty)) ) {
141             $smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE);
142         }        
143         
144         if(!isset($params['width'])) {
145             $width = $_image_data[0];
146         }
147         if(!isset($params['height'])) {
148             $height = $_image_data[1];
149         }
150
151     }
152
153     if(isset($params['dpi'])) {
154         if(strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) {
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="'.$height.'"'.$extra.' />' . $suffix;
165 }
166
167 /* vim: set expandtab: */
168
169 ?>