summaryrefslogtreecommitdiff
path: root/smartyplugins/function.biticon.php
blob: a6422b194e47c5111f9b7dcd3f3e3fa44788b79b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
namespace Bitweaver\Plugins;

use Bitweaver\KernelTools;

/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 * @link https://www.bitweaver.org/wiki/function_biticon function_biticon
 */

/**
 * biticon_first_match
 *
 * @param string $pDir Directory in which we want to search for the icon
 * @param string $pFilename Icon name without the extension
 * @access public
 * @return string|bool Icon name with extension on success, false on failure
 */
function biticon_first_match( $pDir, $pFilename, $pExtensions = ['svg', 'png', 'gif', 'jpg'] ) {
	if( is_dir( $pDir )) {
		foreach( $pExtensions as $ext ) {
			if( is_file( $pDir.$pFilename.'.'.$ext ) ) {
				return $pFilename.'.'.$ext;
			}
		}
	}
	return false;
}

/**
 * Turn collected information into an html image
 *
 * @param array $pParams
 * @var boolean ['url']		set to true if you only want the url and nothing else
 * @var string ['iexplain'] Explanation of what the icon represents
 * @var string ['iclass']
 * @var string ['iforce']	override site-wide setting how to display icons (can be set to 'icon', 'text' or 'icon_text')
 * @var string $pFile Path to icon file
 * @return string Full <img> on success
 */
function biticon_output( $pParams, $pFile ) {
	global $gBitSystem;
	$iexplain = isset( $pParams["iexplain"] ) ? KernelTools::tra( $pParams["iexplain"] ) : 'please set iexplain';

	if( empty( $pParams['iforce'] )) {
		$pParams['iforce'] = 'none';
	}

	if( isset( $pParams["url"] )) {
		$outstr = $pFile;
	} else {
		if( (empty( $pFile ) || $gBitSystem->getConfig( 'site_biticon_display_style' ) == 'text' || $pParams['iforce'] == 'text') && $pParams['iforce'] != 'icon' ) {
			$outstr =  $iexplain;
		} else {
			$outstr='<img src="'.$pFile.'"';
			if( isset( $pParams["iexplain"] ) ) {
				$outstr .= ' alt="'.KernelTools::tra( $pParams["iexplain"] ).'" title="'.KernelTools::tra( $pParams["iexplain"] ).'"';
			} else {
				$outstr .= ' alt=""';
			}

			$ommit = [ 'ilocation', 'ipackage', 'ipath', 'iname', 'iexplain', 'iforce', 'istyle', 'iclass', 'isize' ];
			foreach( $pParams as $name => $val ) {
				if( !in_array( $name, $ommit ) ) {
					$outstr .= ' '.$name.'="'.$val.'"';
				}
			}

			if( !isset( $pParams["iclass"] ) ) {
				$outstr .= ' class="icon"';
			} else {
				$outstr .= ' class="'.$pParams["iclass"].'"';
			}

			if( isset( $pParams["onclick"] ) ) {
				$outstr .=  ' onclick="'.$pParams["onclick"].'"';
			}

			if( str_ends_with( $pFile, '.svg' ) ) {
				$svgSizes = ['small' => 16, 'medium' => 24, 'large' => 32];
				$px = $svgSizes[$pParams['isize'] ?? 'small'] ?? 16;
				$outstr .= " width=\"$px\" height=\"$px\"";
			}

			$outstr .= " />";

			if( $gBitSystem->getConfig( 'site_biticon_display_style' ) == 'icon_text' && $pParams['iforce'] != 'icon' || $pParams['iforce'] == 'icon_text' ) {
				$outstr .= '&nbsp;'.$iexplain;
			}
		}
	}

	if( !preg_match( "#^broken\.#", $pFile )) {
		if( !biticon_write_cache( $pParams, $outstr )) {
			echo KernelTools::tra( 'There was a problem writing the icon cache file' );
		}
	}

	return $outstr;
}

