Passed
Push — 1.5.0 ( 946be9...8419f9 )
by Sylver
04:51
created

controller::create_img()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 74
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 49
c 0
b 0
f 0
dl 0
loc 74
rs 7.246
cc 10
nc 10
nop 9

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * @author		Sylver35 <[email protected]>
5
 * @package		Breizh Smilie Creator Extension
6
 * @copyright	(c) 2019-2025 Sylver35  https://breizhcode.com
7
 * @license		http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 */
9
10
namespace sylver35\smilecreator\controller;
11
use phpbb\config\config;
12
use phpbb\request\request;
13
use phpbb\controller\helper;
14
use phpbb\template\template;
15
use phpbb\language\language;
16
use phpbb\exception\http_exception;
17
18
class controller
19
{
20
	/** @var \phpbb\config\config */
21
	protected $config;
22
23
	/** @var \phpbb\request\request */
24
	protected $request;
25
26
	/** @var \phpbb\controller\helper */
27
	protected $helper;
28
29
	/** @var \phpbb\template\template */
30
	protected $template;
31
32
	/** @var \phpbb\language\language */
33
	protected $language;
34
35
	/** @var string phpBB root path */
36
	protected $root_path;
37
38
	/** @var string phpEx */
39
	protected $php_ext;
40
41
	/** @var string ext path */
42
	protected $ext_path;
43
44
	/** @var string ext path web */
45
	protected $ext_path_web;
46
47
	/**
48
	 * Controller constructor
49
	 */
50
	public function __construct(config $config, request $request, helper $helper, template $template, language $language, $root_path, $php_ext)
51
	{
52
		$this->config = $config;
53
		$this->request = $request;
54
		$this->helper = $helper;
55
		$this->template = $template;
56
		$this->language = $language;
57
		$this->root_path = $root_path;
58
		$this->php_ext = $php_ext;
59
		$this->ext_path = $root_path . 'ext/sylver35/smilecreator/';
60
		$this->ext_path_web = generate_board_url() . '/ext/sylver35/smilecreator/';
61
	}
62
63
	public function create_smiley()
64
	{
65
		$this->language->add_lang('smilie_creator', 'sylver35/smilecreator');
66
		//Get all available smilies ( *.png )
67
		if (!function_exists('filelist'))
68
		{
69
			include($this->root_path . 'includes/functions_admin.' . $this->php_ext);
70
		}
71
		$imglist = filelist($this->ext_path . 'images/', '', 'png');
72
73
		$j = $i = 0;
74
		foreach ($imglist as $key => $images)
75
		{
76
			natcasesort($images);
77
			foreach ($images as $img)
78
			{
79
				if ($img == 'schild.png')
80
				{
81
					continue;
82
				}
83
				//If we have more than 5 smilies in a row, create a new row
84
				$rows = false;
85
				if ($j === 5)
86
				{
87
					$rows = true;
88
					$j = 0;
89
				}
90
				$value = substr($img, 0, strrpos($img, '.'));
91
				$number = str_replace('smilie', '', $value);
92
93
				// Put the smileys into the table
94
				$this->template->assign_block_vars('smileys', [
95
					'NR'		=> $i,
96
					'SRC'		=> $img,
97
					'TITLE'		=> $value,
98
					'SELECT'	=> $number,
99
					'ROWS'		=> $rows,
100
				]);
101
				$i++;
102
				$j++;
103
			}
104
		}
105
		if ((int) $this->config['smiliecreator_count'] !== $i)
106
		{
107
			$this->config->set('smiliecreator_count', $i);
108
		}
109
110
		$this->template->assign_vars([
111
			'S_IN_CREATE_SMILEY'	=> true,
112
			'SMILEY_RANDOM'			=> $i + 1,
113
			'SELECT_FONTCOLOR'		=> $this->build_select(true),
114
			'SELECT_SHADOWCOLOR'	=> $this->build_select(false),
115
			'SMILEYS_SRC'			=> $this->ext_path_web . 'images/',
116
		]);
117
118
		return $this->helper->render('smilie_creator.html', $this->language->lang('SMILIE_CREATOR'));
119
	}
120
121
	public function display_smiley()
122
	{
123
		$text = (string) $this->request->variable('text', '', true);
124
		$smiley = (int) $this->request->variable('smiley', 0);
125
		$fontcolor = (string) $this->request->variable('fontcolor', '');
126
		$shadowcolor = (string) $this->request->variable('shadowcolor', '');
127
		$shieldshadow = (int) $this->request->variable('shieldshadow', 0);
128
		$fontwidth = 6;
129
		$fontheight = 11;
130
		$this->language->add_lang('smilie_creator', 'sylver35/smilecreator');
131
132
		// Smilie no exist ?
133
		if ($smiley > $this->config['smiliecreator_count'])
134
		{
135
			$smiley = 0;
136
		}
137
		// We have a random smilie ?
138
		if ($smiley === 0)
139
		{
140
			$smiley = mt_rand(1, (int) $this->config['smiliecreator_count'] - 1);
141
		}
142
143
		// See if the debug mode is wanted
144
		$debug = (strrpos($text, 'debug-mode') !== false) ? true : false;
145
146
		// Clean the text before
147
		$text = $this->clean_text($text);
148
149
		$output = [];
150
		$char_count = 40;
151
		$nb = mb_strlen($text);
152
		if ($nb > 40)
153
		{
154
			$output[0] = substr($text, 0, 40);
155
			if ($nb > 120)
156
			{
157
				$output[1] = substr($text, 40, 40);
158
				$output[2] = substr($text, 80, 37) . '...';
159
			}
160
			else if ($nb > 80)
161
			{
162
				$output[1] = substr($text, 40, 40);
163
				$output[2] = substr($text, 80, 40);
164
			}
165
			else
166
			{
167
				$output[1] = substr($text, 40, 40);
168
			}
169
		}
170
		else
171
		{
172
			$char_count = $nb;
173
			$output[0] = $text;
174
		}
175
176
		// Maybe we have to tweak here a bit. Depends on the font...
177
		$in = ($char_count * $fontwidth) + 14;
178
		$width = ($in < 60) ? 60 : $in;
179
		$out = sizeof($output);
180
		$height = ($out * $fontheight) + 35;
181
182
		// Main work here
183
		// Create image and send it to the browser
184
		$image = @imagepng($this->create_img($smiley, $width, $height, $fontcolor, $shadowcolor, $shieldshadow, $fontwidth, $fontheight, $output), NULL, -1, PNG_ALL_FILTERS);
185
		header('Pragma: public');
186
		header('Expires: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
187
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
188
		header('Content-Disposition: inline; filename="smilecreator-' . $text . '.png"');
189
		header('Cache-Control: maxage=10');
190
191
		// A good place for debug here if wanted
192
		if ($debug !== false)
193
		{
194
			header('Content-Type: text/html');
195
		}
196
		else
197
		{
198
			header('Content-Type: image/png');
199
			if ($filesize = filesize($image) !== false)
0 ignored issues
show
Bug introduced by
$image of type boolean is incompatible with the type string expected by parameter $filename of filesize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

199
			if ($filesize = filesize(/** @scrutinizer ignore-type */ $image) !== false)
Loading history...
200
			{
201
				header('Content-Length: ' . $filesize);
0 ignored issues
show
Bug introduced by
Are you sure $filesize of type true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

201
				header('Content-Length: ' . /** @scrutinizer ignore-type */ $filesize);
Loading history...
202
			}
203
		}
204
205
		$this->template->assign_var('SMILEY', $image);
206
207
		$this->template->set_filenames([
208
			'body'	=> '@sylver35_smilecreator/smiley.html'
209
		]);
210
211
		garbage_collection();
212
		exit_handler();
213
	}
214
215
	private function build_select($type)
216
	{
217
		$list = [
218
			'silver'	=> 'C0C0C0',
219
			'darkred'	=> '8B0000',
220
			'red'		=> 'FF0000',
221
			'orange'	=> 'FFA500',
222
			'brown'		=> 'A52A2A',
223
			'green'		=> '00AA00',
224
			'olive'		=> '808000',
225
			'cyan'		=> '00FFFF',
226
			'blue'		=> '0000FF',
227
			'darkblue'	=> '00008B',
228
			'indigo'	=> '4B0082',
229
			'violet'	=> 'EE82EE',
230
			'black'		=> '000000',
231
			'yellow'	=> 'FFFF00',
232
			'white'		=> 'FFFFFF',
233
		];
234
235
		$select = '<option style="color: black;background-color: white;" value="' . (($type) ? '000000' : '0') . '">' . $this->language->lang($type ? 'COLOR_DEFAULT' : 'SHADOWCOLOR_NO') . '</option>';
236
237
		foreach ($list as $color => $code)
238
		{
239
			$background = ($color === 'white' || $color === 'yellow') ? 'black' : 'white';
240
			$select .= '<option name="' . $color . '" style="color: #' . $code . ';background-color: ' . $background . ';" value="' . $code . '">' . $this->language->lang('COLOR_' . strtoupper($color)) . '</option>';
241
		}
242
243
		return $select;
244
	}
245
246
	private function clean_text($text)
247
	{
248
		return  str_replace(
249
			['  ', 'é', 'ê', 'è', 'ë', 'É', 'Ê', 'È', 'Ë', 'à', 'â', 'ä', 'ã', 'À', 'Â', 'Ä', 'Ã', 'î', 'ï', 'Î', 'Ï', 'ó', 'ò', 'ô', 'ö', 'õ', 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', 'ù', 'û', 'ü', 'Ù', 'Û', 'Ü', 'ç', 'Ç', 'ñ', 'Ñ'],
250
			[' ', 'e', 'e', 'e', 'e', 'E', 'E', 'E', 'E', 'a', 'a', 'a', 'a', 'A', 'A', 'A', 'A', 'i', 'i', 'I', 'I', 'o', 'o', 'o', 'o', 'o', 'O', 'O', 'O', 'O', 'O', 'u', 'u', 'u', 'U', 'U', 'U', 'c', 'c', 'n', 'n'],
251
			str_replace(["'", '"', ',', ';', '%', '€', '£', '?', '=', '&lt;', '&gt;', '&quot;', '&amp;', '<', '>', '&'], '', $text)
252
		);
253
	}
254
255
	private function clean_nb($nb, $range)
256
	{
257
		return (($nb + $range) > 255) ? 255 : $nb + $range;
258
	}
259
260
	private function create_img($smiley, $width, $height, $fontcolor, $shadowcolor, $shieldshadow, $fontwidth, $fontheight, $output)
261
	{
262
		$image = @imagecreate($width, $height);
263
		$smiley = @imagecreatefrompng($this->ext_path . 'images/smilie' . $smiley . '.png');
264
		$schild = @imagecreatefrompng($this->ext_path . 'images/schild.png');
265
266
		if ($image === false || $smiley === false || $schild === false)
267
		{
268
			throw new http_exception(400, 'CREATE_ERROR');
269
		}
270
271
		$r1 = (int) hexdec(substr($fontcolor, 0, 2));
272
		$g1 = (int) hexdec(substr($fontcolor, 2, 2));
273
		$b1 = (int) hexdec(substr($fontcolor, 4, 2));
274
		$r2 = (int) hexdec(substr($shadowcolor, 0, 2));
275
		$g2 = (int) hexdec(substr($shadowcolor, 2, 2));
276
		$b2 = (int) hexdec(substr($shadowcolor, 4, 2));
277
		$bgcolor = imagecolorallocate($image, 111, 252, 134);
278
		$txtcolor = imagecolorallocate($image, $r1, $g1, $b1);
279
		$txt2color = imagecolorallocate($image, $r2, $g2, $b2);
280
		$bocolor = imagecolorallocate($image, 0, 0, 0);
281
		$schcolor = imagecolorallocate($image, 255, 255, 255);
282
		$shadow1color = imagecolorallocate($image, 235, 235, 235);
283
		$shadow2color = imagecolorallocate($image, 219, 219, 219);
284
		$smileycolor = imagecolorsforindex($smiley, imagecolorat($smiley, 5, 14));
285
286
		imagesetpixel($schild, 1, 14, imagecolorallocate($schild, $this->clean_nb($smileycolor['red'], 52), $this->clean_nb($smileycolor['green'], 59), $this->clean_nb($smileycolor['blue'], 11)));
287
		imagesetpixel($schild, 2, 14, imagecolorallocate($schild, $this->clean_nb($smileycolor['red'], 50), $this->clean_nb($smileycolor['green'], 52), $this->clean_nb($smileycolor['blue'], 50)));
288
		imagesetpixel($schild, 1, 15, imagecolorallocate($schild, $this->clean_nb($smileycolor['red'], 50), $this->clean_nb($smileycolor['green'], 52), $this->clean_nb($smileycolor['blue'], 50)));
289
		imagesetpixel($schild, 2, 15, imagecolorallocate($schild, $this->clean_nb($smileycolor['red'], 22), $this->clean_nb($smileycolor['green'], 21), $this->clean_nb($smileycolor['blue'], 35)));
290
		imagesetpixel($schild, 1, 16, imagecolorat($smiley, 5, 14));
291
		imagesetpixel($schild, 2, 16, imagecolorat($smiley, 5, 14));
292
		imagesetpixel($schild, 5, 16, imagecolorallocate($schild, $this->clean_nb($smileycolor['red'], 22), $this->clean_nb($smileycolor['green'], 21), $this->clean_nb($smileycolor['blue'], 35)));
293
		imagesetpixel($schild, 6, 16, imagecolorat($smiley, 5, 14));
294
		imagesetpixel($schild, 5, 15, imagecolorallocate($schild, $this->clean_nb($smileycolor['red'], 52), $this->clean_nb($smileycolor['green'], 59), $this->clean_nb($smileycolor['blue'], 11)));
295
		imagesetpixel($schild, 6, 15, imagecolorallocate($schild, $this->clean_nb($smileycolor['red'], 50), $this->clean_nb($smileycolor['green'], 52), $this->clean_nb($smileycolor['blue'], 50)));
296
297
		if (@imagecopy($image, $schild, ($width / 2 - 3), 0, 0, 0, 6, 4) === false)
298
		{
299
			throw new http_exception(400, 'CREATE_ERROR');
300
		}
301
		if (@imagecopy($image, $schild, ($width / 2 - 3), ($height - 24), 0, 5, 9, 17) === false)
302
		{
303
			throw new http_exception(400, 'CREATE_ERROR');
304
		}
305
		if (@imagecopy($image, $smiley, ($width / 2 + 6), ($height - 24), 0, 0, 23, 23) === false)
306
		{
307
			throw new http_exception(400, 'CREATE_ERROR');
308
		}
309
310
		imagefilledrectangle($image, 0, 4, $width, ($height - 25), $bocolor);
311
		imagefilledrectangle($image, 1, 5, ($width - 2), ($height - 26), $schcolor);
312
313
		if ($shieldshadow)
314
		{
315
			imagefilledpolygon($image, [(($width - 2) / 2 + ((($width - 2) / 4) - 3)), 5, (($width - 2) / 2 + ((($width - 2) / 4) + 3)), 5, (($width - 2) / 2 - ((($width - 2) / 4) - 3)), ($height - 26), (($width - 2) / 2 - ((($width - 2) / 4) + 3)), ($height - 26)], 4, $shadow1color);
316
			imagefilledpolygon($image, [(($width - 2) / 2 + ((($width - 2) / 4) + 4)), 5, ($width - 2), 5, ($width - 2), ($height - 26), (($width - 2) / 2 - ((($width - 2) / 4) - 4)), ($height - 26)], 4, $shadow2color);
317
		}
318
319
		$i = 0;
320
		while ($i < sizeof($output))
321
		{
322
			if ($shadowcolor)
323
			{
324
				imagestring($image, 2, (($width - (strlen(trim($output[$i])) * $fontwidth) - 2) / 2 + 1), ($i * $fontheight + 6), trim($output[$i]), $txt2color);
325
			}
326
			imagestring($image, 2, (($width - (strlen(trim($output[$i])) * $fontwidth) - 2) / 2), ($i * $fontheight + 5), trim($output[$i]), $txtcolor);
327
			$i++;
328
		}
329
330
		imagecolortransparent($image, $bgcolor);
331
		imageinterlace($image, 1);
332
333
		return $image;
334
	}
335
}
336