Location: PHPKode > projects > ProjectPress > projectpress/classes/class.User.php
<?php
if(!defined('access')) die ('You are not allowed to execute this file directly.');

 /**
 * User Class
 *
 * @package ProjectPress User Class
 * @author Joshua Parker
 * @link http://www.joshparker.us/
 */
class User {
	
	/**
     * User object
     *
     * @access public
     * @var object
     */
    public static $instance;
	
	 /**
     * Creates and references the User object.
     *
     * @access public
     * @return object User object
     */
    public static function instance() {
        if ( !self::$instance )
            self::$instance = new User();
        return self::$instance;
    }
	
	/**
     * Retrieve a user's full name.
     *
	 * @access public
     * @param string $username Grab the name of a user.
     * @return object A user's first and last name
     */
	public function get_name($username) {
	  
		$result = pmdb::connect()->query("SELECT * FROM " . DB . "members WHERE username = '".$username."'");
	  
	  		if($result->num_rows > 0) {
	  		while($r = $result->fetch_object()) {
	  			
				return $r->first_name . ' ' . $r->last_name;
			}
	  
		}
			
	}
	
	/**
     * Retrieve a user's email address.
     *
	 * @access public
     * @param string $username Grab the email of a user.
     * @return object A user's email address
     */
	public function get_email($username) {
	  
		$result = pmdb::connect()->query("SELECT email FROM " . DB . "members WHERE username = '" . $username . "'");
	  
	  		while($r = $result->fetch_object()) {
	  			
				return $r->email;
		}
			
	}
	
	/**
     * Retrieve a user's AOL AIM handle.
     *
	 * @access public
     * @param string $username Grab the aol handle of a user.
     * @return object A user's AOL AIM handle.
     */
	public function get_aim($username) {
	  
		$result = pmdb::connect()->query("SELECT aim FROM " . DB . "members WHERE username = '" . $username . "'");
	  
	  		while($r = $result->fetch_object()) {
	  			
				return $r->aim;	  
	  
	  }
			
	}
	
	/**
     * Retrieve a user's full MSN handle.
     *
	 * @access public
     * @param string $username Grab the msn handle of a user.
     * @return object A user's MSN handle.
     */
	public function get_msn($username) {
	  
		$result = pmdb::connect()->query("SELECT msn FROM " . DB . "members WHERE username = '" . $username . "'");
	  
	  		while($r = $result->fetch_object()) {
	  			
				return $r->msn;	  
	  
	  }
			
	}
	
	/**
     * Retrieve a user's Google Talk.
     *
	 * @access public
     * @param string $username Grab the gtalk handle of a user.
     * @return object A user's gTalk handle.
     */
	public function get_gtalk($username) {
	  
		$result = pmdb::connect()->query("SELECT gtalk FROM " . DB . "members WHERE username = '" . $username . "'");
	  
	  		while($r = $result->fetch_object()) {
	  			
				return $r->gtalk;	  
	  
	  }
			
	}
	
	/**
     * Retrieve a user's iChat handle.
     *
	 * @access public
     * @param string $username Grab the ichat handle of a user.
     * @return object A user's iChat handle.
     */
	public function get_ichat($username) {
	  
		$result = pmdb::connect()->query("SELECT ichat FROM " . DB . "members WHERE username = '" . $username . "'");
	  
	  		while($r = $result->fetch_object()) {
	  			
				return $r->ichat;	  
	  
	  }
			
	}
	
	/**
     * Retrieve user's first part of address.
     *
	 * @access public
     * @param string $username Grab a user's mailing address.
     * @return object A user's mailing address.
     */
	public function get_address1($username) {
		
		$result = pmdb::connect()->query("SELECT address1 FROM " . DB . "members WHERE username = '" . $username . "'");
		
			while($r = $result->fetch_object()) {
				
				return $r->address1;

			}
	
	}
	