/**
 * smarty_function_biticon
 *
 * @param array $pParams
 * @var boolean ['url']			set to true if you only want the url and nothing else
 * @var string  ['iexplain']	Explanation of what the icon represents
 * @var string  ['iclass']
 * @var string  ['iforce']		override site-wide setting how to display icons (can be set to 'icon', 'text' or 'icon_text')
 * @param bool $pCheckSmall look for a small render of the image
 * @access public
 * @return string final <img>
 */
function smarty_function_biticon( $pParams, $pSmall = false ) {
	global $gBitSystem, $gBitThemes, $gSniffer;

	// this is needed in case everything goes horribly wrong
	$copyParams = $pParams;

	// ensure that ipath has a leading and trailing slash
	$pParams['ipath'] = !empty( $pParams['ipath'] ) ? str_replace( "//", "/", "/".$pParams['ipath']."/" ) : '/';

	// try to separate iname from ipath if we've been given some sloppy naming
	if( strstr( $pParams['iname'], '/' )) {
		$pParams['iname'] = $pParams['ipath'].$pParams['iname'];
		$boom = explode( '/', $pParams['iname'] );
		$pParams['iname'] = array_pop( $boom );
		$pParams['ipath'] = str_replace( "//", "/", "/".implode( '/', $boom )."/" );
	}

	// if we don't have an ipath yet, we will set it here
	if( $pParams['ipath'] == '/' ) {
		$configSize = !empty( $pParams['isize'] ) ? $pParams['isize'] : $gBitSystem->getConfig( 'site_icon_size', 'small' );
		if( !empty( $pParams['ilocation'] )) {
			if( $pParams['ilocation'] == 'menu' ) {
				$pParams['ipath'] .= $configSize.'/';
				$pParams['iforce'] = 'icon_text';
			} elseif( $pParams['ilocation'] == 'quicktag' ) {
				$pParams['ipath'] .= $configSize.'/';
				$pParams['iforce'] = 'icon';
				$pParams['iclass'] = 'quicktag icon';
			}
		} else {
			$pParams['ipath'] .= $configSize.'/';
		}
	}

	// we have one special case: pkg_icons don't have a size variant
	if( strstr( $pParams['iname'], 'pkg_' ) && !strstr( $pParams['ipath'], 'small' )) {
		$pParams['ipath'] = preg_replace( "!/.*?/$!", "/", $pParams['ipath'] );
	}

	// make sure ipackage is set correctly
	$pParams['ipackage'] = !empty( $pParams['ipackage'] ) ? strtolower( $pParams['ipackage'] ?? '' ) :'icons';

	// if the user is using a text-browser we force text instead of icons
	if( $gSniffer->_browser_info['browser'] == 'lx' || $gSniffer->_browser_info['browser'] == 'li' ) {
		$pParams['iforce'] = 'text';
	}

	// get out of here as quickly as possible if we've already cached the icon information before
	if(( $ret = biticon_read_cache( $pParams )) && !( defined( 'TEMPLATE_DEBUG' ) && TEMPLATE_DEBUG == true )) {
		return $ret;
	}

	// first deal with most common scenario: icon style ( a selected iconset from util/iconsets/ )
	if( $pParams['ipackage'] == 'icons' ) {
		// get the current icon style
		// istyle is a private parameter!!! - only used on theme manager page for icon preview!!!
		// violators will be poked with soft cushions by the Cardinal himself!!!
		$icon_style = !empty( $pParams['istyle'] ) ? $pParams['istyle'] : $gBitSystem->getConfig( 'site_icon_style', DEFAULT_ICON_STYLE );

		// Iconsets are SVG-only: serve from scalable/ at ipath-derived dimensions.
		// ipath (/small/, /medium/, /large/) is preserved for cache key differentiation.
		$pParams['isize'] = trim( $pParams['ipath'], '/' );
		if( false !== ( $matchFile = biticon_first_match( UTIL_PKG_PATH."iconsets/$icon_style/scalable/", $pParams['iname'], ['svg'] ))) {
			return biticon_output( $pParams, UTIL_PKG_URL."iconsets/$icon_style/scalable/".$matchFile );
		}
		if( $icon_style != DEFAULT_ICON_STYLE && false !== ( $matchFile = biticon_first_match( UTIL_PKG_PATH."iconsets/".DEFAULT_ICON_STYLE."/scalable/", $pParams['iname'], ['svg'] ))) {
			return biticon_output( $pParams, UTIL_PKG_URL."iconsets/".DEFAULT_ICON_STYLE."/scalable/".$matchFile );
		}

		// if that didn't work, we'll try liberty
		$pParams['ipath'] = '/'.$gBitSystem->getConfig( 'site_icon_size', 'small' ).'/';
		$pParams['ipackage'] = 'liberty';
	}

	// since package icons reside in <pkg>/icons/ we don't need the small/ subdir
	if( strstr( "/small/", $pParams['ipath'] )) {
		$pParams['ipath'] = str_replace( "small/", "", $pParams['ipath'] );
		$small = true;
	}

	// first check themes/force
	if( false !== ( $matchFile = biticon_first_match( THEMES_PKG_PATH."force/icons/".$pParams['ipackage'].$pParams['ipath'], $pParams['iname'] ))) {
		return biticon_output( $pParams, BIT_ROOT_URL."themes/force/icons/".$pParams['ipackage'].$pParams['ipath'].$matchFile );
	}

	//if we have site styles, look there
	if( false !== ( $matchFile = biticon_first_match( $gBitThemes->getStylePath().'icons/'.$pParams['ipackage'].$pParams['ipath'], $pParams['iname'] ))) {
		return biticon_output( $pParams, $gBitThemes->getStyleUrl().'icons/'.$pParams['ipackage'].$pParams['ipath'].$matchFile );
	}

	//Well, then lets look in the package location
	if( false !== ( $matchFile = biticon_first_match( constant( strtoupper( $pParams['ipackage'] ).'_PKG_PATH' )."icons".$pParams['ipath'], $pParams['iname'] ))) {
		return biticon_output( $pParams, constant( strtoupper( $pParams['ipackage'] ).'_PKG_URL' )."icons".$pParams['ipath'].$matchFile );
	}

	// Still didn't find it! Well lets output something (return empty string if only the url is requested)
	if( isset( $pParams['url'] )) {
		return '';
	}
		if( empty( $pSmall ) ) {
			// if we were looking for the large icon, we'll try the whole kaboodle again, looking for the small icon
			$copyParams['ipath'] = preg_replace( "!/.*?/$!", "/small/", $pParams['ipath'] );
			return smarty_function_biticon( $copyParams, true );
		}
			return biticon_output( $pParams, '' );

}

