<?PHP
/******************************************************
    user class

- used to keep user information
******************************************************/
require_once('classes/email.php');

class 
user{
    
    var 
$id 0;
    
    var 
$isLoggedIn false;
    var 
$isAdmin false;
    
    var 
$email '';
    var 
$preferences = array();
    
    var 
$trackedPackages = array();
    
    var 
$error '';
    
    function 
user($id 0$email ''){
        if(
$id != 0){
            
$sql "SELECT * FROM `user` WHERE `user_p` = '{$id}'";
            
$results mysql_query($sql) or die(mysql_error());
            if(
$row mysql_fetch_assoc($results)){
                
$this->email $row['user_email'];
                
$this->id $row['user_p'];

                if(
$row['preferences'] != '')
                    
$this->preferences unserialize($row['preferences']);

                if(
$row['admin'] == 1)
                    
$this->isAdmin true;
            }
        }elseif(
$email != ''){
            
$sql "SELECT * FROM `user` WHERE `user_email` = '".mysqlclean($email)."'";
            
$results mysql_query($sql) or die(mysql_error());
            if(
$row mysql_fetch_assoc($results)){
                
$this->email $row['user_email'];
                
$this->id $row['user_p'];

                if(
$row['preferences'] != '')
                    
$this->preferences unserialize($row['preferences']);

                if(
$row['admin'] == 1)
                    
$this->isAdmin true;
            }
        }
    }

    function 
login($email$password){
        
        
$sql "SELECT * FROM `user` WHERE `user_email` = '".mysqlclean($email)."'
                AND (user_password = md5('"
.mysqlclean($password)."') OR temp_password = md5('".mysqlclean($password)."') OR cookie_key = md5('".mysqlclean($password)."'))";
        
$results mysql_query($sql) or die(mysql_error());
        if(
$row mysql_fetch_assoc($results)){
            
$this->email $row['user_email'];
            
$this->id $row['user_p'];
            
            if(
$row['preferences'] != '')
                
$this->preferences unserialize($row['preferences']);
                
            if(
$row['admin'] == 1)
                
$this->isAdmin true;
            
            
$this->isLoggedIn true;
            
            if(isset(
$_SERVER['HTTP_X_FORWARDED_FOR'])){
                
$ip_address $_SERVER['HTTP_X_FORWARDED_FOR'];
            }else{
                
$ip_address $_SERVER['REMOTE_ADDR'];
            }
            
            
$sql "UPDATE `user` SET ip_address = '".$ip_address."' WHERE user_p = ".$this->id;
            
mysql_query($sql) or die(mysql_error());
            
            
            return 
true;
        }
        
        
$this->error 'Login Failed';
        return 
false;
    }
    
    function 
register($email$password){
        
// check email
        
if( !(preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/'$email)) &&
            !(
preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/',$email))){
            
$this->error 'Email address is not valid';
            return 
false;
        }
        
// check password
        
if(strlen($password) < 6){
            
$this->error 'Please pick a password that is at least 6 characters long';
            return 
false;
        }
        
// check if user exists
        
$sql "SELECT user_p FROM `user` WHERE `user_email` = '".mysqlclean($email)."'";
        
$results mysql_query($sql) or die(mysql_error());
        if(
$row mysql_fetch_assoc($results)){
            
$this->error 'This email is already registered, please try the lost password function to retrive your password';
            return 
false;
        }
        
        
// insert user
        
$sql "INSERT INTO `user` SET `user_email` = '".mysqlclean($email)."', `user_password` = md5('".mysqlclean($password)."'), _added = NOW()";
        
mysql_query($sql) or die(mysql_error());
        
        
$body "Thank you for registering at Gentoo-Portage.com";
        
        
$mail = new email();
        
$mail->Subject "Thank you for registering at Gentoo-Portage.com";
        
$mail->Body $body;
        
$mail->AltBody $body;
        
$mail->AddAddress($email);
        @
$mail->send();
        
        return 
true;
    }
    
    function 
change_password($old_pass$new_pass){
        
$sql "SELECT user_p FROM `user` WHERE `user_email` = '".mysqlclean($this->email)."'
                AND (user_password = md5('"
.mysqlclean($old_pass)."') OR temp_password = md5('".mysqlclean($old_pass)."'))";
        
$results mysql_query($sql) or die(mysql_error());
        if(
$row mysql_fetch_assoc($results)){
            
$sql "UPDATE `user` SET user_password = MD5('".mysqlclean($new_pass)."'), temp_password = '' WHERE user_p = " $row['user_p'];
            
mysql_query($sql) or die(mysql_error());
            return 
true;
        }else{
            
$this->error 'Your old password did not match our records';
            return 
false;
        }
    }
    
    function 
set_cookie(){
        
$rnd_pass $this->random_password(40);
        
$sql "UPDATE `user` SET `cookie_key` = MD5('".mysqlclean($rnd_pass)."') WHERE user_email = '".mysqlclean($this->email)."'";
        
mysql_query($sql) or die(mysql_error());
        if(
mysql_affected_rows() > 0){
            
setcookie('email'$this->emailtime() + 60*60*24*365);
            
setcookie('rememberme'$rnd_passtime() + 60*60*24*365);
        }
    }
    
    function 
lost_password($email){
        
$rnd_pass $this->random_password();
        
        
$sql "UPDATE `user` SET `temp_password` = MD5('".mysqlclean($rnd_pass)."') WHERE user_email = '".mysqlclean($email)."'";
        
mysql_query($sql) or die(mysql_error());
        if(
mysql_affected_rows() > 0){
            
$body "Your temporary password for theis account is: " $rnd_pass;
            
            
$mail = new email();
            
$mail->Subject "Your Gentoo-Portage.com password";
            
$mail->Body $body;
            
$mail->AltBody $body;
            
$mail->AddAddress($email);
            @
$mail->send();
        
            return 
true;
        }
        
        
$this->error 'Email not found, please register';
        return 
false;
    }
    
    
// update settings
    
function update($pref){
        
$sql "UPDATE `user` SET user_display = '".mysqlclean(htmlspecialchars($pref['user_display']))."' , preferences = '".mysqlclean(serialize($pref))."' WHERE user_p = ".$this->id;
         
mysql_query($sql) or die(mysql_error());
        
$this->user($this->id);
    }
    
    
// tracking
    
function isTracked($package_id){
        return 
in_array($package_id$this->get_tracked_packages());
    }
    
    function 
get_tracked_packages($forceNew false){
        if(
count($this->trackedPackages) == || $forceNew){
            
$this->trackedPackages = array();
            
            
$sql "SELECT package_f FROM user_tracking WHERE user_f = " $this->id;
            
$results mysql_query($sql) or die(mysql_error());
            while(
$row mysql_fetch_assoc($results)){
                
$this->trackedPackages[] = $row['package_f'];
            }
        }
        
        return 
$this->trackedPackages;
    }
    
    function 
track($package_id$display){
        
$sql "INSERT INTO user_tracking (`user_f`, `package_f`, `package_display`, `_added`) VALUES (".$this->id.",".intval($package_id).",'".mysqlclean($display)."',NOW())";
        
mysql_query($sql);
        
$this->get_tracked_packages(true);
    }
    
    function 
untrack($package_id){
        
$sql "DELETE FROM user_tracking WHERE user_f = ".$this->id." AND package_f = ".intval($package_id);
        
mysql_query($sql) or die(mysql_error());
        
$this->get_tracked_packages(true);
    }
    
    function 
random_password($size 8$chars "abcdefghijkmnopqrstuvwxyz0123456789"){
        
$pass '';
        
srand((double)microtime()*1000000);
        for (
$i $i <= $size $i++){
            
$pass .= substr($charsrand(0,strlen($chars) - 1), 1);
        }
        return 
$pass;
    }
    
}

?>