1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
captcha.php
См. документацию.
1<?php
2
3//Some marketplace modules define the same class
4if (!class_exists("CCaptcha")):
5
6class CCaptcha
7{
8 var $imageWidth = 180;
9 var $imageHeight = 40;
10 var $codeLength = 5;
11 var $ttfFilesPath = "/bitrix/modules/main/fonts";
12 var $arTTFFiles = ["font.ttf"];
13 var $textAngleFrom = -20;
14 var $textAngleTo = 20;
15 var $textStartX = 7;
16 var $textDistanceFrom = 27;
17 var $textDistanceTo = 32;
18 var $textFontSize = 20;
19 var $bTransparentText = true;
20 var $transparentTextPercent = 10;
21 var $arTextColor = [[0, 100], [0, 100], [0, 100]];
22 var $arBGColor = [[255, 255], [255, 255], [255, 255]];
23 var $arRealBGColor = false;
24 var $numEllipses = 100;
25 var $arEllipseColor = [[127, 255], [127, 255], [127, 255]];
26 var $numLines = 20;
27 var $arLineColor = [[110, 250], [110, 250], [110, 250]];
28 var $bLinesOverText = false;
29 var $arBorderColor = [0, 0, 0];
30 var $bWaveTransformation = false;
31 var $bEmptyText = false;
32 var $arChars = [
33 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
34 'N', 'P', 'Q', 'R', 'S', 'T'/*,'U'*//*,'V'*/, 'W', 'X', 'Y', 'Z',
35 '2', '3', '4', '5', '6', '7', '8', '9',
36 ];//'1','I','O','0'
37 var $image;
38 var $code;
39 var $codeCrypt;
40 var $sid;
41
42 public function __construct()
43 {
44 $this->transparentTextPercent = COption::GetOptionInt("main", "CAPTCHA_transparentTextPercent", 10);
45 $this->bTransparentText = $this->transparentTextPercent > 0;
46 $this->SetBGColorRGB(
47 COption::GetOptionString("main", "CAPTCHA_arBGColor_1", "FFFFFF"),
48 COption::GetOptionString("main", "CAPTCHA_arBGColor_2", "FFFFFF")
49 );
50 $this->SetEllipsesNumber(
51 COption::GetOptionInt("main", "CAPTCHA_numEllipses", 100)
52 );
53 $this->SetEllipseColorRGB(
54 COption::GetOptionString("main", "CAPTCHA_arEllipseColor_1", "7F7F7F"),
55 COption::GetOptionString("main", "CAPTCHA_arEllipseColor_2", "FFFFFF")
56 );
57 $this->SetLinesOverText(
58 COption::GetOptionString("main", "CAPTCHA_bLinesOverText", "N") === "Y"
59 );
60 $this->SetLinesNumber(
61 COption::GetOptionInt("main", "CAPTCHA_numLines", 20)
62 );
63 $this->SetLineColorRGB(
64 COption::GetOptionString("main", "CAPTCHA_arLineColor_1", "6E6E6E"),
65 COption::GetOptionString("main", "CAPTCHA_arLineColor_2", "FAFAFA")
66 );
67 $this->SetTextWriting(
68 COption::GetOptionInt("main", "CAPTCHA_textAngel_1", -20),
69 COption::GetOptionInt("main", "CAPTCHA_textAngel_2", 20),
70 COption::GetOptionInt("main", "CAPTCHA_textStartX", 7),
71 COption::GetOptionInt("main", "CAPTCHA_textDistance_1", 27),
72 COption::GetOptionInt("main", "CAPTCHA_textDistance_2", 32),
73 COption::GetOptionInt("main", "CAPTCHA_textFontSize", 20)
74 );
75 $this->SetTextColorRGB(
76 COption::GetOptionString("main", "CAPTCHA_arTextColor_1", "000000"),
77 COption::GetOptionString("main", "CAPTCHA_arTextColor_2", "646464")
78 );
79 $this->SetWaveTransformation(
80 COption::GetOptionString("main", "CAPTCHA_bWaveTransformation", "N") === "Y"
81 );
82 $this->SetEmptyText(
83 COption::GetOptionString("main", "CAPTCHA_bEmptyText", "N") === "Y"
84 );
85 $this->SetBorderColorRGB(
86 COption::GetOptionString("main", "CAPTCHA_arBorderColor", "000000")
87 );
88 $this->SetTTFFonts(
89 explode(",", COption::GetOptionString("main", "CAPTCHA_arTTFFiles", "font.ttf"))
90 );
91
92 $strChars = COption::GetOptionString("main", "CAPTCHA_letters", "ABCDEFGHJKLMNPQRSTWXYZ23456789");
93 $arChars = [];
94 for ($i = 0, $n = mb_strlen($strChars); $i < $n; $i++)
95 {
96 $arChars[] = mb_substr($strChars, $i, 1);
97 }
98 $this->SetCodeChars($arChars);
99 }
100
101 /* SET */
102 function SetImageSize($width, $height)
103 {
104 $width = intval($width);
105 $height = intval($height);
106
107 if ($width > 0)
108 {
109 $this->imageWidth = $width;
110 }
111
112 if ($height > 0)
113 {
114 $this->imageHeight = $height;
115 }
116 }
117
118 function SetCodeLength($length)
119 {
120 $length = intval($length);
121
122 if ($length > 0)
123 {
124 $this->codeLength = $length;
125 }
126 }
127
128 function SetTTFFontsPath($ttfFilesPath)
129 {
130 if ($ttfFilesPath <> '')
131 {
132 $filename = trim(str_replace("\\", "/", trim($ttfFilesPath)), "/");
133 $FILE_NAME = rel2abs($_SERVER["DOCUMENT_ROOT"], "/" . $filename);
134 if (mb_strlen($FILE_NAME) > 1 && is_dir($_SERVER["DOCUMENT_ROOT"] . $FILE_NAME))
135 {
136 $this->ttfFilesPath = $FILE_NAME;
137 }
138 }
139 }
140
141 function GetTTFFontsPath()
142 {
143 return $this->ttfFilesPath;
144 }
145
146 function SetTTFFonts($arFonts)
147 {
148 if (!is_array($arFonts) || empty($arFonts))
149 {
150 $arFonts = [];
151 }
152
153 $this->arTTFFiles = $arFonts;
154 }
155
156 function SetTextWriting($angleFrom, $angleTo, $startX, $distanceFrom, $distanceTo, $fontSize)
157 {
158 $angleFrom = intval($angleFrom);
159 $angleTo = intval($angleTo);
160 $startX = intval($startX);
161 $distanceFrom = intval($distanceFrom);
162 $distanceTo = intval($distanceTo);
163 $fontSize = intval($fontSize);
164
165 $this->textAngleFrom = $angleFrom;
166 $this->textAngleTo = $angleTo;
167
168 if ($startX > 0)
169 {
170 $this->textStartX = $startX;
171 }
172
173 if ($distanceFrom <> 0)
174 {
175 $this->textDistanceFrom = $distanceFrom;
176 }
177
178 if ($distanceTo <> 0)
179 {
180 $this->textDistanceTo = $distanceTo;
181 }
182
183 if ($fontSize > 0)
184 {
185 $this->textFontSize = $fontSize;
186 }
187 }
188
189 function SetTextTransparent($bTransparentText, $transparentTextPercent = 10)
190 {
191 $this->bTransparentText = (bool)$bTransparentText;
192 $this->transparentTextPercent = intval($transparentTextPercent);
193 }
194
195 function SetColor($arColor)
196 {
197 if (!is_array($arColor) || count($arColor) != 3)
198 {
199 return false;
200 }
201
202 $arNewColor = [];
203 $bCorrectColor = true;
204
205 for ($i = 0; $i < 3; $i++)
206 {
207 if (!is_array($arColor[$i]))
208 {
209 $arColor[$i] = [$arColor[$i]];
210 }
211
212 for ($j = 0; $j < 2; $j++)
213 {
214 if ($j > 0)
215 {
216 if (!array_key_exists($j, $arColor[$i]))
217 {
218 $arColor[$i][$j] = $arColor[$i][$j - 1];
219 }
220 }
221
222 $arColor[$i][$j] = intval($arColor[$i][$j]);
223 if ($arColor[$i][$j] < 0 || $arColor[$i][$j] > 255)
224 {
225 $bCorrectColor = false;
226 break;
227 }
228
229 if ($j > 0)
230 {
231 if ($arColor[$i][$j] < $arColor[$i][$j - 1])
232 {
233 $bCorrectColor = false;
234 break;
235 }
236 }
237
238 $arNewColor[$i][$j] = $arColor[$i][$j];
239
240 if ($j > 0)
241 {
242 break;
243 }
244 }
245 }
246
247 if ($bCorrectColor)
248 {
249 return $arNewColor;
250 }
251
252 return false;
253 }
254
255 function SetBGColor($arColor)
256 {
257 if ($arNewColor = $this->SetColor($arColor))
258 {
259 $this->arBGColor = $arNewColor;
260 $this->arRealBGColor = false;
261 }
262 }
263
264 function SetBGColorRGB($color_1, $color_2)
265 {
266 if (preg_match("/^[0-9A-Fa-f]{6}$/", $color_1) && preg_match("/^[0-9A-Fa-f]{6}$/", $color_1))
267 {
268 $arColor = [
269 [hexdec(mb_substr($color_1, 0, 2)), hexdec(mb_substr($color_2, 0, 2))],
270 [hexdec(mb_substr($color_1, 2, 2)), hexdec(mb_substr($color_2, 2, 2))],
271 [hexdec(mb_substr($color_1, 4, 2)), hexdec(mb_substr($color_2, 4, 2))],
272 ];
273 $this->SetBGColor($arColor);
274 }
275 }
276
277 function SetTextColor($arColor)
278 {
279 if ($arNewColor = $this->SetColor($arColor))
280 {
281 $this->arTextColor = $arNewColor;
282 }
283 }
284
285 function SetTextColorRGB($color_1, $color_2)
286 {
287 if (preg_match("/^[0-9A-Fa-f]{6}$/", $color_1) && preg_match("/^[0-9A-Fa-f]{6}$/", $color_1))
288 {
289 $arColor = [
290 [hexdec(mb_substr($color_1, 0, 2)), hexdec(mb_substr($color_2, 0, 2))],
291 [hexdec(mb_substr($color_1, 2, 2)), hexdec(mb_substr($color_2, 2, 2))],
292 [hexdec(mb_substr($color_1, 4, 2)), hexdec(mb_substr($color_2, 4, 2))],
293 ];
294 $this->SetTextColor($arColor);
295 }
296 }
297
298 function SetEllipseColor($arColor)
299 {
300 if ($arNewColor = $this->SetColor($arColor))
301 {
302 $this->arEllipseColor = $arNewColor;
303 }
304 }
305
306 function SetEllipseColorRGB($color_1, $color_2)
307 {
308 if (preg_match("/^[0-9A-Fa-f]{6}$/", $color_1) && preg_match("/^[0-9A-Fa-f]{6}$/", $color_1))
309 {
310 $arColor = [
311 [hexdec(mb_substr($color_1, 0, 2)), hexdec(mb_substr($color_2, 0, 2))],
312 [hexdec(mb_substr($color_1, 2, 2)), hexdec(mb_substr($color_2, 2, 2))],
313 [hexdec(mb_substr($color_1, 4, 2)), hexdec(mb_substr($color_2, 4, 2))],
314 ];
315 $this->SetEllipseColor($arColor);
316 }
317 }
318
319 function SetLineColor($arColor)
320 {
321 if ($arNewColor = $this->SetColor($arColor))
322 {
323 $this->arLineColor = $arNewColor;
324 }
325 }
326
327 function SetLineColorRGB($color_1, $color_2)
328 {
329 if (preg_match("/^[0-9A-Fa-f]{6}$/", $color_1) && preg_match("/^[0-9A-Fa-f]{6}$/", $color_1))
330 {
331 $arColor = [
332 [hexdec(mb_substr($color_1, 0, 2)), hexdec(mb_substr($color_2, 0, 2))],
333 [hexdec(mb_substr($color_1, 2, 2)), hexdec(mb_substr($color_2, 2, 2))],
334 [hexdec(mb_substr($color_1, 4, 2)), hexdec(mb_substr($color_2, 4, 2))],
335 ];
336 $this->SetLineColor($arColor);
337 }
338 }
339
340 function SetBorderColor($arColor)
341 {
342 if ($arNewColor = $this->SetColor($arColor))
343 {
344 $this->arBorderColor = $arNewColor;
345 }
346 }
347
348 function SetBorderColorRGB($color)
349 {
350 if (preg_match("/^[0-9A-Fa-f]{6}$/", $color))
351 {
352 $arColor = [
353 hexdec(mb_substr($color, 0, 2)),
354 hexdec(mb_substr($color, 2, 2)),
355 hexdec(mb_substr($color, 4, 2)),
356 ];
357 $this->SetBorderColor($arColor);
358 }
359 }
360
361 function SetEllipsesNumber($num)
362 {
363 $this->numEllipses = intval($num);
364 }
365
366 function SetLinesNumber($num)
367 {
368 $this->numLines = intval($num);
369 }
370
371 function SetLinesOverText($bLinesOverText)
372 {
373 $this->bLinesOverText = (bool)$bLinesOverText;
374 }
375
376 function SetCodeChars($arChars)
377 {
378 if (is_array($arChars) && !empty($arChars))
379 {
380 $this->arChars = $arChars;
381 }
382 }
383
384 function SetWaveTransformation($bWaveTransformation)
385 {
386 $this->bWaveTransformation = (bool)$bWaveTransformation;
387 }
388
389 function SetEmptyText($bEmptyText)
390 {
391 $this->bEmptyText = (bool)$bEmptyText;
392 }
393
394 /* UTIL */
395 function GetColor($arColor)
396 {
397 $arResult = [];
398 for ($i = 0, $n = count($arColor); $i < $n; $i++)
399 {
400 $arResult[$i] = round(rand($arColor[$i][0], $arColor[$i][1]));
401 }
402 return $arResult;
403 }
404
405 function InitImage($width = false, $height = false)
406 {
407 if (!$width)
408 {
409 $width = $this->imageWidth;
410 }
411 if (!$height)
412 {
413 $height = $this->imageHeight;
414 }
415 $image = imagecreatetruecolor($width, $height);
416 if (!$this->arRealBGColor)
417 {
418 $this->arRealBGColor = $this->GetColor($this->arBGColor);
419 }
420 $bgColor = imagecolorallocate($image, $this->arRealBGColor[0], $this->arRealBGColor[1], $this->arRealBGColor[2]);
421 imagefilledrectangle($image, 0, 0, imagesx($image), imagesy($image), $bgColor);
422 return $image;
423 }
424
425 function CreateImage()
426 {
427 $this->image = $this->InitImage();
428
429 $this->DrawEllipses();
430
431 if (!$this->bLinesOverText)
432 {
433 $this->DrawLines();
434 }
435
436 $this->DrawText();
437
438 if ($this->bLinesOverText)
439 {
440 $this->DrawLines();
441 }
442
443 if ($this->bWaveTransformation)
444 {
445 $this->Wave();
446 }
447
448 $arBorderColor = $this->GetColor($this->arBorderColor);
449 $borderColor = imagecolorallocate($this->image, $arBorderColor[0], $arBorderColor[1], $arBorderColor[2]);
450 imageline($this->image, 0, 0, $this->imageWidth - 1, 0, $borderColor);
451 imageline($this->image, 0, 0, 0, $this->imageHeight - 1, $borderColor);
452 imageline($this->image, $this->imageWidth - 1, 0, $this->imageWidth - 1, $this->imageHeight - 1, $borderColor);
453 imageline($this->image, 0, $this->imageHeight - 1, $this->imageWidth - 1, $this->imageHeight - 1, $borderColor);
454 }
455
456 function CreateImageError($arMsg)
457 {
458 $this->image = imagecreate($this->imageWidth, $this->imageHeight);
459 $textColor = imagecolorallocate($this->image, 255, 255, 255);
460
461 if (!is_array($arMsg))
462 {
463 $arMsg = [$arMsg];
464 }
465
466 $bTextOut = false;
467 $y = 5;
468 for ($i = 0, $n = count($arMsg); $i < $n; $i++)
469 {
470 if (trim($arMsg[$i]) <> '')
471 {
472 $bTextOut = true;
473 imagestring($this->image, 3, 5, $y, $arMsg[$i], $textColor);
474 $y += 15;
475 }
476 }
477
478 if (!$bTextOut)
479 {
480 imagestring($this->image, 3, 5, 5, "Error!", $textColor);
481 imagestring($this->image, 3, 5, 20, "Reload the page!", $textColor);
482 }
483 }
484
485 function Wave()
486 {
487 $img = $this->image;
488 $img2 = $this->InitImage();
489
490 // случайные параметры (можно поэкспериментировать с коэффициентами):
491 // частоты
492 $rand1 = mt_rand(700000, 1000000) / 15000000;
493 $rand2 = mt_rand(700000, 1000000) / 15000000;
494 $rand3 = mt_rand(700000, 1000000) / 15000000;
495 $rand4 = mt_rand(700000, 1000000) / 15000000;
496 // фазы
497 $rand5 = mt_rand(0, 3141592) / 1000000;
498 $rand6 = mt_rand(0, 3141592) / 1000000;
499 $rand7 = mt_rand(0, 3141592) / 1000000;
500 $rand8 = mt_rand(0, 3141592) / 1000000;
501 // амплитуды
502 $rand9 = mt_rand(400, 600) / 500;
503 $rand10 = mt_rand(400, 600) / 200;
504
505 $height = $this->imageHeight;
506 $height_1 = $height - 1;
507 $width = $this->imageWidth;
508 $width_1 = $width - 1;
509
510 for ($x = 0; $x < $width; $x++)
511 {
512 for ($y = 0; $y < $height; $y++)
513 {
514 // координаты пикселя-первообраза.
515 $sx = $x + (sin($x * $rand1 + $rand5) + sin($y * $rand3 + $rand6)) * $rand9;
516 $sy = $y + (sin($x * $rand2 + $rand7) + sin($y * $rand4 + $rand8)) * $rand10;
517
518 if ($sx < 0 || $sy < 0 || $sx >= $width_1 || $sy >= $height_1)
519 {
520 // первообраз за пределами изображения
521 }
522 else
523 {
524 // цвета основного пикселя и его 3-х соседей для лучшего антиалиасинга
525 $rgb = imagecolorat($img, $sx, $sy);
526 $color_r = ($rgb >> 16) & 0xFF;
527 $color_g = ($rgb >> 8) & 0xFF;
528 $color_b = $rgb & 0xFF;
529
530 $rgb = imagecolorat($img, $sx + 1, $sy);
531 $color_x_r = ($rgb >> 16) & 0xFF;
532 $color_x_g = ($rgb >> 8) & 0xFF;
533 $color_x_b = $rgb & 0xFF;
534
535 $rgb = imagecolorat($img, $sx, $sy + 1);
536 $color_y_r = ($rgb >> 16) & 0xFF;
537 $color_y_g = ($rgb >> 8) & 0xFF;
538 $color_y_b = $rgb & 0xFF;
539
540 $rgb = imagecolorat($img, $sx + 1, $sy + 1);
541 $color_xy_r = ($rgb >> 16) & 0xFF;
542 $color_xy_g = ($rgb >> 8) & 0xFF;
543 $color_xy_b = $rgb & 0xFF;
544 }
545 // сглаживаем
546 $frsx = $sx - floor($sx); //отклонение координат первообраза от целого
547 $frsy = $sy - floor($sy);
548 $frsx1 = 1 - $frsx;
549 $frsy1 = 1 - $frsy;
550 // вычисление цвета нового пикселя как пропорции от цвета основного пикселя и его соседей
551 $i11 = $frsx1 * $frsy1;
552 $i01 = $frsx * $frsy1;
553 $i10 = $frsx1 * $frsy;
554 $i00 = $frsx * $frsy;
555 $red = floor($color_r * $i11 +
556 $color_x_r * $i01 +
557 $color_y_r * $i10 +
558 $color_xy_r * $i00
559 );
560 $green = floor($color_g * $i11 +
561 $color_x_g * $i01 +
562 $color_y_g * $i10 +
563 $color_xy_g * $i00
564 );
565 $blue = floor($color_b * $i11 +
566 $color_x_b * $i01 +
567 $color_y_b * $i10 +
568 $color_xy_b * $i00
569 );
570 imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $red, $green, $blue));
571 }
572 }
573 $this->image = $img2;
574 }
575
576 function EmptyText()
577 {
578 $sx = imagesx($this->image) - 1;
579 $sy = imagesy($this->image) - 1;
580
581 $backup = imagecreatetruecolor($sx, $sy);
582 imagealphablending($backup, false);
583 imagecopy($backup, $this->image, 0, 0, 0, 0, $sx, $sy);
584
585 $white = imagecolorallocate($this->image, 255, 255, 255);
586 $bgColor = imagecolorallocate($this->image, $this->arRealBGColor[0], $this->arRealBGColor[1], $this->arRealBGColor[2]);
587
588 for ($x = 1; $x < $sx; $x++)
589 {
590 for ($y = 1; $y < $sy; $y++)
591 {
592 $c1 = imagecolorat($backup, $x - 1, $y);
593 if ($c1 != $white && $c1 != $bgColor)
594 {
595 $c2 = imagecolorat($backup, $x + 1, $y);
596 if ($c1 == $c2)
597 {
598 $c3 = imagecolorat($backup, $x, $y - 1);
599 if ($c2 == $c3)
600 {
601 $c4 = imagecolorat($backup, $x, $y + 1);
602 if ($c3 == $c4)
603 {
604 imagesetpixel($this->image, $x, $y, $bgColor);
605 }
606 }
607 }
608 }
609 }
610 }
611
612 if (function_exists('imageconvolution'))
613 {
614 $gaussian = [[1.0, 1.0, 1.0], [1.0, 7.0, 1.0], [1.0, 1.0, 1.0]];
615 imageconvolution($this->image, $gaussian, 15, 0);
616
617 $mask = [
618 [-0.1, -0.1, -0.1],
619 [-0.1, 1.8, -0.1],
620 [-0.1, -0.1, -0.1],
621 ];
622 imageconvolution($this->image, $mask, 1, 0);
623 }
624 }
625
626 function DestroyImage()
627 {
628 imagedestroy($this->image);
629 }
630
631 function ShowImage()
632 {
633 imagejpeg($this->image);
634 }
635
636 function DrawText()
637 {
638 $alpha = ($this->bTransparentText ? floor($this->transparentTextPercent / 100 * 127) : 0);
639
640 $bPrecise = $this->textDistanceFrom < 0 && $this->textDistanceTo < 0;
641
642 if ($bPrecise)
643 {
644 //We'll need inversed color to draw on background
645 $bg_color_hex = $this->arRealBGColor[0] << 16 | $this->arRealBGColor[1] << 8 | $this->arRealBGColor[2];
646 $not_bg_color = [
647 (!$bg_color_hex >> 16) & 0xFF,
648 (!$bg_color_hex >> 8) & 0xFF,
649 (!$bg_color_hex) & 0xFF,
650 ];
651 }
652
653 $arPos = [];
654 $x = 0;
655
656 for ($i = 0; $i < $this->codeLength; $i++)
657 {
658 $char = mb_substr($this->code, $i, 1);
659 $utf = $char;
660
661 $ttfFile = $_SERVER["DOCUMENT_ROOT"] . $this->ttfFilesPath . "/" . $this->arTTFFiles[rand(1, count($this->arTTFFiles)) - 1];
662 $angle = rand($this->textAngleFrom, $this->textAngleTo);
663
664 $bounds = imagettfbbox($this->textFontSize, $angle, $ttfFile, $utf);
665
666 $height = max($bounds[1], $bounds[3], $bounds[5], $bounds[7]) - min($bounds[1], $bounds[3], $bounds[5], $bounds[7]);
667 $width = max($bounds[0], $bounds[2], $bounds[4], $bounds[6]) - min($bounds[0], $bounds[2], $bounds[4], $bounds[6]);
668
669 $y = $height + rand(0, ($this->imageHeight - $height) * 0.9);
670
671 $arLeftBounds = [];
672 $arRightBounds = [];
673 $dx = 0;
674
675 if ($bPrecise)
676 {
677 //Now for precise positioning we need to draw characred and define its borders
678 $img = $this->InitImage($width, $this->imageHeight);
679 $tmp = imagecolorallocate($img, $not_bg_color[0], $not_bg_color[1], $not_bg_color[2]);
680 $dx = -min($bounds[0], $bounds[2], $bounds[4], $bounds[6]);
681 imagettftext($img, $this->textFontSize, $angle, $dx, $y, $tmp, $ttfFile, $utf);
682
683 for ($yy = 0; $yy < $this->imageHeight; $yy++)
684 {
685 $arLeftBounds[$yy] = 0;
686 for ($xx = 0; $xx < $width; $xx++)
687 {
688 $rgb = imagecolorat($img, $xx, $yy);
689 if ($rgb !== $bg_color_hex)
690 {
691 $arLeftBounds[$yy] = $xx;
692 break;
693 }
694 }
695
696 $arRightBounds[$yy] = 0;
697 if ($arLeftBounds[$yy] > 0)
698 {
699 for ($xx = $width; $xx > 0; $xx--)
700 {
701 $rgb = imagecolorat($img, $xx - 1, $yy);
702 if ($rgb !== $bg_color_hex)
703 {
704 $arRightBounds[$yy] = $xx - 1;
705 break;
706 }
707 }
708 }
709 }
710
711 imagedestroy($img);
712 }
713
714 if ($i > 0)
715 {
716 if ($bPrecise)
717 {
718 $arDX = [];
719 for ($yy = 0; $yy < $this->imageHeight; $yy++)
720 {
721 if ($arPos[$i - 1][6][$yy] > 0 && $arLeftBounds[$yy] > 0)
722 {
723 $arDX[$yy] = ($arPos[$i - 1][6][$yy] - $arPos[$i - 1][7]) - ($arLeftBounds[$yy] - $dx);
724 }
725 else
726 {
727 $arDX[$yy] = $arPos[$i - 1][5][$yy] - $arPos[$i - 1][7];
728 }
729 }
730 $x += max($arDX) + (rand($this->textDistanceFrom, $this->textDistanceTo));
731 }
732 else
733 {
734 $x += rand($this->textDistanceFrom, $this->textDistanceTo);
735 }
736 }
737 else
738 {
739 $x = rand($this->textStartX / 2, $this->textStartX * 2);
740 }
741
742 $arPos[$i] = [
743 $angle, //0
744 $x, //1
745 $y, //2
746 $ttfFile, //3
747 $char, //4
748 $arLeftBounds, //5
749 $arRightBounds, //6
750 $dx, //7
751 $utf, //8
752 ];
753 }
754
755 $x2 = 0;
756 foreach ($arPos as $pos)
757 {
758 $arTextColor = $this->GetColor($this->arTextColor);
759
760 if ($this->bTransparentText)
761 {
762 $color = imagecolorallocatealpha($this->image, $arTextColor[0], $arTextColor[1], $arTextColor[2], $alpha);
763 }
764 else
765 {
766 $color = imagecolorallocate($this->image, $arTextColor[0], $arTextColor[1], $arTextColor[2]);
767 }
768
769 $bounds = imagettftext($this->image, $this->textFontSize, $pos[0], $pos[1], $pos[2], $color, $pos[3], $pos[8]);
770
771 $x2 = $pos[1] + ($bounds[2] - $bounds[0]);
772 }
773
774 if ($this->bEmptyText)
775 {
776 $this->EmptyText();
777 }
778
779 return $x2;
780 }
781
782 function DrawEllipses()
783 {
784 if ($this->numEllipses > 0)
785 {
786 for ($i = 0; $i < $this->numEllipses; $i++)
787 {
788 $arEllipseColor = $this->GetColor($this->arEllipseColor);
789 $color = imagecolorallocate($this->image, $arEllipseColor[0], $arEllipseColor[1], $arEllipseColor[2]);
790 imagefilledellipse($this->image, round(rand(0, $this->imageWidth)), round(rand(0, $this->imageHeight)), round(rand(0, $this->imageWidth / 8)), round(rand(0, $this->imageHeight / 2)), $color);
791 }
792 }
793 }
794
795 function DrawLines()
796 {
797 if ($this->numLines > 0)
798 {
799 for ($i = 0; $i < $this->numLines; $i++)
800 {
801 $arLineColor = $this->GetColor($this->arLineColor);
802 $color = imagecolorallocate($this->image, $arLineColor[0], $arLineColor[1], $arLineColor[2]);
803 imageline($this->image, rand(1, $this->imageWidth), rand(1, $this->imageHeight / 2), rand(1, $this->imageWidth), rand($this->imageHeight / 2, $this->imageHeight), $color);
804 }
805 }
806 }
807
808 /* OUTPUT */
809 function Output()
810 {
811 header("Expires: Sun, 1 Jan 2000 12:00:00 GMT");
812 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
813 header("Cache-Control: no-store, no-cache, must-revalidate");
814 header("Cache-Control: post-check=0, pre-check=0", false);
815 header("Pragma: no-cache");
816 header("Content-Type: image/jpeg");
817 $this->CreateImage();
818 $this->ShowImage();
819 $this->DestroyImage();
820 }
821
822 function OutputError()
823 {
824 header("Expires: Sun, 1 Jan 2000 12:00:00 GMT");
825 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
826 header("Cache-Control: no-store, no-cache, must-revalidate");
827 header("Cache-Control: post-check=0, pre-check=0", false);
828 header("Pragma: no-cache");
829 header("Content-Type: image/jpeg");
830
831 $numArgs = func_num_args();
832 if ($numArgs > 0)
833 {
834 $arMsg = func_get_arg(0);
835 }
836 else
837 {
838 $arMsg = [];
839 }
840
841 $this->CreateImageError($arMsg);
842 $this->ShowImage();
843 $this->DestroyImage();
844 }
845
846 /* CODE */
847 function SetCode()
848 {
849 if (!defined("CAPTCHA_COMPATIBILITY"))
850 {
851 CCaptcha::SetCaptchaCode();
852 return;
853 }
854
855 $max = count($this->arChars);
856
857 $this->code = "";
858 for ($i = 0; $i < $this->codeLength; $i++)
859 {
860 $this->code .= $this->arChars[rand(1, $max) - 1];
861 }
862
863 $this->sid = time();
864
865 $session = \Bitrix\Main\Application::getInstance()->getSession();
866 $session["CAPTCHA_CODE"][$this->sid] = $this->code;
867 }
868
869 function SetCodeCrypt()
870 {
871 if (!defined("CAPTCHA_COMPATIBILITY"))
872 {
873 CCaptcha::SetCaptchaCode();
874 return;
875 }
876
877 $max = count($this->arChars);
878
879 $this->code = "";
880 for ($i = 0; $i < $this->codeLength; $i++)
881 {
882 $this->code .= $this->arChars[rand(1, $max) - 1];
883 }
884
885 $session = \Bitrix\Main\Application::getInstance()->getSession();
886 if (!$session->get("CAPTCHA_PASSWORD"))
887 {
888 $session["CAPTCHA_PASSWORD"] = randString(10);
889 }
890
891 $this->codeCrypt = $this->CryptData($this->code, "E", $session["CAPTCHA_PASSWORD"]);
892 }
893
894 function SetCaptchaCode($sid = false)
895 {
896 $max = count($this->arChars);
897
898 $this->code = "";
899 for ($i = 0; $i < $this->codeLength; $i++)
900 {
901 $this->code .= $this->arChars[rand(1, $max) - 1];
902 }
903
904 $this->sid = $sid === false ? $this->Generate32RandomString() : $sid;
905
906 CCaptcha::Add(
907 [
908 "CODE" => $this->code,
909 "ID" => $this->sid,
910 ]
911 );
912 }
913
914 function Generate32RandomString()
915 {
916 $prefix = (defined("BX_CLUSTER_GROUP") ? BX_CLUSTER_GROUP : "0");
917 return mb_substr($prefix . md5(uniqid()), 0, 32);
918 }
919
920 function InitCaptchaCode($sid)
921 {
922 global $DB;
923
924 $res = $DB->Query("SELECT CODE FROM b_captcha WHERE ID = '" . $DB->ForSQL($sid, 32) . "' ");
925 if (!$ar = $res->Fetch())
926 {
927 return false;
928 }
929
930 $this->code = $ar["CODE"];
931 $this->sid = $sid;
932 $this->codeLength = mb_strlen($this->code);
933
934 return true;
935 }
936
937 function InitCode($sid)
938 {
939 if (!defined("CAPTCHA_COMPATIBILITY"))
940 {
941 return CCaptcha::InitCaptchaCode($sid);
942 }
943
944 $session = \Bitrix\Main\Application::getInstance()->getSession();
945 if (!is_array($session["CAPTCHA_CODE"]) || empty($session["CAPTCHA_CODE"]))
946 {
947 return false;
948 }
949
950 if (!array_key_exists($sid, $session["CAPTCHA_CODE"]))
951 {
952 return false;
953 }
954
955 $this->code = $session["CAPTCHA_CODE"][$sid];
956 $this->sid = $sid;
957 $this->codeLength = mb_strlen($this->code);
958
959 return true;
960 }
961
962 function InitCodeCrypt($codeCrypt)
963 {
964 if (!defined("CAPTCHA_COMPATIBILITY"))
965 {
966 return CCaptcha::InitCaptchaCode($codeCrypt);
967 }
968
969 if ($codeCrypt == '')
970 {
971 return false;
972 }
973
974 $session = \Bitrix\Main\Application::getInstance()->getSession();
975 if (!$session->get("CAPTCHA_PASSWORD"))
976 {
977 return false;
978 }
979
980 $this->codeCrypt = $codeCrypt;
981 $this->code = $this->CryptData($codeCrypt, "D", $session["CAPTCHA_PASSWORD"]);
982 $this->codeLength = mb_strlen($this->code);
983
984 return true;
985 }
986
987 function GetSID()
988 {
989 return $this->sid;
990 }
991
992 function GetCodeCrypt()
993 {
994 if (!defined("CAPTCHA_COMPATIBILITY"))
995 {
996 return $this->sid;
997 }
998
999 return $this->codeCrypt;
1000 }
1001
1002 function CheckCaptchaCode($userCode, $sid, $bUpperCode = true)
1003 {
1004 global $DB;
1005
1006 if (!is_string($userCode) || $userCode == '' || !is_string($sid) || $sid == '')
1007 {
1008 return false;
1009 }
1010
1011 if ($bUpperCode)
1012 {
1013 $userCode = mb_strtoupper($userCode);
1014 }
1015
1016 $res = $DB->Query("SELECT CODE FROM b_captcha WHERE ID = '" . $DB->ForSQL($sid, 32) . "' ");
1017 if (!$ar = $res->Fetch())
1018 {
1019 return false;
1020 }
1021
1022 if ($ar["CODE"] != $userCode)
1023 {
1024 return false;
1025 }
1026
1027 CCaptcha::Delete($sid);
1028
1029 return true;
1030 }
1031
1032 function CheckCode($userCode, $sid, $bUpperCode = true)
1033 {
1034 if (!defined("CAPTCHA_COMPATIBILITY"))
1035 {
1036 return CCaptcha::CheckCaptchaCode($userCode, $sid, $bUpperCode);
1037 }
1038
1039 if (!is_string($userCode) || $userCode == '' || !is_string($sid) || $sid == '')
1040 {
1041 return false;
1042 }
1043
1044 $session = \Bitrix\Main\Application::getInstance()->getSession();
1045 if (empty($session["CAPTCHA_CODE"]) || !is_array($session["CAPTCHA_CODE"]))
1046 {
1047 return false;
1048 }
1049
1050 if (!isset($session["CAPTCHA_CODE"][$sid]))
1051 {
1052 return false;
1053 }
1054
1055 if ($bUpperCode)
1056 {
1057 $userCode = mb_strtoupper($userCode);
1058 }
1059
1060 if ($session["CAPTCHA_CODE"][$sid] != $userCode)
1061 {
1062 return false;
1063 }
1064
1065 unset($session["CAPTCHA_CODE"][$sid]);
1066
1067 return true;
1068 }
1069
1070 function CheckCodeCrypt($userCode, $codeCrypt, $password = "", $bUpperCode = true)
1071 {
1072 if (!defined("CAPTCHA_COMPATIBILITY"))
1073 {
1074 return CCaptcha::CheckCaptchaCode($userCode, $codeCrypt, $bUpperCode);
1075 }
1076
1077 if ($codeCrypt == '')
1078 {
1079 return false;
1080 }
1081
1082 $session = \Bitrix\Main\Application::getInstance()->getSession();
1083 if (!$session->get("CAPTCHA_PASSWORD"))
1084 {
1085 return false;
1086 }
1087
1088 if ($bUpperCode)
1089 {
1090 $userCode = mb_strtoupper($userCode);
1091 }
1092
1093 $code = $this->CryptData($codeCrypt, "D", $session["CAPTCHA_PASSWORD"]);
1094
1095 if ($code != $userCode)
1096 {
1097 return false;
1098 }
1099
1100 return true;
1101 }
1102
1103 function CryptData($data, $type, $pwdString)
1104 {
1105 $type = strtoupper($type);
1106 if ($type != "D")
1107 {
1108 $type = "E";
1109 }
1110
1111 if ($type == 'D')
1112 {
1113 $data = base64_decode(urldecode($data));
1114 }
1115
1116 $key[] = "";
1117 $box[] = "";
1118 $pwdLength = mb_strlen($pwdString);
1119
1120 for ($i = 0; $i <= 255; $i++)
1121 {
1122 $key[$i] = ord(mb_substr($pwdString, ($i % $pwdLength), 1));
1123 $box[$i] = $i;
1124 }
1125 $x = 0;
1126
1127 for ($i = 0; $i <= 255; $i++)
1128 {
1129 $x = ($x + $box[$i] + $key[$i]) % 256;
1130 $temp_swap = $box[$i];
1131 $box[$i] = $box[$x];
1132 $box[$x] = $temp_swap;
1133 }
1134 $cipher = "";
1135 $a = 0;
1136 $j = 0;
1137 for ($i = 0, $n = mb_strlen($data); $i < $n; $i++)
1138 {
1139 $a = ($a + 1) % 256;
1140 $j = ($j + $box[$a]) % 256;
1141 $temp = $box[$a];
1142 $box[$a] = $box[$j];
1143 $box[$j] = $temp;
1144 $k = $box[(($box[$a] + $box[$j]) % 256)];
1145 $cipherby = ord(mb_substr($data, $i, 1)) ^ $k;
1146 $cipher .= chr($cipherby);
1147 }
1148
1149 if ($type == 'D')
1150 {
1151 $res_data = urldecode(urlencode($cipher));
1152 }
1153 else
1154 {
1155 $res_data = urlencode(base64_encode($cipher));
1156 }
1157
1158 return $res_data;
1159 }
1160
1161 public function Add($arFields)
1162 {
1163 global $DB;
1164
1165 if (!is_set($arFields, "CODE") || $arFields["CODE"] == '')
1166 {
1167 return false;
1168 }
1169
1170 if (!is_set($arFields, "ID") || $arFields["ID"] == '')
1171 {
1172 $arFields["ID"] = $this->Generate32RandomString();
1173 }
1174
1175 if (!is_set($arFields, "IP") || $arFields["IP"] == '')
1176 {
1177 $arFields["IP"] = $_SERVER["REMOTE_ADDR"];
1178 }
1179
1180 if (!is_set($arFields, "DATE_CREATE") || $arFields["DATE_CREATE"] == '' || !$DB->IsDate($arFields["DATE_CREATE"], false, LANG, "FULL"))
1181 {
1182 unset($arFields["DATE_CREATE"]);
1183 $arFields["~DATE_CREATE"] = CDatabase::CurrentTimeFunction();
1184 }
1185
1186 $pool = \Bitrix\Main\Application::getInstance()->getConnectionPool();
1187 $pool->useMasterOnly(true);
1188
1189 $arInsert = $DB->PrepareInsert("b_captcha", $arFields);
1190
1191 $result = $DB->Query("INSERT INTO b_captcha (" . $arInsert[0] . ") VALUES (" . $arInsert[1] . ")", true);
1192
1193 $pool->useMasterOnly(false);
1194
1195 if ($result)
1196 {
1197 return $arFields["ID"];
1198 }
1199 return false;
1200 }
1201
1202 function Delete($sid)
1203 {
1204 global $DB;
1205
1206 if (!$DB->Query("DELETE FROM b_captcha WHERE ID='" . $DB->ForSQL($sid, 32) . "' "))
1207 {
1208 return false;
1209 }
1210
1211 return true;
1212 }
1213}
1214
$type
Определения options.php:106
$arResult
Определения generate_coupon.php:16
static GetOptionString($module_id, $name, $def="", $site=false, $bExactSite=false)
Определения option.php:8
static GetOptionInt($module_id, $name, $def="", $site=false)
Определения option.php:49
static CurrentTimeFunction()
Определения database.php:30
if(!is_array($prop["VALUES"])) $tmp
Определения component_props.php:203
$arFields
Определения dblapprove.php:5
$data['IS_AVAILABLE']
Определения .description.php:13
$filename
Определения file_edit.php:47
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
global $DB
Определения cron_frame.php:29
endif
Определения csv_new_setup.php:990
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
ShowImage($PICTURE_ID, $iMaxW=0, $iMaxH=0, $sParams=false, $strImageUrl="", $bPopup=false, $strPopupTitle=false, $iSizeWHTTP=0, $iSizeHHTTP=0)
Определения tools.php:5145
is_set($a, $k=false)
Определения tools.php:2133
randString($pass_len=10, $pass_chars=false)
Определения tools.php:2154
__construct(?int $storeId, int $productId, string $barcode, int $userId)
Определения basestorebarcodeaction.php:38
$ar
Определения options.php:199
if(empty($signedUserToken)) $key
Определения quickway.php:257
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$width
Определения html.php:68
$fontSize
Определения pdf.php:30
else $a
Определения template.php:137
$password
Определения result.php:7
$x2
Определения pdf.php:124
$k
Определения template_pdf.php:567
$max
Определения template_copy.php:262
$n
Определения update_log.php:107