/**
 * biticon_cache
 *
 * @param array $pParams
 * @access public
 * @return string cached icon string on sucess, false on failure
 */
function biticon_read_cache( $pParams ) {
	$ret = false;
	$cacheFile = biticon_get_cache_file( $pParams );
	if( is_readable( $cacheFile )) {
		if( $h = fopen( $cacheFile, 'r' )) {
			$ret = fread( $h, filesize( $cacheFile ));
			fclose( $h );
		}
	}

	return $ret;
}

/**
 * biticon_write_cache
 *
 * @param array $pParams
 * @access public
 * @return bool true on success, false on failure
 */
function biticon_write_cache( $pParams, $pCacheString ) {
	$ret = false;
	if( $cacheFile = biticon_get_cache_file( $pParams )) {
		if( $h = fopen( $cacheFile, 'w' )) {
			$ret = fwrite( $h, $pCacheString );
			fclose( $h );
		}
	}

	return $ret;
}

/**
 * will get the path to the cache files based on the stuff in $pParams
 *
 * @param array $pParams
 * @access public
 * @return string full path to cachefile
 */
function biticon_get_cache_file( $pParams ) {
	global $gBitThemes, $gBitSystem;

	// create a hash filename based on the parameters given
	$hashstring = '';
	$ihash = [ 'iforce', 'ipath', 'iname', 'iexplain', 'ipackage', 'url', 'istyle', 'id', 'style', 'onclick' ];
	foreach( $pParams as $param => $value ) {
		if( in_array( $param, $ihash )) {
			$hashstring .= strtolower( $value ?? '' );
		}
	}

	// return path to cache file
	return $gBitThemes->getIconCachePath().md5( $hashstring.$gBitSystem->getConfig( 'site_biticon_display_style', 'icon' ));
}