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