forked from CodeForAfrica/GotToVote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerificationController.php
101 lines (85 loc) · 2.89 KB
/
VerificationController.php
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
<?php
use Neutron\ReCaptcha\ReCaptcha;
class VerificationController extends BaseController {
/*
|--------------------------------------------------------------------------
| Default Home Controller
|--------------------------------------------------------------------------
|
| You may wish to use controllers instead of, or in addition to, Closure
| based routes. That's great! Here is an example controller method to
| get you started. To route to this controller, just add the route:
|
| Route::get('/', 'VerificationController@getIndex');
|
*/
public function getIndex()
{
// Initialize reCaptcha
$recaptcha = ReCaptcha::create(Config::get('app.gtv.recaptcha.public_key'), Config::get('app.gtv.recaptcha.private_key'));
return View::make('verify', array(
'reg_no' => '',
'message' => '<p>Enter your registration number first.</p>',
'recaptcha' => $recaptcha
));
}
public function verifyRegistration()
{
// Initialize variables
$reg_no = Input::get('reg_no');
// Check reCaptcha
$recaptcha = ReCaptcha::create(Config::get('app.gtv.recaptcha.public_key'), Config::get('app.gtv.recaptcha.private_key'));
$response = $recaptcha->checkAnswer($_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
if ($response->isValid())
{
// No voter number
if ($reg_no == '') {
$message = '<p class="text-danger">Enter your registration number first.</p>';
return View::make('verify', array(
'reg_no' => $reg_no,
'message' => $message,
'recaptcha' => $recaptcha
));
}
// Validate registration no
$validator = Validator::make(
array('reg_no' => $reg_no),
array('reg_no' => 'numeric|min:5')
);
if ($validator->fails())
{
// The given data did not pass validation
$message = '<p class="text-danger">The registration number entered does not seem to be valid. Please check it and try again.</p>';
return View::make('verify', array(
'reg_no' => $reg_no,
'message' => $message,
'recaptcha' => $recaptcha
));
}
// Fetch from GTV VRC API
$message = $this->fetchFromAPI($reg_no);
return View::make('verify', array(
'reg_no' => $reg_no,
'message' => $message,
'recaptcha' => $recaptcha
));
} else {
// Captcha not valid
$message = '<p class="text-danger">Captcha not valid. Please try again.</p>';
return View::make('verify', array(
'reg_no' => $reg_no,
'message' => $message,
'recaptcha' => $recaptcha
));
}
}
public function fetchFromAPI($reg_no)
{
$url = Config::get('app.gtv.api.url').'/web?reg_no='.$reg_no;
$response = json_decode(file_get_contents($url));
if ($response->success == 'false') {
return '<p class="text-danger">'.$response->message.'</p>';
}
return '<p class="text-success">'.$response->message.'</p>';
}
}