Sourcecode

Captcha/de/themarcraft/utils/Captcha.php


Captcha.php

<?php

namespace de\themarcraft\utils;

use de\themarcraft\utils\Utils;

class Captcha
{
    private string $captcha;

    public function __construct()
    {
        $this->captcha = Utils::getRandomString(5);
    }

    public function getCapthaText(): string
    {
        return $this->captcha;
    }

    public function getEncryptedCaptcha(): string
    {
        return base64_encode(password_hash(base64_encode($this->captcha), PASSWORD_DEFAULT));
    }

    public function getCaptchaImage(): string
    {
        $scale = 8;
        $im = imagecreate(250 * $scale, 60 * $scale);
        $grau[0] = imagecolorallocate($im, 80, 80, 80);
        imagefill($im, 0, 0, $grau[0]);
        $schwarz = imagecolorallocate($im, 0, 0, 0);
        $grau[1] = imagecolorallocate($im, 5, 5, 5);
        $grau[4] = imagecolorallocate($im, 10, 10, 10);
        $grau[5] = imagecolorallocate($im, 20, 20, 20);
        $grau[6] = imagecolorallocate($im, 120, 120, 120);
        $col[0] = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
        $col[] = imagecolorallocate($im, 192, 192, 192);
        $col[] = imagecolorallocate($im, 192, 0, 0);
        $col[] = imagecolorallocate($im, 0, 192, 0);
        $col[] = imagecolorallocate($im, 5, 5, 5);
        $col[] = imagecolorallocate($im, 20, 20, 20);

        for ($i = 1; $i <= 1000 * $scale; $i++) {
            $grau[2] = imagecolorallocate($im, rand(1, 100), rand(1, 100), rand(1, 100));
            $tmp = rand(1, 250);
            $grau[3] = imagecolorallocate($im, $tmp, $tmp, $tmp);
            imageline($im, rand(0, 400 * $scale), rand(-150, 100 * $scale), rand(-150, 400 * $scale), rand(-150, 100 * $scale), $grau[rand(0, 6)]);
        }

        $feld = mb_str_split($this->captcha);


        for ($i = 1; $i <= 5; $i++) {
            $schriftart = $_SERVER["DOCUMENT_ROOT"] . "/arial.ttf";


            imagettftext($im, 30 * $scale, -35 * $i, 45 * $i * $scale - 15, 25 * $scale - $i * 6, $schwarz, $schriftart, $feld[$i - 1]);
        }

        for ($i = 0; $i < 1000 * $scale; $i++) {
            imagefilledellipse($im, rand(0, 250 * $scale), rand(0, 60 * $scale), 1 * $scale, 1 * $scale, $col[rand(0, count($col) - 1)]);
        }

        imagettftext($im, 5 * $scale, 0, 0, 63 * $scale - 5 * $scale, $col[rand(0, count($col) - 1)], $schriftart, "©2024 tmcz.de");

        imagepng($im, $_SERVER["DOCUMENT_ROOT"] . "/tmp_captcha.png");
        imagedestroy($im);
        $imageData = base64_encode(file_get_contents($_SERVER["DOCUMENT_ROOT"] . "/tmp_captcha.png"));
        $mimeType = mime_content_type($_SERVER["DOCUMENT_ROOT"] . "/tmp_captcha.png");
        return 'data:' . $mimeType . ';base64,' . $imageData;
    }

    public static function verifyCaptcha($solution, $input)
    {
        if (password_verify(base64_encode($input), base64_decode($solution))) {
            return true;
        } else {
            return false;
        }
    }
}