	/**
     * Retrieve user's second part of address.
     *
	 * @access public
     * @param string $username Grab a user's second address if exist..
     * @return object A user's second address.
     */
	public function get_address2($username) {
		
		$result = pmdb::connect()->query("SELECT address2 FROM " . DB . "members WHERE username = '" . $username . "'");
		
		if($result) {
					
			while($r = $result->fetch_object()) {
				
				return $r->address2;
			
		}

			}
	
	}
	
	/**
     * Retrieve a user's City.
     *
	 * @access public
     * @param string $username Grab the username of a user.
     * @return object A user's City for address.
     */
	public function get_city($username) {
		
		$result = pmdb::connect()->query("SELECT city FROM " . DB . "members WHERE username = '" . $username . "'");
		
			while($r = $result->fetch_object()) {
				
				return $r->city;

			}
	
	}
	
	/**
     * Retrieve a user's State.
     *
	 * @access public
     * @param string $username Grab the username of a user.
     * @return object A user's state of residence.
     */
	public function get_state($username) {
		
		$result = pmdb::connect()->query("SELECT state FROM " . DB . "members WHERE username = '" . $username . "'");
		
			while($r = $result->fetch_object()) {
				
				return $r->state;

			}
	
	}
	
	/**
     * Retrieve a user's Zip Code.
     *
	 * @access public
     * @param string $username Grab the username of a user.
     * @return object A user's Zip Code.
     */
	public function get_zip($username) {
		
		$result = pmdb::connect()->query("SELECT zip FROM " . DB . "members WHERE username = '" . $username . "'");
		
			while($r = $result->fetch_object()) {
				
				return $r->zip;

			}
	
	}
	
	/**
     * Retrieve a user's Phone Number.
     *
	 * @access public
     * @param string $username Grab the username of a user.
     * @return object A user's Phone Number.
     */
	public function get_phone($username) {
		
		$result = pmdb::connect()->query("SELECT phone FROM " . DB . "members WHERE username = '" . $username . "'");
		
			while($r = $result->fetch_object()) {
				
				return $r->phone;

			}
	
	}
	
	/**
     * Retrieve a user's DOB.
     *
	 * @access public
     * @param string $username Grab the username of a user.
     * @return object A user's DOB.
     */
	public function get_dob($username) {
		
		$result = pmdb::connect()->query("SELECT dob FROM " . DB . "members WHERE username = '" . $username . "'");
		
			while($r = $result->fetch_object($result)) {
				
				return $r->dob;

			}
	
	}
	
	/**
     * Retrieve a user's Bio.
     *
	 * @access public
     * @param string $username Grab the username of a user.
     * @return object A user's Bio.
     */
	public function get_bio($username) {
		
		$result = pmdb::connect()->query("SELECT bio FROM " . DB . "members WHERE username = '" . $username . "'");
		
		if($result) {
			
			while($r = $result->fetch_object()) {
				
				return $r->bio;

			}
			
		}
	
	}
	
	/**
     * View user's profile if account is active.
     *
	 * @access public
     * @param string $username Grab the username of a user.
     * @return object A user's active profile.
     */
	public function profile_active($username) {
		
		$result = pmdb::connect()->query("SELECT * FROM " . DB . "members WHERE username = '" . $username . "' AND active = '1'");
		
			while($r = $result->fetch_object()) {
				
				return $r->active;

			}

	}
	
	/**
     * Retrieve a Members Directory.
     *
	 * @access public
     * 
     * @return A list of Members in the directory.
     */
	public function get_members_directory() {

		//check if the starting row variable was passed in the URL or not
		if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
  		//we give the value of the starting row to 0 because nothing was found in URL
  		$startrow = 0;
		//otherwise we take the value from the URL
		} else {
  		$startrow = (int)$_GET['startrow'];
		}

		$results = pmdb::connect()->query("SELECT * FROM " . DB . "members WHERE privacy = '0' AND active = '1' ORDER BY last_name ASC LIMIT " . $startrow . ",10");

