forked from TurbineJesse/OSClass
/
login.php
executable file
·210 lines (183 loc) · 13.4 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
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
<?php if ( ! defined('ABS_PATH')) exit('ABS_PATH is not loaded. Direct access is not allowed.');
/**
* OSClass – software for creating and publishing online classified advertising platforms
*
* Copyright (C) 2010 OSCLASS
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class CWebLogin extends BaseModel
{
function __construct() {
parent::__construct() ;
if( !osc_users_enabled() ) {
osc_add_flash_error_message( _m('Users not enabled') ) ;
$this->redirectTo(osc_base_url(true));
}
}
//Business Layer...
function doModel() {
switch( $this->action ) {
case('login_post'): //post execution for the login
if(!osc_users_enabled()) {
osc_add_flash_error_message(_m('Users are not enabled'));
$this->redirectTo(osc_base_url());
}
require_once LIB_PATH . 'osclass/UserActions.php' ;
$user = User::newInstance()->findByEmail( Params::getParam('email') ) ;
$url_redirect = osc_user_dashboard_url();
$page_redirect = '';
if(osc_rewrite_enabled()) {
if(isset($_SERVER['HTTP_REFERER'])) {
$request_uri = urldecode(preg_replace('@^' . osc_base_url() . '@', "", $_SERVER['HTTP_REFERER']));
$tmp_ar = explode("?", $request_uri);
$request_uri = $tmp_ar[0];
$rules = Rewrite::newInstance()->listRules();
foreach($rules as $match => $uri) {
if(preg_match('#'.$match.'#', $request_uri, $m)) {
$request_uri = preg_replace('#'.$match.'#', $uri, $request_uri);
if(preg_match('|([&?]{1})page=([^&]*)|', '&'.$request_uri.'&', $match)) {
$page_redirect = $match[2];
}
break;
}
}
}
} else if(preg_match('|[\?&]page=([^&]+)|', $_SERVER['HTTP_REFERER'].'&', $match)) {
$page_redirect = $match[1];
}
if(Params::getParam('http_referer')!='') {
Session::newInstance()->_setReferer(Params::getParam('http_referer'));
$url_redirect = Params::getParam('http_referer');
} else if(Session::newInstance()->_getReferer()!='') {
Session::newInstance()->_setReferer(Session::newInstance()->_getReferer());
$url_redirect = Session::newInstance()->_getReferer();
} else if($page_redirect!='' && $page_redirect!='login') {
Session::newInstance()->_setReferer($_SERVER['HTTP_REFERER']);
$url_redirect = $_SERVER['HTTP_REFERER'];
}
if (!$user) {
osc_add_flash_error_message(_m('The username doesn\'t exist')) ;
$this->redirectTo(osc_user_login_url());
}
if ( $user["s_password"] != sha1( Params::getParam('password') ) ) {
osc_add_flash_error_message( _m('The password is incorrect')) ;
$this->redirectTo(osc_user_login_url());
}
$uActions = new UserActions(false);
$logged = $uActions->bootstrap_login($user['pk_i_id']) ;
if($logged==0) {
osc_add_flash_error_message(_m('The username doesn\'t exist')) ;
} else if($logged==1) {
osc_add_flash_error_message(_m('The user has not been validated yet'));
} else if($logged==2) {
osc_add_flash_error_message(_m('The user has been suspended'));
} else if($logged==3) {
if ( Params::getParam('remember') == 1 ) {
//this include contains de osc_genRandomPassword function
require_once osc_lib_path() . 'osclass/helpers/hSecurity.php';
$secret = osc_genRandomPassword() ;
User::newInstance()->update(
array('s_secret' => $secret)
,array('pk_i_id' => $user['pk_i_id'])
);
Cookie::newInstance()->set_expires( osc_time_cookie() ) ;
Cookie::newInstance()->push('oc_userId', $user['pk_i_id']) ;
Cookie::newInstance()->push('oc_userSecret', $secret) ;
Cookie::newInstance()->set() ;
}
$this->redirectTo( $url_redirect ) ;
} else {
osc_add_flash_error_message(_m('This should never happens'));
}
if(!$user['b_enabled']) {
$this->redirectTo(osc_user_login_url());
}
$this->redirectTo(osc_user_login_url());
break ;
case('recover'): //form to recover the password (in this case we have the form in /gui/)
$this->doView( 'user-recover.php' ) ;
break ;
case('recover_post'): //post execution to recover the password
require_once LIB_PATH . 'osclass/UserActions.php' ;
// e-mail is incorrect
if( !preg_match('|^[a-z0-9\.\_\+\-]+@[a-z0-9\.\-]+\.[a-z]{2,3}$|i', Params::getParam('s_email')) ) {
osc_add_flash_error_message( _m('Invalid email address') ) ;
$this->redirectTo( osc_recover_user_password_url() );
}
$userActions = new UserActions(false) ;
$success = $userActions->recover_password() ;
switch ($success) {
case(0): // recover ok
osc_add_flash_ok_message( _m('We have sent you an email with the instructions to reset your password')) ;
$this->redirectTo( osc_base_url() ) ;
break;
case(1): // e-mail does not exist
osc_add_flash_error_message( _m('We were not able to identify you given the information provided')) ;
$this->redirectTo( osc_recover_user_password_url() ) ;
break;
case(2): // recaptcha wrong
osc_add_flash_error_message( _m('The recaptcha code is wrong')) ;
$this->redirectTo( osc_recover_user_password_url() ) ;
break;
}
break ;
case('forgot'): //form to recover the password (in this case we have the form in /gui/)
$user = User::newInstance()->findByIdPasswordSecret(Params::getParam('userId'), Params::getParam('code'));
if($user) {
$this->doView( 'user-forgot_password.php' ) ;
} else {
osc_add_flash_error_message( _m('Sorry, the link is not valid')) ;
$this->redirectTo( osc_base_url() ) ;
}
break;
case('forgot_post'):
if( (Params::getParam('new_password') == '') || (Params::getParam('new_password2') == '') ) {
osc_add_flash_warning_message( _m('Password cannot be blank')) ;
$this->redirectTo(osc_forgot_user_password_confirm_url(Params::getParam('userId'), Params::getParam('code')));
}
$user = User::newInstance()->findByIdPasswordSecret(Params::getParam('userId'), Params::getParam('code'));
if($user['b_enabled'] == 1) {
if(Params::getParam('new_password')==Params::getParam('new_password2')) {
User::newInstance()->update(
array('s_pass_code' => osc_genRandomPassword(50)
, 's_pass_date' => date('Y-m-d H:i:s', 0)
, 's_pass_ip' => $_SERVER['REMOTE_ADDR']
, 's_password' => sha1(Params::getParam('new_password'))
), array('pk_i_id' => $user['pk_i_id'])
);
osc_add_flash_ok_message( _m('The password has been changed'));
$this->redirectTo(osc_user_login_url());
} else {
osc_add_flash_error_message( _m('Error, the password don\'t match')) ;
$this->redirectTo(osc_forgot_user_password_confirm_url(Params::getParam('userId'), Params::getParam('code')));
}
} else {
osc_add_flash_error_message( _m('Sorry, the link is not valid')) ;
}
$this->redirectTo( osc_base_url() ) ;
break;
default: //login
if( osc_logged_user_id() != '') {
$this->redirectTo(osc_user_dashboard_url());
}
$this->doView( 'user-login.php' ) ;
}
}
//hopefully generic...
function doView($file) {
osc_run_hook("before_html");
osc_current_web_theme_path($file) ;
osc_run_hook("after_html");
}
}
?>