Skip to content

Commit

Permalink
add captcha
Browse files Browse the repository at this point in the history
  • Loading branch information
skywalker512 committed Jun 28, 2018
1 parent 901ae81 commit a96ce2a
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 0 deletions.
84 changes: 84 additions & 0 deletions addons/plugins/MSCaptcha/MSCaptchaController.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* MSCaptcha by DaVchezt
**/
if (!defined("IN_ESOTALK")) exit;

Class MSCaptchaController extends ETController {
protected $width = 110;
protected $height = 45;
protected $font = '/font/League_Gothic.ttf';
protected $font_size = 20;
protected $font_colors = array(0, 50, 100, 150, 200);

public function action_index()
{
// ttf font
$this->font = dirname(__FILE__) . $this->font;
// Create the image
$src = imagecreatetruecolor($this->width, $this->height);
// Create white color
$white = imagecolorallocate($src, 255, 255, 255);
imagefill($src, 0, 0, $white);
//Prevent the spider use the same number as captcha to register automaticly.
$cc = ET::$session->get('inputmscaptha');
$errnum = ET::$session->get('capthaerrnum');
if(!$errnum) $errnum = 0;
while (true){
// Get the code
$x = array_rand(array("+","*"),1);
if($x == 0){
// Genrate random number
$a = rand(10, 50);
$b = rand(10, 50);
$c = $a + $b;
}else{
$a = rand(5, 10);
$b = rand(5, 10);
$c = $a * $b;
}
//增大恶意刷注册撞中的难度
if($errnum > 5){
$x = 1;
$a = rand(5, 20);
$b = rand(5, 20);
$c = $a * $b;
}
if($errnum > 15){
$x = 1;
$a = rand(50, 99);
$b = rand(50, 99);
$c = $a * $b;
}
if ($c != $cc) break;
}
// Genrate session of code
ET::$session->store('mscaptcha', $c);

$arr = array($a, $x==0?'+':' x ', $b, '=', '?');
// Create Image from code
for($i = 0; $i < count($arr); $i++) {
$color = imagecolorallocatealpha(
$src,
$this->font_colors[rand(0, count($this->font_colors) - 1)],
$this->font_colors[rand(0, count($this->font_colors) - 1)],
$this->font_colors[rand(0, count($this->font_colors) - 1)],
rand(0, 50)
);
imagettftext(
$src,
$this->font_size,
0,
($this->font_size * ($i + 1)) - 10,
(($this->height * 2) / 3) + 3,
$color,
$this->font,
$arr[$i]
);
}
header("Content-type: image/png");
imagepng($src);
imagedestroy($src);
}

}
7 changes: 7 additions & 0 deletions addons/plugins/MSCaptcha/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MSCaptcha
=========

Just simple captcha for esoTalk


![Preview 1](http://esotalk.org/forum/attachment/54092490367b7_captcha.png)
Binary file not shown.
Binary file added addons/plugins/MSCaptcha/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions addons/plugins/MSCaptcha/plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
// This file is part of esoTalk. Please see the included license file for usage information.

if (!defined("IN_ESOTALK")) exit;

ET::$pluginInfo["MSCaptcha"] = array(
"name" => "MSCaptcha",
"description" => "Just simple captcha.",
"version" => ESOTALK_VERSION,
"author" => "DaVchezt",
"authorEmail" => "[email protected]",
"authorURL" => "http://davchezt.tumblr.com",
"license" => "GPLv2"
);

class ETPlugin_MSCaptcha extends ETPlugin {

public function __construct($rootDirectory)
{
parent::__construct($rootDirectory);

ETFactory::registerController("mscaptcha", "MSCaptchaController", dirname(__FILE__)."/MSCaptchaController.class.php");
}

public function handler_init($sender)
{
$sender->addCSSFile($this->Resource("mscaptcha.css"));
$sender->addJSFile($this->Resource("mscaptcha.js"));
}

public function handler_userController_initJoin($controller, $form)
{
$form->addSection("mscaptcha", T("Solve this"));

$form->addField("mscaptcha", "mscaptcha", function($form)
{
return "<div class=\"mscaptcha\"><img class=\"img-mscaptcha\" src=\"".URL("mscaptcha")."\" alt=\"MSCaptcha\"><i class=\"icon-spinner mscaptcha-loader\" style=\"display:none;\"> loading...</i><br /><a id=\"mscaptcha-refresh\" href=\"#\" class=\"button\"><i class=\"icon-refresh\"></i></a>".$form->input("mscaptcha")."</div>";
},
function($form, $key, &$data)
{
if (ET::$session->get('mscaptcha') != $form->getValue($key)) {
ET::$session->store('inputmscaptha', $form->getValue($key));
if(!($err = ET::$session->get('capthaerrnum'))) $err = 0;
$err++;
ET::$session->store('capthaerrnum', $err);
$form->error($key, T("Invalid!, You need calculator? :D"));
}
});
}

}
29 changes: 29 additions & 0 deletions addons/plugins/MSCaptcha/resources/mscaptcha.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.mscaptcha img {
border: 1px solid #ddd;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
border-radius: 4px;
}
.mscaptcha a.button {
height: 13px;
border: 1px solid #ddd;
-webkit-border-radius: 4px 0 0 4px;
-moz-border-radius: 4px 0 0 4px;
border-radius: 4px 0 0 4px;
}
.mscaptcha input {
width: 63px !important;
height: 19px;
border: 1px solid #ddd;
-webkit-border-radius: 0 4px 4px 0;
-moz-border-radius: 0 4px 4px 0;
border-radius: 0 4px 4px 0;
margin-left: -1px !important;
margin-top: 1px !important;
}
.mscaptcha-loader {
position: relative;
top: -20px;
right: -10px;
}
9 changes: 9 additions & 0 deletions addons/plugins/MSCaptcha/resources/mscaptcha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$('document').ready(function() {
$('#mscaptcha-refresh').click(function (e) {
e.preventDefault();
$('.mscaptcha-loader').show();
$('.img-mscaptcha').load(function () {
$('.mscaptcha-loader').hide();
}).attr('src', ET.webPath + '/mscaptcha?' + (Math.random() * 5));
});
});

0 comments on commit a96ce2a

Please sign in to comment.