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
|
<?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 program 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.
//
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
if (!defined('EG')) die('Direct access not allowed!');
class UsersModel extends Model_Map
{
public static $usersList = array();
public function __construct()
{
$this->_tables='regusers,reggroups,regusers_groups';
$this->_idFields='id_user,id_group';
$this->_where=array(
'id_group' => 'reggroups',
'id_user' => 'regusers',
'name' => 'reggroups',
'confirmation_token'=> 'regusers',
'has_confirmed' => 'regusers',
'deleted' => 'regusers',
'forgot_token' => 'regusers'
);
$this->_popupItemNames = array(
'has_confirmed'=>'has_confirmed',
'deleted'=>'deleted',
'id_group'=>'name',
);
$this->_popupLabels = array(
'has_confirmed'=>'HAS CONFIRMED?',
'deleted'=>'DELETED?',
'id_group'=>'GROUP'
);
$this->orderBy = 'regusers.id_user desc';
parent::__construct();
$this->deleteNotRegistered();
}
public function deleteNotRegistered()
{
$limit = time() - Account::$confirmTime;
$this->db->del('regusers','has_confirmed = 1 and deleted = "no" and creation_time < '.$limit);
}
public function getUser($id_user = 0)
{
$clean['id_user'] = (int)$id_user;
if (array_key_exists($clean['id_user'],self::$usersList))
{
return self::$usersList[$clean['id_user']];
}
else
{
$user = $this->db->select('regusers','username,has_confirmed','id_user='.$clean['id_user']);
if (count($user) > 0)
{
$fuser = (strcmp($user[0]['regusers']['has_confirmed'],0) === 0) ? $user[0]['regusers']['username'] : "__".$user[0]['regusers']['username'];
self::$usersList[$clean['id_user']] = $fuser;
return $fuser;
}
else
{
return "__";
}
}
}
public function insert()
{
//create the token
$confirmation_token = md5(randString(20));
$this->values['confirmation_token'] = $confirmation_token;
//has_confirmed flag
$this->values['has_confirmed'] = 1;
$this->values['creation_time'] = time();
//random ID
$randomId = md5(randString(5).uniqid(mt_rand(),true));
$this->values["temp_field"] = $randomId;
if (isset($_POST['captcha']))
{
if ( strcmp($_SESSION['captchaString'],$_POST['captcha']) === 0 )
{
parent::insert();
if ($this->queryResult)
{
$resId = $this->db->select("regusers","id_user","temp_field='$randomId'");
$clean['id_user'] = $resId[0]['regusers']['id_user'];
$this->db->update("regusers",'temp_field',array(''),'id_user='.$clean['id_user']);
$result = Account::confirm($this->values['username'],$this->values['e_mail'],$clean['id_user'],$confirmation_token);
if ($result)
{
$_SESSION['status'] = 'sent';
}
else
{
$_SESSION['status'] = 'regerror';
}
$hed = new HeaderObj(DOMAIN_NAME);
$hed->redirect('users/notice/'.Lang::$current);
}
}
else
{
$this->result = false;
$this->queryResult = false;
$this->notice = "<div class='alert'>Wrong captcha code...</div>\n";
}
}
}
public function close($id_user)
{
$clean['id_user'] = (int)$id_user;
$this->values = array(
'has_confirmed' => 1,
'deleted' => 'yes',
'e_mail' => ''
);
if ($this->update($clean['id_user']))
{
$_SESSION['status'] = 'deleted';
$profile = new ProfileModel();
$res = $profile->db->select('profile','id_prof','created_by='.$clean['id_user']);
if (count($res) > 0)
{
$clean['id_prof'] = (int)$res[0]['profile']['id_prof'];
$profile->values = array(
'real_name' => '',
'where_you_are' => '',
'birth_date' => '',
'fav_distro' => '',
'projects' => '',
'description' => ''
);
$profile->update($clean['id_prof']);
}
}
}
public function forgot($username)
{
$clean['username'] = ctype_alnum($username) ? sanitizeAll($username) : '';
if (isset($_POST['captcha']))
{
if ( strcmp($_SESSION['captchaString'],$_POST['captcha']) === 0 )
{
$res = $this->db->select('regusers','e_mail,id_user','username="'.$clean['username'].'" and has_confirmed = 0 and deleted = "no"');
if (count($res) > 0)
{
$e_mail = $res[0]['regusers']['e_mail'];
$id_user = (int)$res[0]['regusers']['id_user'];
$forgot_token = md5(randString(20));
$forgot_time = time();
$updateArray = array($forgot_token, $forgot_time);
$this->db->update('regusers','forgot_token,forgot_time',$updateArray,'username="'.$clean['username'].'"');
$result = Account::sendnew($clean['username'],$e_mail,$id_user,$forgot_token);
if ($result)
{
$_SESSION['status'] = 'sent_new';
}
else
{
$_SESSION['status'] = 'sent_new_error';
}
$hed = new HeaderObj(DOMAIN_NAME);
$hed->redirect('users/notice/'.Lang::$current,1);
}
else
{
$this->notice = "<div class='alert'>the user does not exist</div>\n";
}
}
else
{
$this->result = false;
$this->queryResult = false;
$this->notice = "<div class='alert'>Wrong captcha code...</div>\n";
}
}
}
}
|