-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathCaptchaController.php
More file actions
144 lines (119 loc) · 4.93 KB
/
Copy pathMathCaptchaController.php
File metadata and controls
144 lines (119 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* MathCaptchaController
* @author Yasir Arafat <dev.yasirarafat@gmail.com>
*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MathCaptchaController{
public static $min = 1;
public static $max = 9;
static function generate(){
$num1 = rand(self::$min,self::$max);
$num2 = rand(self::$min,self::$max);
$opr_list = ['+','-','*'];
$opt = $opr_list[array_rand($opr_list,1)];
session()->put('yasir.math_captcha.cap_num1',$num1);
session()->put('yasir.math_captcha.cap_num2',$num2);
session()->put('yasir.math_captcha.cap_opt',$opt);
}
static function reset(){
self::generate();
}
static function get() {
return [
'yasir.math_captcha.cap_num1' => session('yasir.math_captcha.cap_num1'),
'yasir.math_captcha.cap_num2' => session('yasir.math_captcha.cap_num2'),
'yasir.math_captcha.cap_opt' => session('yasir.math_captcha.cap_opt'),
];
}
static function genGet(){
self::generate();
return self::get();
}
/**
* captcha input
*
* @param Request $request
* @param string $name captcha input field
* @param string $target captcha parent element
* @return void
*/
static function input($name="captcha",$target="", $input_classes = ""){
$data = self::genGet();
return '<div id="captcha-wrapper" class="input-group"><label for="captcha" class="form-label d-block w-100 captcha-label">Please solve the captcha: <span class="text-danger">*</span> '.$data['yasir.math_captcha.cap_num1'].' '.$data['yasir.math_captcha.cap_opt'].' '.$data['yasir.math_captcha.cap_num2'].'</label>
<input type="text" id="captcha" name="'.$name.'" required="required" value="" class="form-control '.$input_classes.'"><button data-target="'.$target.'" type="button" class="captcha-refresh-btn input-group-text btn btn-primary"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-refresh"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M20 11a8.1 8.1 0 0 0 -15.5 -2m-.5 -4v4h4" /><path d="M4 13a8.1 8.1 0 0 0 15.5 2m.5 4v-4h-4" /></svg></button></div>
';
}
/**
* captcha input refresh
*
* @param Request $request
* @param string $name captcha input field
* @return void
*/
static function inputRefresh($name="captcha"){
self::generate();
return self::input($name="captcha");
}
/**
* captcha validation
*
* @param Request $request
* @param string $name captcha input field
* @param string $message validation message
*/
static function validate(Request $request,$name="captcha", $message = "Invalid captcha, please try again"){
$validator = \Validator::make($request->all(),[
$name => 'required|numeric'
],[
$name.'.required' => 'Captcha Field is required',
$name.'.numeric' => 'The captcha must be a number.'
]);
$validator->validate();
$data = self::get();
if($data['yasir.math_captcha.cap_opt']=='+'){
$originalOutput = ($data['yasir.math_captcha.cap_num1'] + $data['yasir.math_captcha.cap_num2']);
}
elseif($data['yasir.math_captcha.cap_opt']=='-'){
$originalOutput = ($data['yasir.math_captcha.cap_num1'] - $data['yasir.math_captcha.cap_num2']);
}elseif($data['yasir.math_captcha.cap_opt']=='*'){
$originalOutput = ($data['yasir.math_captcha.cap_num1'] * $data['yasir.math_captcha.cap_num2']);
}
if(((int) $request->get($name))!=$originalOutput){
$validator->errors()->add($name,$message);
// $validator->getMessageBag()->add($name,'Invalid captcha, please try again');
return back()->withInput()->withErrors($validator);
}
return true;
}
function resetCaptcha(Request $request){
$data = self::input($request->name,$request->target);
return $data;
}
/**
* Undocumented getScript
*
* @param string $selector to where new html code with input will be loaded
* @return void
*/
static function getScript($selector='#captcha-wrapper'){
return "$(document).on('click', '.captcha-refresh-btn', function(event) {
if($(this).data('target')){
var selector = $(this).data('target')
}else{
var selector = '".$selector."'
}
var name = $(selector).find('input').prop('name')
event.preventDefault();
/* Act on the event */
$.ajax({
data: {name: name,target:selector},
url: '".route('get.captcha')."',
})
.done(function(e) {
$(selector).html(e);
})
});";
}
}