/
Login.php
125 lines (100 loc) · 2.77 KB
/
Login.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
public function index()
{
$this->load->library('migration');
if($this->Employee->is_logged_in())
{
redirect('home');
}
else
{
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('username', 'lang:login_username', 'required|callback_login_check');
if($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else
{
redirect('home');
}
}
}
public function login_check($username)
{
if(!$this->installation_check())
{
$this->form_validation->set_message('login_check', $this->lang->line('login_invalid_installation'));
return FALSE;
}
if(!$this->migration->is_latest())
{
set_time_limit(3600);
// trigger any required upgrade before starting the application
$this->migration->latest();
}
$password = $this->input->post('password');
if(!$this->Employee->login($username, $password))
{
$this->form_validation->set_message('login_check', $this->lang->line('login_invalid_username_and_password'));
return FALSE;
}
if($this->config->item('gcaptcha_enable'))
{
$g_recaptcha_response = $this->input->post('g-recaptcha-response');
if(!$this->gcaptcha_check($g_recaptcha_response))
{
$this->form_validation->set_message('login_check', $this->lang->line('login_invalid_gcaptcha'));
return FALSE;
}
}
return TRUE;
}
private function gcaptcha_check($response)
{
if(!empty($response))
{
$check = array(
'secret' => $this->config->item('gcaptcha_secret_key'),
'response' => $response,
'remoteip' => $this->input->ip_address()
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($check));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$status = json_decode($result, TRUE);
if(!empty($status['success']))
{
return TRUE;
}
}
return FALSE;
}
private function installation_check()
{
// get PHP extensions and check that the required ones are installed
$extensions = implode(', ', get_loaded_extensions());
$keys = array('bcmath', 'intl', 'gd', 'openssl', 'mbstring', 'curl');
$pattern = '/';
foreach($keys as $key)
{
$pattern .= '(?=.*\b' . preg_quote($key, '/') . '\b)';
}
$pattern .= '/i';
$result = preg_match($pattern, $extensions);
if(!$result)
{
error_log('Check your php.ini');
error_log('PHP installed extensions: ' . $extensions);
error_log('PHP required extensions: ' . implode(', ', $keys));
}
return $result;
}
}
?>