-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeneral.php
260 lines (229 loc) · 6.88 KB
/
general.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
class General
{
public static function processing_get($global)
{
$get = array();
// Set part
$get['part'] = self::cleanVars($global, 'part', 'index', 'string');
// Set code
$get['message'] = self::cleanVars($global, 'message', '', 'int');
return $get;
}
public static function processing_post($global)
{
$post = array();
// Set filename
$post['filename'] = self::cleanVars($global, 'filename', '', 'string');
// Set email
$post['email'] = self::cleanVars($global, 'email', '', 'mail');
// Set password
$post['password'] = self::cleanVars($global, 'password', '', 'string');
return $post;
}
public static function cleanVars(&$global, $key, $default = '', $type = 'int')
{
switch ($type) {
case 'array':
$ret = (isset($global[$key]) && is_array($global[$key])) ? $global[$key] : $default;
break;
case 'date':
$ret = (isset($global[$key])) ? strtotime($global[$key]) : $default;
break;
case 'string':
$ret = (isset($global[$key])) ? filter_var($global[$key], FILTER_SANITIZE_MAGIC_QUOTES) : $default;
break;
case 'mail':
$ret = (isset($global[$key])) ? filter_var($global[$key], FILTER_VALIDATE_EMAIL) : $default;
break;
case 'url':
$ret = (isset($global[$key])) ? filter_var($global[$key], FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) : $default;
break;
case 'ip':
$ret = (isset($global[$key])) ? filter_var($global[$key], FILTER_VALIDATE_IP) : $default;
break;
case 'amp':
$ret = (isset($global[$key])) ? filter_var($global[$key], FILTER_FLAG_ENCODE_AMP) : $default;
break;
case 'text':
$ret = (isset($global[$key])) ? htmlentities($global[$key], ENT_QUOTES, 'UTF-8') : $default;
break;
case 'int': default:
$ret = (isset($global[$key])) ? filter_var($global[$key], FILTER_SANITIZE_NUMBER_INT) : $default;
break;
}
if ($ret === false) {
return $default;
}
return $ret;
}
public static function redirect($path = '')
{
$url = Config::getHost('mainUrl');
if (!empty($path)) {
$url = sprintf('%s/%s',$url, $path);
}
header('Location: ' . $url);
exit;
}
public function getIp()
{
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
//check for ip from share internet
$ip = $_SERVER["HTTP_CLIENT_IP"];
} elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
// Check for the Proxy User
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
$ip = $_SERVER["REMOTE_ADDR"];
}
return $ip;
}
public static function setMessage($id = '')
{
// Set form html
$html = <<<'EOT'
<div class="alert %s" role="alert">
<h4>%s</h4>
<p>%s</p>
</div>
EOT;
$class = 'alert-danger';
$title = Language::getText('messageError');
switch ($id) {
case 1:
$text = Language::getText('messageLoginEmpty');
break;
case 2:
$text = Language::getText('messageLoginWrongEmail');
break;
case 3:
$text = Language::getText('messageLoginWrongPassword');
break;
case 4:
$class = 'alert-success';
$title = Language::getText('messageSuccess');
$text = Language::getText('messageLoginSucces');
break;
case 5:
$class = 'alert-warning';
$title = Language::getText('messageWarning');
$text = Language::getText('messageNotLogin');
break;
case 6:
$class = 'alert alert-info';
$title = Language::getText('messageLoginout');
$text = Language::getText('messageLoginoutText');
break;
}
$message = sprintf(
$html,
$class,
$title,
$text
);
return $message;
}
public static function loginForm()
{
// Set form html
$form = <<<'EOT'
<form class="form-horizontal well" action="%s" method="post">
<div class="form-group">
<label for="loginInputEmail" class="col-sm-2 control-label">%s</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="loginInputEmail" name="email">
</div>
</div>
<div class="form-group">
<label for="loginInputPassword" class="col-sm-2 control-label">%s</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="loginInputPassword" name="password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">%s</button>
</div>
</div>
</form>
EOT;
// Set action
$action = sprintf(
'%s/index.php?part=doLogin',
Config::getHost('mainUrl')
);
// Set form elements
$form = sprintf(
$form,
$action,
Language::getText('formLoginEmail'),
Language::getText('formLoginPassword'),
Language::getText('formLoginSubmit')
);
// return form
return $form;
}
public static function doLogin($post = array())
{
// Check is not empty
if (empty($post['email']) || empty($post['password'])) {
self::redirect('index.php?part=index&message=1');
}
// hash password
$password = md5($post['password']);
// Get system setting
$system = array(
'email' => Config::getHost('loginEmail'),
'pass' => md5(Config::getHost('loginPass')),
);
// Check email
if ($system['email'] != $post['email']) {
self::redirect('index.php?part=index&message=2');
}
// Check password
if ($system['pass'] != $password) {
self::redirect('index.php?part=index&message=3');
}
// do login
$time = time();
$ip = self::getIp();
// Set session
$_SESSION['fg_login'] = 1;
$_SESSION['fg_ip'] = $ip;
$_SESSION['fg_time'] = $time;
// back to index
self::redirect('index.php?part=index&message=4');
}
public static function doLogout()
{
session_destroy();
self::redirect('index.php?part=login&message=6');
}
public static function checkLogin()
{
if (!empty($_SESSION)) {
if (isset($_SESSION['fg_login']) &&
$_SESSION['fg_login'] == 1 &&
isset($_SESSION['fg_ip']) &&
!empty($_SESSION['fg_ip']) &&
isset($_SESSION['fg_time']) &&
!empty($_SESSION['fg_time'])
) {
// Get ip
$ip = self::getIp();
// Check
if ($ip == $_SESSION['fg_ip']) {
return 1;
} else {
self::redirect('index.php?part=login&message=5');
}
} else {
self::redirect('index.php?part=login&message=5');
}
} else {
self::redirect('index.php?part=login&message=5');
}
}
}
?>