forked from osclass/Osclass
/
login.php
138 lines (121 loc) · 7.9 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
<?php
/**
* 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() ;
}
//Business Layer...
function doModel() {
switch( $this->action ) {
case('login_post'): //post execution for the login
$user = User::newInstance()->findByEmail( Params::getParam('email') ) ;
if (!$user) {
osc_add_flash_message(_m('The username doesn\'t exist')) ;
$this->redirectTo(osc_user_login_url());
}
if(!$user['b_enabled']) {
osc_add_flash_message(_m('The user has not been validated yet'));
$this->redirectTo(osc_user_login_url());
}
if ( $user["s_password"] == sha1( Params::getParam('password') ) ) {
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() ;
}
//we are logged in... let's go!
Session::newInstance()->_set('userId', $user['pk_i_id']) ;
Session::newInstance()->_set('userName', $user['s_name']) ;
Session::newInstance()->_set('userEmail', $user['s_email']) ;
$phone = ($user['s_phone_mobile']) ? $user['s_phone_mobile'] : $user['s_phone_land'];
Session::newInstance()->_set('userPhone', $phone) ;
} else {
osc_add_flash_message( _m('The password is incorrect')) ;
}
//returning logged in to the main page...
$this->redirectTo( osc_user_dashboard_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' ;
$userActions = new UserActions(false) ;
$recaptcha_ok = $userActions->recover_password() ;
if($recaptcha_ok) {
// We ALWAYS show the same message, so we don't give clues about which emails are in our database and which don't!
osc_add_flash_message( _m('We have sent you an email with the instructions to reset your password')) ;
$this->redirectTo( osc_base_url() ) ;
} else {
osc_add_flash_message( _m('The recaptcha code is wrong')) ;
$this->redirectTo( osc_recover_user_password_url() ) ;
}
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_message( _m('Sorry, the link is not valid')) ;
$this->redirectTo( osc_base_url() ) ;
}
break;
case('forgot_post'):
$user = User::newInstance()->findByIdPasswordSecret(Params::getParam('userId'), Params::getParam('code'));
if($user) {
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_message( _m('The password has been changed'));
$this->redirectTo(osc_user_login_url());
} else {
osc_add_flash_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_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_current_web_theme_path($file) ;
}
}
?>