Ver Mensaje Individual
  #10 (permalink)  
Antiguo 22/11/2009, 21:53
SeNdEr2003
 
Fecha de Ingreso: agosto-2003
Mensajes: 906
Antigüedad: 20 años, 8 meses
Puntos: 3
Respuesta: Sistema de usuarios no loguea

ah este es el access.class.php

Código PHP:
<?php
class flexibleAccess{
var 
$dbName 'db';
var 
$dbHost 'localhost';
var 
$dbPort port;
var 
$dbUser 'usr';
var 
$dbPass 'pass';
var 
$dbTable  'table';
var 
$sessionVariable 'userSessionValue';
var 
$tbFields = array(
'userID'=> 'id'
'login' => 'usuario',
'email' => 'email',
'pass'  => 'clave',
'active'=> 'activacion'
);
var 
$remTime 2592000;
var 
$remCookieName 'ckSavePass';
var 
$remCookieDomain '';
var 
$passMethod 'sha1';
var 
$displayErrors true;
var 
$userID;
var 
$dbConn;
var 
$userData=array();
function 
flexibleAccess($dbConn ''$settings '')
{
if ( 
is_array($settings) ){
foreach ( 
$settings as $k => $v ){
if ( !isset( 
$this->{$k} ) ) die('Property '.$k.' does not exists. Check your settings.');
$this->{$k} = $v;
}
}
$this->remCookieDomain $this->remCookieDomain == '' $_SERVER['HTTP_HOST'] : $this->remCookieDomain;
$this->dbConn = ($dbConn=='')? mysql_connect($this->dbHost.':'.$this->dbPort$this->dbUser$this->dbPass):$dbConn;
if ( !
$this->dbConn ) die(mysql_error($this->dbConn));
mysql_select_db($this->dbName$this->dbConn)or die(mysql_error($this->dbConn));
if( isset( 
$_SESSION ) ) session_start();
if ( !empty(
$_SESSION[$this->sessionVariable]) )
{
$this->loadUser$_SESSION[$this->sessionVariable] );
}
if ( isset(
$_COOKIE[$this->remCookieName]) && $_COOKIE[$this->remCookieName]!="0" && !$this->is_loaded()){
$u unserialize(base64_decode($_COOKIE[$this->remCookieName]));
$this->login($u['uname'], $u['password']);
}
}
function 
login($uname$password$remember false$loadUser true)
{
$uname    $this->escape($uname);
$password $originalPassword $this->escape($password);
switch(
strtolower($this->passMethod)){
case 
'sha1':
$password "SHA1('$password')"; break;
case 
'md5' :
$password "MD5('$password')";break;
case 
'nothing':
$password "'$password'";
}
$res $this->query("SELECT * FROM `{$this->dbTable}` 
WHERE `{$this->tbFields['login']}` = '$uname' AND `{$this->tbFields['pass']}` = $password LIMIT 1"
,__LINE__);
if ( @
mysql_num_rows($res) == 0)
return 
false;
if ( 
$loadUser )
{
$this->userData mysql_fetch_array($res);
$this->userID $this->userData[$this->tbFields['userID']];
$_SESSION[$this->sessionVariable] = $this->userID;
if ( 
$remember ){
$cookie base64_encode(serialize(array('uname'=>$uname,'password'=>$originalPassword)));
$a setcookie($this->remCookieName$cookie,time()+$this->remTime'/');
}
}
return 
true;
}
function 
logout($redirectTo '')
{
setcookie($this->remCookieName"0"time()-86400'/');
unset(
$_SESSION[$this->sessionVariable],$_COOKIE[$this->remCookieName],$this->userID);
$this->userData '';
if ( 
$redirectTo != '' && !headers_sent()){
header('Location: '.$redirectTo );
exit;
}
}
function 
is($prop){
return 
$this->get_property($prop)==1?true:false;
}
function 
get_property($property)
{
//if (empty($this->userID)) $this->error('No user is loaded', __LINE__);
//if (!isset($this->userData[$property])) $this->error('Unknown property <b>'.$property.'</b>', __LINE__);
return $this->userData[$property];
}
function 
is_active()
{
return 
$this->userData[$this->tbFields['active']];
}
function 
is_loaded()
{
return empty(
$this->userID) ? false true;
}
function 
activate()
{
if (empty(
$this->userID)) $this->error('No user is loaded'__LINE__);
if ( 
$this->is_active()) $this->error('Allready active account'__LINE__);
$res $this->query("UPDATE `{$this->dbTable}` SET {$this->tbFields['active']} = 1 
WHERE `{$this->tbFields['userID']}` = '"
.$this->escape($this->userID)."' LIMIT 1");
if (@
mysql_affected_rows() == 1)
{
$this->userData[$this->tbFields['active']] = true;
return 
true;
}
return 
false;
}
function 
insertUser($data){
if (!
is_array($data)) $this->error('Data is not an array'__LINE__);
switch(
strtolower($this->passMethod)){
case 
'sha1':
$password "SHA1('".$data[$this->tbFields['pass']]."')"; break;
case 
'md5' :
$password "MD5('".$data[$this->tbFields['pass']]."')";break;
case 
'nothing':
$password $data[$this->tbFields['pass']];
}
foreach (
$data as $k => $v $data[$k] = "'".$this->escape($v)."'";
$data[$this->tbFields['pass']] = $password;
$this->query("INSERT INTO `{$this->dbTable}` (`".implode('`, `'array_keys($data))."`) VALUES (".implode(", "$data).")");
return (int)
mysql_insert_id($this->dbConn);
}
function 
randomPass($length=10$chrs '23456789qwertyupasdfghjkzxcvbnm'){
for(
$i 0$i $length$i++) {
$pwd .= $chrs{mt_rand(0strlen($chrs)-1)};
}
return 
$pwd;
}
function 
query($sql$line 'Uknown')
{
$res mysql_db_query($this->dbName$sql$this->dbConn);
if ( !
res )
$this->error(mysql_error($this->dbConn), $line);
return 
$res;
}
function 
loadUser($userID)
{
$res $this->query("SELECT * FROM `{$this->dbTable}` WHERE `{$this->tbFields['userID']}` = '".$this->escape($userID)."' LIMIT 1");
if ( 
mysql_num_rows($res) == )
return 
false;
$this->userData mysql_fetch_array($res);
$this->userID $userID;
$_SESSION[$this->sessionVariable] = $this->userID;
return 
true;
}
function 
escape($str) {
$str get_magic_quotes_gpc()?stripslashes($str):$str;
$str mysql_real_escape_string($str$this->dbConn);
return 
$str;
}
function 
error($error$line ''$die false) {
if ( 
$this->displayErrors )
echo 
'<b>Error: </b>'.$error.'<br /><b>Line: </b>'.($line==''?'Unknown':$line).'<br />';
if (
$die) exit;
return 
false;
}
}
?>