-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_TcUser.php
More file actions
executable file
·315 lines (280 loc) · 16.3 KB
/
_TcUser.php
File metadata and controls
executable file
·315 lines (280 loc) · 16.3 KB
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
<?PHP
/* ------------------------------------------------------------------------------------------------------------
Mantenedor : Rodrigo Dittmar
Dependencias : TcClass _functions _TcDebug _TcDB
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Classe desenvolvida por Rodrigo Dittmar para o projeto SICE - Empresa ESolution Informática
Licenciado para uso sem fins comerciais para o projeto CLibras Aberta - UFPR
------------------------------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------------------------------
Classe : TcUser
Descricao : Classe para Gerenciamento de Usuario
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Controle de Versão
1.0.0 : 20051201 - Versão inicial
1.0.1 : 20060401 - Otimização do código
1.0.2 : 20060501 - Otimização do código e correção de BUGS
1.2.0 : 20061001 - Implementacao da classe para o AJAX, Otimizacao
1.3.0 : 20070331 - Adaptacao a classe TcDB
1.3.0 : 20070630 - Correcao Codificacao Erros/Definicao do Log Debug em Niveis
Niveis de Debug
0 - Construtores / Destrutores
1 - Login do Usuario
2 - Logout e Revalidacao do Usuario / active
9 - Auxiliares / Privadas / Sem muita importancia
1.5.0 : 20191201 - Adaptação da Classe para o sistema CLibras
Atualização do Log de Acesso - Gravado no DB
Padronização da nomenclatura de variáveis
Mascaramento da sessão conforme o nome do Projeto (MD5)
Definição de funções privadas
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$FConfig = array('multiple' => false, // Múltiplas instâncias
'timeout' => 6000 , // Timeout
'project' => null , // Dados do projeto
'log' => array('access' => true ,
'password' => true ),
'pass' => array(3,25), // Tamanho da senha mínimo, máximo
'validate' => array('email','register','code','rf') );
------------------------------------------------------------------------------------------------------------ */
class TcUser {
private $FOwner = ''; //Proprietario da classe (nome de quem instanciou)
private $FDB; //Classe de acesso ao TcDB
private $FQuery; //Classe de execução da Query TcQuery
private $FSession; //Nome do cookie para armazenar a seção
private $FConfig = array(); //Vetor de Configurações
private $FIP = ''; //Endereço IP - ipv4/ipv6 do usuário
private $FDebug = false; //recurso para gravar log do debug
private $FDebugLevel = array(); //Niveis de gravacao do debug
/* -----------------=-----------=----------------------------------------------------------------------------
__construct(0) : Cria a classe e atualiza os atributos
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
aOwner :string : Proprietario do Objeto
aConfig :array : Array de Configuração (modelo $esDB)
aTcDB :&TcDB : Objeto Banco de Dados
aDebug :&tcDebug : Objeto Debug
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
---------------------------------------------------------------------------------------------------------- */
function __construct( $aOwner = '' , $aConfig , &$aTcDB , &$aTcDebug = false ) {
$this -> FOwner = $aOwner; // Proprietário da Classe
$this -> FDB = $aTcDB; // Objeto TcDB
$this -> FDebug = $aTcDebug; // Objeto TcDebug
$this -> FDebugLevel = array(0,0,0,0,0,0,0,0,0,0); // Nivel do Log/Debug
$this -> FConfig = array('multiple' => true, // Múltiplas instâncias
'timeout' => 6000 , // Timeout - Login
'project' => null , // Dados do projeto
'log' => array('access' => true ,
'password' => true ),
'validate' => array('email','register','code')
); //,'rf') );
//Setando os parametros da classe -- Apenas o que for necessário alterar
if( is_array($aConfig) ) {
foreach ($aConfig as $a=>$b) {
$this -> FConfig[strtolower($a)] = $b;
};
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Definindo a variável da sessão
$this -> FSession = md5($this -> FConfig['project']);
session_name( $this -> FSession ) ;
session_set_cookie_params ( 0 , $this -> FSession );
session_start();
//Definindo os Niveis de Backup
if( is_object($this -> FDebug) ) {
$this -> FDebugLevel = $this -> FDebug -> FWrite;
$this -> logDebug( __METHOD__ , 'Debug Level :' . implode($this -> FDebugLevel,",") );
}
}
/* -----------------=-----------=----------------------------------------------------------------------------
__destruct(0) : Libera a memoria alocada e encerra a coneccao
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-----------------------------------------------------------------------------------------------------------*/
function __destruct() {
unset( $this -> FQuery );
if( $this -> FDebugLevel[0] ) $this -> logDebug( __METHOD__ );
}
/* -----------------=-----------=----------------------------------------------------------------------------
logDebug : Armazena o Log de Debug
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
aFunction :string : Evento da Classe
aDes :string : Descricao do Evento
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
---------------------------------------------------------------------------------------------------------- */
private function logDebug( $aFunction , $aDes = '' ) {
$this -> FDebug -> log( $this -> FOwner, $aFunction , $aDes );
}
/* -----------------=-----------=----------------------------------------------------------------------------
logDB(9) : Adiciona o log de acesso ao DB
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
aType : Tipo de LOG
1 - Log de Acesso / Login / Logout
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-----------------------------------------------------------------------------------------------------------*/
private function logDB( $aType ) {
// Conectando ao DB
if( !$this ->FQuery ) {
$this -> FQuery = new TcQuery_light( $this -> FOwner.'.TcUser' , $this -> FDB , $this -> FDebug );
}
// INACABADO
if( $this -> FConfig['log']['access'] ) {
} else if ( $this -> FConfig['log']['login'] == "Y" ) {
}
}
/* -----------------=-----------=----------------------------------------------------------------------------
setTimeOut : Verifica o Tempo de Login e atualiza evitando o timeout
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
aUser :string : Nome do Usuario
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
---------------------------------------------------------------------------------------------------------- */
private function setTimeOut( $aTime ) {
if( $this -> FDebugLevel[0] ) $this -> logDebug( __METHOD__ , $aTime );
$this -> $FConfig['timeout'] = $aTime;
}
/* ==========================================================================================================
Validacao do Usuario
/* -----------------=-----------=----------------------------------------------------------------------------
login(1) : Loga o usuario ao sistema
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
aUser :string : Nome do Usuario
aPass :string : Senha
aRelogin :bool : Primeiro acesso ao sistema
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
:Boolean : Sucesso ou Fracasso
---------------------------------------------------------------------------------------------------------- */
function login( $aUser , $aPass , $aRelogin = false ) {
//Limites do campo de Login
if( ( ( strlen( trim($aUser) ) <= ES_LOGINPASS_MIN ) or ( strlen( trim($aUser) ) > ES_LOGINPASS_MAX ) ) or
( ( strlen( trim($aPass) ) <= ES_LOGINPASS_MIN ) or ( strlen( trim($aPass) ) > ES_LOGINPASS_MAX ) ) ) {
if( $this -> FDebugLevel[1] ) $this -> logDebug( __METHOD__ , 'error login/password length (ES_LOGINPASS_MIN .. ES_LOGINPASS_MAX)' );
return( false );
}
if( $this -> FDebugLevel[1] ) $this -> logDebug( __METHOD__ , ( $aRelogin?"Relogin":"" ) );
// Caso solicite apenas validacao de usuario
if( $aRelogin ) {
return( $this -> active() );
}
//Criando a Classe de acesso ao DB
if( !$this ->FQuery ) {
$this -> FQuery = new TcQuery_light( $this -> FOwner.'.TcUser' , $this -> FDB , $this -> FDebug );
}
$aCol = $this -> FConfig['validate'];
foreach( $aCol as $c => $v ) {
$aCol[$c] = $v . " = '{$aUser}'";
}
$aQuery = "select idUser, name, code, password, SocialName, avatar, skin from cl_user " .
" where ( " . implode(' or ', $aCol ) . " ); ";
//Inicializando os Recursos e validando o login
if( $this -> query( $aQuery ) == 0 ) {
//Recuperando a Primeira linha de dados
$aRow = $this -> FQuery -> fetchRow();
if( $this -> FDebugLevel[9] ) $this -> logDebug( __METHOD__ , $aRow );
//Validando a Senha
if ( strcmp( trim($aRow['password']) , trim(password($aPass)) ) == 0 ) {
if( $this -> FDebugLevel[1] ) $this -> logDebug( __METHOD__ , "login sucessfull" );
$_SESSION['id'] = $aRow['iduser']; // id do usuário
$_SESSION['user'] = $aRow['code']; // Usuário
$_SESSION['ip'] = get_IP(); // Endereço IP
$_SESSION['login'] = time(); // Horario do Ultimo login
$_SESSION['lupdate'] = time(); // Horario da atualização do sistema
// Definindo os módulos acessíveis
// Criar a rotina de consulta SQL - Verificar os atributos e direitos
$_SESSION['modules'] = ['usudta' => true];
return(true);
} else
if( $this -> FDebugLevel[1] ) $this -> logDebug( __METHOD__ , "user error (".password($aPass).")");
} else
if( $this -> FDebugLevel[1] ) $this -> logDebug( __METHOD__ , "error in select (".$aQuery.")");
return(false);
}
/* -----------------=-----------=----------------------------------------------------------------------------
Logout(1) : Finaliza a conexao do Usuario
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
aCause : string : Motivo
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
---------------------------------------------------------------------------------------------------------- */
function logout( $aCause = '' ) {
if( $this -> FDebugLevel[1] )
$this -> logDebug( __METHOD__ , $aCause );
if( isset($_COOKIE[session_name()]) ) {
setcookie(session_name(), '', time()-42000, '/');
}
$_SESSION = array();
@session_unset();
@session_destroy();
}
/* -----------------=-----------=----------------------------------------------------------------------------
active(2) : Verifica se a secao esta ativa e nao expirou
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
:Boolean : Ativo ou Inativo
---------------------------------------------------------------------------------------------------------- */
function active() {
//Verificando se a sessao esta ativa
if( isset($_SESSION['user']) ) {
if( $this -> FDebugLevel[2] ) $this -> logDebug( __METHOD__ , $_SESSION );
if( $this -> FDebugLevel[2] ) $this -> logDebug( __METHOD__ , $this -> FConfig );
//Executa o logout se expirar o tempo
if( ($_SESSION['lupdate'] + $this -> FConfig['timeout']) > time() ) {
$_SESSION['lupdate'] = time();
return( true );
} else {
$this -> logout('Session timeout');
}
} else {
if( $this -> FDebugLevel[2] ) $this -> logDebug( __METHOD__ , 'not logged in' );
}
return( false );
}
function reLogin( ) { //Mantido apenas para retrocompatibilidade
return( $this -> active() );
}
/*===========================================================================================================
Funcoes Auxiliares
/* -----------------=-----------=----------------------------------------------------------------------------
query(9) : Executa uma query
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
aQuery :string : Query SQL
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
:Boolean : Sucesso ou Fracasso
---------------------------------------------------------------------------------------------------------- */
function query( $aQuery ) {
if( $this -> FDebugLevel[9] ) $this -> logDebug( __METHOD__ , $aQuery );
return( $this -> FQuery -> query( $aQuery ) );
}
/* -----------------=-----------=----------------------------------------------------------------------------
Funcao : newpassword - Cria e retorna uma senha temporaria
Entrada: aLen - Tamanho da Senha
Saida : STRING - Retorna a senha temporaria
---------------------------------------------------------------------------------------------------------- */
function newPassword($aLen = 7) {
$apass=""; //Armazena a Senha
for( $i=0; $i < $aLen; $i++){
$char=rand(48,122);
if ( ($char > 97 && $char < 122) ||
($char > 65 && $char < 90) ||
($char > 48 && $char < 57) )
$apass.=chr($char);
else
$i--;
}
return ($apass);
}
/* -----------------=-----------=----------------------------------------------------------------------------
Funcao : newpassword - Cria e retorna uma senha temporaria
Entrada: aLen - Tamanho da Senha
Saida : STRING - Retorna a senha temporaria
---------------------------------------------------------------------------------------------------------- */
function setPassword($aLen = 7) {
$apass=""; //Armazena a Senha
for( $i=0; $i < $aLen; $i++){
$char=rand(48,122);
if ( ($char > 97 && $char < 122) ||
($char > 65 && $char < 90) ||
($char > 48 && $char < 57) )
$apass.=chr($char);
else
$i--;
}
return ($apass);
}
}