			if($results) {
        	while($r = $results->fetch_object()) {
        		
        		$mem = '<tr><td>' . get_user_avatar($r->username, $r->email, 36);
        		$mem .= '</td> <td><a href="' . PM_URI . '/profile/profile.php?username=' . $r->username . '">' . $r->first_name . ' ' . $r->last_name . '</a></td> <td>' . $r->phone . '</td>';
        		$mem .= '<td>' . clickable_link($r->email) . '</td></tr>';
				
				echo $mem;
			}

			echo '<tr><td colspan="4"><a href="members.php?startrow='.($startrow+10).'">Next</a>';
			
			$prev = $startrow - 10;

			//only print a "Previous" link if a "Next" was clicked
			if ($prev >= 0) {
    		echo '&nbsp;<a href="members.php?startrow='.$prev.'">Previous</a>';
			}

    		echo '</td></tr>';
			
		}

	}

	public function get_my_groups() {
		
		$results = pmdb::connect()->query("SELECT * FROM " . DB . "group_members, " . DB . "groups, " . DB . "group_types WHERE gm_user = '" . $_GET['username'] . "' AND gp_id = grp_id AND gt_id = grpt_id");
		
		if($results->num_rows > 0) {
			
			while($r = $results->fetch_object()) {
				
				return "<tbody><tr><td><a href=" . PM_URI . '/groups/group.php?g_id=' . $r->grp_id . ">" . $r->group_name . "</a></td> <td>" . $r->group_type . "</td> <td>" . $r->gender . "</td></tr></tbody>";

			}
		}
		
	}
	
	public function get_private_message() {
 	
 		$gpm = pmdb::connect()->query("SELECT * FROM " . DB . "messages WHERE receiver = '" . $_SESSION['username'] . "' LIMIT 4");

			while($r = $gpm->fetch_array()) {
		
			echo '<tr><td>' . get_user_avatar($r['sender'],$r['email'],25) .  '&nbsp;&nbsp;<a href="'.PM_URI.'/pms/viewmsg.php?msg_id=' . $r['id'] . '">' . $r['subject'] . '</a> <br /><span>from ' . get_name($r['sender']) . ', sent @ '.date('M j, g:i a', strtotime($r['date'])).'</span></td></tr>';
		}
 	
 }

	public function get_user_info($username,$field) {
       $result = pmdb::connect()->query("SELECT " . $field . " FROM " . DB . "members WHERE username = '" . $username . "'");
       		while($r = $result->fetch_object()) {
               $info = $r->$field;
       		return $info;
       }
	}

	public function checkUser($uid, $oauth_provider, $username,$email,$twitter_otoken,$twitter_otoken_secret) {
        $query = pmdb::connect()->query("SELECT * FROM " . DB . "members WHERE oauth_uid = '$uid' and oauth_provider = '$oauth_provider'");
        $result = $query->fetch_array();
        if (!empty($result)) {
            # User is already present
        } else {
        	$username = strtolower(str_replace(" ", "", $username));
            #user not present. Insert a new Record
            $query = pmdb::connect()->query("INSERT INTO " . DB . "members (user_id,oauth_provider, oauth_uid, username,email,active,twitter_oauth_token,twitter_oauth_token_secret,date) VALUES ('$uid', '$oauth_provider', '$uid', '$username','$email','1','$twitter_otoken','$twitter_otoken_secret',NOW())");
            $query = pmdb::connect()->query("SELECT * FROM " . DB . "members WHERE oauth_uid = '$uid' and oauth_provider = '$oauth_provider'");
            $result = $query->fetch_array();
			pmdb::connect()->query("INSERT INTO " . DB . "user_roles (userID,roleID,addDate) VALUES ('$uid','2',NOW())");
			$_SESSION['username'] = $result['username']; // Sets the username session.
			$_SESSION['userID'] = $result['oauth_uid'];
            return $result;
        }
        return $result;
    }
		
}
Return current item: ProjectPress