1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_forms\Controls\Security\Captcha; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use ArrayAccess; |
7
|
|
|
use kalanis\kw_forms\Exceptions\RenderException; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AGraphical |
12
|
|
|
* @package kalanis\kw_forms\Controls\Security\Captcha |
13
|
|
|
* Graphical captcha |
14
|
|
|
*/ |
15
|
|
|
abstract class AGraphical extends ACaptcha |
16
|
|
|
{ |
17
|
|
|
protected $templateLabel = '<img src="data:image/png;base64,%2$s" id="%1$s" alt="You need to solve this." />'; |
18
|
|
|
protected $templateInput = '<input type="text" value=""%2$s />'; |
19
|
|
|
/** @var string */ |
20
|
|
|
protected $font = ''; |
21
|
|
|
/** @var string */ |
22
|
|
|
protected $renderError = 'Cannot render captcha image!'; |
23
|
|
|
/** @var ArrayAccess */ |
24
|
|
|
protected $session = null; |
25
|
|
|
|
26
|
8 |
|
protected function fillSession(string $alias, ArrayAccess &$session, string $text): void |
27
|
|
|
{ |
28
|
8 |
|
$stringNow = $alias . '_now'; |
29
|
8 |
|
$stringLast = $alias . '_last'; |
30
|
|
|
|
31
|
8 |
|
$session->offsetSet($stringLast, ($session->offsetExists($stringNow) ? $session->offsetGet($stringNow) : null)); |
32
|
8 |
|
$session->offsetSet($stringNow, $text); |
33
|
8 |
|
$this->session = & $session; |
34
|
8 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Render label on form control |
38
|
|
|
* @param string|array<string, string> $attributes |
39
|
|
|
* @throws RenderException |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function renderLabel($attributes = array()): string |
43
|
|
|
{ |
44
|
|
|
if ($this->canPass()) { |
45
|
|
|
return ''; |
46
|
|
|
} |
47
|
|
|
return $this->wrapIt(sprintf($this->templateLabel, $this->getAttribute('id'), $this->getImage(strval($this->getLabel())), $this->renderAttributes($attributes)), $this->wrappersLabel); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $text |
52
|
|
|
* @throws RenderException |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
protected function getImage(string $text): string |
56
|
|
|
{ |
57
|
|
|
$im = imagecreatetruecolor(160, 25); |
58
|
|
|
|
59
|
|
|
if (false === $im) { |
60
|
|
|
// @codeCoverageIgnoreStart |
61
|
|
|
// problems with gd library |
62
|
|
|
throw new RenderException($this->renderError); |
63
|
|
|
} |
64
|
|
|
// @codeCoverageIgnoreEnd |
65
|
|
|
|
66
|
|
|
$white = intval(imagecolorallocate($im, 255, 255, 255)); |
67
|
|
|
$grey = intval(imagecolorallocate($im, 169, 169, 169)); |
68
|
|
|
$black = intval(imagecolorallocate($im, 0, 0, 0)); |
69
|
|
|
imagefilledrectangle($im, 0, 0, 160, 25, $white); |
70
|
|
|
|
71
|
|
|
imagettftext($im, 20, 0, 9, 19, $black, $this->font, $text); |
72
|
|
|
imagettftext($im, 20, 0, 11, 21, $black, $this->font, $text); |
73
|
|
|
imagettftext($im, 20, 0, 10, 20, $black, $this->font, $text); |
74
|
|
|
|
75
|
|
|
for ($i = 0; 3 > $i; $i++) { |
76
|
|
|
imageline($im, 0, $i * 10, 400, $i * 10, $grey); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
for ($i = 0; 16 > $i; $i++) { |
80
|
|
|
imageline($im, $i * 10, 0, $i * 10, 30, $grey); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
ob_start(); |
84
|
|
|
imagepng($im); |
85
|
|
|
$img = strval(ob_get_contents()); |
86
|
|
|
ob_end_clean(); |
87
|
|
|
|
88
|
|
|
imagedestroy($im); |
89
|
|
|
|
90
|
|
|
return $img; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Generate and returns random string with combination of numbers and chars with specified length |
95
|
|
|
* @param int $stringLength |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
6 |
|
protected function generateRandomString(int $stringLength = 16): string |
99
|
|
|
{ |
100
|
6 |
|
$all = ['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i', |
101
|
|
|
'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','!','$','%']; |
102
|
6 |
|
$string = ''; |
103
|
6 |
|
for ($i = 0; $i < $stringLength; $i++) { |
104
|
6 |
|
$rand = mt_rand(0, count($all) - 1); |
105
|
6 |
|
$string .= $all[$rand]; |
106
|
|
|
} |
107
|
|
|
//print_r(['CPT_>'=>$string]); |
108
|
6 |
|
return $string; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|