Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```
- <?php
- class Member {
- /*
- * @Member class: This is the base account type and very other account type should inherit from this
- * @author: Al Nmeri
- * ==== @constructor ====
- * @param: a unique username to init user for db indexing.
- * @returns: attaches user info to properties
- **/
- public $name;
- public $pagename;
- public $can_comment;
- public $profile_pic;
- public $username;
- public $bio;
- public $account_type;
- public function __construct($name) {
- $this->name = str_replace('_', ' ', ucwords(strtolower($name)));
- $this->pagename = "http://site.com/". strtolower(str_replace('_', '.', $name)) . '/';
- $this->can_comment = true;
- $this->profile_pic = "http://site.com/user.png";
- $this->username = $name;
- $this->bio = "";
- $this->account_type = 'member';
- }
- /*
- * @param: PDO conn object
- * @returns: returns true on successful insert of all user properties into db
- **/
- public function activate (PDO $conn) {
- $active = $conn->prepare("INSERT INTO user_info (name, username, pagename, can_comment, profile_pic, bio, account_type) VALUES (:name, :username, :pagename, :can_comment, :profile_pic, :bio, :account_type)");
- $active->execute((array) $this);
- return true;
- }
- /*
- * @description: For settings page. To fetch all alterable user properties
- * @param: PDO conn object
- * @param: joiner: the variable that join the tables
- * @returns: returns an object containing current user properties for sitewide usage
- **/
- public static function fetch_me (PDO $conn, $joiner) {
- $prep = $conn->prepare("SELECT users.username, users.email, users.password, user_info.name, user_info.profile_pic, user_info.bio, user_info.pagename FROM users INNER JOIN user_info ON users.username=user_info.username WHERE users.username=?"); //ON is the joiner between the tables like WHERE is for values
- $prep->execute([filter_var($joiner, FILTER_SANITIZE_STRING)]);
- return $prep->fetch(PDO::FETCH_OBJ);
- }
- /*
- * @param: $old_pass: old password
- * @param: $new_pass: new password;
- * @param: $conn: PDO connection
- * @description: changes user password
- * @returns: false on wrong password or failure
- **/
- public static function change_password($old_pass, $new_pass, $conn) {
- $new_pass = password_hash($new_pass, PASSWORD_BCRYPT);
- $sess = $_SESSION['username'];
- if (password_verify($old_pass, SELF::fetch_me($conn, $sess)->password)) {
- $change_pass_query = $conn->prepare("UPDATE users SET password=? WHERE username=?")->execute([$new_pass, $sess]);
- }
- else return false;
- }
- /*
- * @param: $pass: user password
- * @param: $new_email: $email to change to
- * @param: $conn: PDO connection
- * @description: changes user email
- * @returns: false on wrong password or failure
- **/
- public static function change_email($pass, $new_email, $conn) {
- $sess = $_SESSION['username'];
- $new_email = filter_var($new_email, FILTER_SANITIZE_EMAIL);
- if (password_verify($pass, SELF::fetch_me($conn, $sess)->password)) {
- //test if email is already in use by someone else
- $email_in_use = $conn->prepare("SELECT COUNT(*) FROM users WHERE email=?")->execute([$new_email]);
- if ($email_in_use->fetchColumn() == "1") {
- return "<span style='color: red; padding-left:5%;' id='errorText'> sorry. email in use by another user </span>";
- }
- //else update db with new email
- else {
- $change_email_query = $conn->prepare("UPDATE users SET email=? WHERE username= ?")->execute([$new_pass, $sess]);
- }
- }
- else return "Changes not saved: wrong password.";
- }
- /*
- * @param: valid PDO connection
- * @param: Session variable
- * @description: updates user's last login
- * @return: null
- **/
- public static function set_last_login($conn, $sess) {
- $update = $conn->query("UPDATE users SET last_login=NOW() WHERE username=?")->execute([$sess]);
- }
- public function bio($bio, $conn) {
- $sess = $_SESSION['username'];
- $update = $conn->prepare("UPDATE user_info SET bio=? WHERE username=?")->execute([htmlspecialchars($bio), $sess]);
- }
- /*
- * @function: A one time function to check if user has changed default display picture and updates db if so
- * @param: valid PDO connection
- **/
- public static function change_picture($conn){
- $sess = $_SESSION['username'];
- // Check if user has previously uploaded profile picture
- if (SELF::fetch_me($conn, $sess)->profile_pic == "../user.png") {
- $new_image_link = $_SERVER["DOCUMENT_ROOT"].'/a/profile_images/'. SELF::fetch_me($conn, $sess)->username. '.png';
- $dp = $conn->prepare("UPDATE user_info SET profile_pic=?")->execute([$new_image_link]);
- }
- }
- /*@function: In case of upgrade, use overloading to add new features **/
- public function __set ($name, $method){
- return $this->name = $method;
- }
- }
- ?>
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement