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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
<?php
// h-source, a web software to build a community of people that want to share their hardware information.
// Copyright (C) 2010 Antonio Gallo (h-source-copyright.txt)
//
// This file is part of h-source
//
// h-source is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// h-source is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with h-source. If not, see <http://www.gnu.org/licenses/>.
if (!defined('EG')) die('Direct access not allowed!');
class UsersController extends BaseController
{
public function __construct($model, $controller, $queryString)
{
parent::__construct($model, $controller, $queryString);
$this->model('UsersModel');
$this->model('ProfileModel');
$this->model('HardwareModel');
$data['title'] = 'Login';
$this->append($data);
}
public function login($lang = 'en')
{
$data = array();
if ( strcmp($this->_updating,'no') === 0 )
{
$data['flag'] = isset($type) ? 'setted' : null;
$data['title'] = 'Login - '.Website::$generalName;
$redirect = $this->request->get('redirect','','sanitizeAll');
$data['action'] = Url::getRoot("users/login/".$this->lang."?redirect=$redirect");
$data['notice'] = null;
$this->s['registered']->checkStatus();
if ($this->s['registered']->status['status']=='logged') { //check if already logged
$this->redirect('home/index/'.Lang::$current,3,'You are already logged...');
die();
}
if (isset($_POST['username']) and isset($_POST['password']))
{
$username = ctype_alnum($_POST['username']) ? sanitizeAll($_POST['username']) : '';
$choice = $this->s['registered']->login($username,$_POST['password']);
switch($choice) {
case 'logged':
$this->redirect('home/index/'.Lang::$current,3,'You are already logged...');
break;
case 'accepted':
if (strcmp($redirect,'') !== 0)
{
$redirect = html_entity_decode($redirect,ENT_QUOTES,DEFAULT_CHARSET);
header('Location: http://'.DOMAIN_NAME."/".$redirect);
}
else
{
$this->redirect('home/index/'.Lang::$current,0);
}
break;
case 'login-error':
$data['notice'] = '<div class="alert">Wrong username or password</div>';
break;
case 'wait':
$data['notice'] = '<div class="alert">You have to wait 5 seconds before you can try to login another time</div>';
break;
}
}
}
$this->append($data);
$this->load('login');
}
public function logout($lang = 'en')
{
$res = $this->s['registered']->logout();
if ($res === 'not-logged')
{
$data['notice'] = "<div class='alert'>You can't logout because you are not logged...</div>\n";
}
else if ($res === 'was-logged')
{
$this->redirect('home/index/'.Lang::$current,0);
}
else if ($res === 'error')
{
}
$this->append($data);
$this->load('logout');
}
public function add($lang = 'en')
{
$data['title'] = 'create account - '.Website::$generalName;
if ( strcmp($this->_updating,'no') === 0 )
{
//start session for captcha
session_start();
if ( isset($_SESSION['status']) ) unset($_SESSION['status']);
$this->shift(1);
$this->m['UsersModel']->strongConditions['insert'] = array(
"checkAlphanum" => "username",
"checkLength|35" => "username",
"checkMail" => "e_mail",
"+checkLength|60" => "e_mail",
"checkEqual" => "password,confirmation",
"checkMatch|/^[a-zA-Z0-9\_\-\!]+$/" => "password,confirmation|characters allowed for the password: a-z A-Z 0-9 - _ !"
);
$this->m['UsersModel']->databaseConditions['insert'] = array(
"checkUnique" => "username|the username is already present",
"+checkUnique" => "e_mail|the e-mail address is already present"
);
if ($this->s['registered']->status['status'] === 'logged')
{
$this->redirect('home/index/'.$this->lang,2,'you are already logged..');
}
else
{
$data['notice'] = null;
$this->m['UsersModel']->setFields('username:sanitizeAll,e_mail:sanitizeAll,password:sha1','none');
$this->m['UsersModel']->updateTable('insert');
$data['notice'] = $this->m['UsersModel']->notice;
$values = $this->m['UsersModel']->getFormValues('insert','sanitizeHtml');
$values['confirmation'] = '';
$data['values'] = $values;
$this->append($data);
$this->load('add');
$this->right();
}
}
else
{
$this->redirect('users/login/'.$this->lang,0);
}
}
public function confirm($lang = 'en', $id_user = 0, $confirmation_token = '')
{
$data['title'] = 'confirm account - '.Website::$generalName;
if ( strcmp($this->_updating,'no') === 0 )
{
if ($this->s['registered']->status['status'] === 'logged')
{
$this->redirect('home/index/'.$this->lang,2,'you are already logged..');
}
else
{
$clean['id_user'] = (int)$id_user;
$clean['confirmation_token'] = sanitizeAlphanum($confirmation_token);
$data['status_confirm'] = false;
$res = $this->m['UsersModel']->select('id_user,creation_time')->where(array("id_user"=>$clean['id_user'],"confirmation_token"=>$clean['confirmation_token'],"has_confirmed"=>1,"deleted"=>"no"))->send();
// echo $this->m['UsersModel']->getQuery();
if (count($res) > 0)
{
$now = time();
$checkTime = $res[0]['regusers']['creation_time'] + Account::$confirmTime;
if ($checkTime > $now)
{
$this->m['UsersModel']->values = array('has_confirmed' => 0, 'creation_time' => 0);
if ($this->m['UsersModel']->update($clean['id_user']))
{
$data['status_confirm'] = true;
//ad a record in the profile table
$this->m['ProfileModel']->values = array('created_by' => $clean['id_user']);
$this->m['ProfileModel']->insert();
}
}
}
// var_dump($data['status_confirm']);
$this->append($data);
$this->load('confirmation');
$this->right();
}
}
else
{
$this->redirect('users/login/'.$this->lang,0);
}
}
public function change($lang = 'en', $id_user = 0, $forgot_token = '')
{
session_start();
$data['title'] = 'change password - '.Website::$generalName;
if ( strcmp($this->_updating,'no') === 0 )
{
if ($this->s['registered']->status['status'] === 'logged')
{
$this->redirect('home/index/'.$this->lang,2,'you are already logged..');
}
else
{
$clean['id_user'] = (int)$id_user;
$clean['forgot_token'] = sanitizeAlphanum($forgot_token);
$res = $this->m['UsersModel']->select('username,id_user,forgot_time,e_mail')->where(array("id_user"=>$clean['id_user'],"forgot_token"=>$clean['forgot_token'],"has_confirmed"=>0,"deleted"=>"no"))->send();
if (count($res) > 0)
{
$now = time();
$checkTime = $res[0]['regusers']['forgot_time'] + Account::$confirmTime;
if ($checkTime > $now)
{
$username = $res[0]['regusers']['username'];
$email = $res[0]['regusers']['e_mail'];
$newPassword = generateString(10);
$this->m['UsersModel']->values = array('password' => sha1($newPassword), 'forgot_time' => 0);
if ($this->m['UsersModel']->update($clean['id_user']))
{
$result = Account::sendpassword($username,$email,$newPassword);
if ($result)
{
$_SESSION['status'] = 'sent_new_password';
}
else
{
$_SESSION['status'] = 'sent_new_password_error';
}
$hed = new HeaderObj(DOMAIN_NAME);
$hed->redirect('users/notice/'.Lang::$current,1);
}
}
}
$this->append($data);
$this->load('change');
$this->right();
}
}
else
{
$this->redirect('users/login/'.$this->lang,0);
}
}
public function forgot($lang = 'en')
{
$data['title'] = 'request password - '.Website::$generalName;
if ( strcmp($this->_updating,'no') === 0 )
{
session_start();
if ( isset($_SESSION['status']) ) unset($_SESSION['status']);
$this->shift(1);
if ($this->s['registered']->status['status'] === 'logged')
{
$this->redirect('home/index/'.$this->lang,2,'you are already logged..');
}
else
{
$data['notice'] = null;
if (isset($_POST['forgotAction']))
{
if (isset($_POST['username']))
{
$this->m['UsersModel']->forgot($_POST['username']);
$data['notice'] = $this->m['UsersModel']->notice;
}
}
$this->append($data);
$this->load('forgot');
$this->right();
}
}
else
{
$this->redirect('users/login/'.$this->lang,0);
}
}
public function notice($lang = 'en')
{
$data['title'] = 'notice - '.Website::$generalName;
if ( strcmp($this->_updating,'no') === 0 )
{
session_start();
if ($this->s['registered']->status['status'] === 'logged')
{
$this->redirect('home/index/'.$this->lang,2,'you are already logged..');
}
else
{
$this->load('notice');
$this->right();
}
}
else
{
$this->redirect('users/login/'.$this->lang,0);
}
}
}
|