From 4f240dcfb02a82b333b6acde09da6340e5e4750d Mon Sep 17 00:00:00 2001 From: FusionPBX Date: Wed, 24 Apr 2019 19:43:09 -0600 Subject: [PATCH] Create captcha.php --- resources/classes/captcha.php | 161 ++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 resources/classes/captcha.php diff --git a/resources/classes/captcha.php b/resources/classes/captcha.php new file mode 100644 index 0000000000..28462857c0 --- /dev/null +++ b/resources/classes/captcha.php @@ -0,0 +1,161 @@ + + Portions created by the Initial Developer are Copyright (C) 2019 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +/** + * captcha class + * + * @method string get + */ +class captcha { + + /** + * Called when the object is created + */ + //public $db; + //public $domain_uuid; + public $code; + + /** + * Class constructor + */ + public function __construct() { + + } + + /** + * Called when there are no references to a particular object + * unset the variables used in the class + */ + public function __destruct() { + foreach ($this as $key => $value) { + unset($this->$key); + } + } + + /** + * Create the captcha image + * @var string $code + */ + public function image_captcha() { + + //includes + include "root.php"; + require_once "config.php"; + require_once "resources/functions.php"; + error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ); //hide notices and warnings + + //start the session + ini_set("session.cookie_httponly", True); + if (!isset($_SESSION)) { session_start(); } + + //$_SESSION["captcha"] = substr(md5(uuid()), 0, 6); + //$text = $_SESSION["captcha"]; + $text = $this->code; + + // Set the font path + $font_path = $_SERVER["DOCUMENT_ROOT"]."/resources/captcha/fonts"; + + // Array of fonts + //$fonts[] = 'ROUGD.TTF'; + //$fonts[] = 'Zebra.ttf'; + //$fonts[] = 'hanshand.ttf'; + $fonts = glob($font_path.'/*.[tT][tT][fF]'); + //print_r($fonts); + //exit; + + // Randomize the fonts + srand(uuid()); + $random = (rand()%count($fonts)); + //$font = $font_path.'/'.$fonts[$random]; + $font = $fonts[$random]; + + // Set the font size + $font_size = 16; + if(@$_GET['fontsize']) { + $font_size = $_GET['fontsize']; + } + + // Create the image + $size = $this->image_size($font_size, 0, $font, $text); + $width = $size[2] + $size[0] + 8; + $height = abs($size[1]) + abs($size[7]); + //$width = 100; + //$height = 40; + + // Set the image size + $image = imagecreate($width, $height); + + // Create some colors + $white = imagecolorallocate($image, 255, 255, 255); + $black = imagecolorallocate($image, 0, 0, 0); + + // Set the transparent color + imagecolortransparent($image, $white); + + // Add the text + imagefttext($image, $font_size, 0, 0, abs($size[5]), $black, $font, $text); + + // Set the content-type + //header("Content-type: image/png"); + //imagepng($image)); + + ob_start(); + imagepng($image); + $image_buffer = ob_get_clean(); + //echo "\n"; + imagedestroy($image); + return $image_buffer; + } + + /** + * return the image in base64 + */ + public function image_base64() { + return base64_encode($this->image_captcha()); + } + + /** + * Get the image size + * @var string $value string image size + */ + private function image_size($size, $angle, $font, $text) { + $dummy = imagecreate(1, 1); + $black = imagecolorallocate($dummy, 0, 0, 0); + $bbox = imagettftext($dummy, $size, $angle, 0, 0, $black, $font, $text); + imagedestroy($dummy); + return $bbox; + } + +} + +/* +$captcha = new captcha; +$captcha->code = 'abcdefg'; +$image_base64 = $captcha->base64(); +echo "\n"; +*/ + +?>