Subversion Repositories cheapmusic

Rev

Rev 57 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
// Get current page file name
$pageFile = basename($_SERVER['PHP_SELF']);

// Include config file && User class
require_once 'config.php';
require_once 'User.class.php';

// Check whether user ID is available in cookie and cookie hash matches
if (isset($_COOKIE['rememberUserId']) && !empty($rememberUserId)) {
    require_once 'includes/password.php';
    $user = new User();
    $conditions['where'] = array(
        'id' => $_COOKIE['rememberUserId'],
    );
    $conditions['return_type'] = 'single';
    $userData = $user->getRows($conditions);
    if (!empty($userData) && password_verify($userData['password'] . $userData['id'], $_COOKIE['hash'])) {
        $_SESSION['sessData']['userLoggedIn'] = true;
        $_SESSION['sessData']['userID'] = $rememberUserId;
    }
}

// Get session data
$sessData = !empty($_SESSION['sessData']) ? $_SESSION['sessData'] : array();

// Redirect to homepage if user not logged in
$userLoggedIn = (!empty($sessData['userLoggedIn']) && !empty($sessData['userID'])) ? true : false;

switch ($pageFile) {
    case 'account.php':
    case 'editAccount.php':
    case 'changePassword.php':
        if ($userLoggedIn) {
            $user = new User();
            $conditions['where'] = array(
                'id' => $sessData['userID'],
            );
            $conditions['return_type'] = 'single';
            $userData = $user->getRows($conditions);

            $httpPos = strpos($userData['picture'], 'http');
            if ($httpPos === false) {
                $userPicture = !empty($userData['picture']) ? UPLOAD_URL . 'profile_picture/' . $userData['picture'] : PUBLIC_URL . 'images/default.png';
            }
            else {
                $userPicture = $userData['picture'];
            }
            $userName = $userData['first_name'] . ' ' . $userData['last_name'];
            $userTheme = $userData['theme'];
        }
        else {
            MySessionHandler::commit(session_id());
            header("Location: index.php");
            exit();
        }
    break;
    case 'forgotPassword.php':
    case 'registration.php':
    case 'resetPassword.php':
        if ($userLoggedIn) {
            MySessionHandler::commit(session_id());
            header("Location: account.php");
            exit();
        }
    break;
    case 'index.php':
        if ($userLoggedIn) {
            MySessionHandler::commit(session_id());
            header("Location: account.php");
            exit();
        }
        else {
            // Include social login handler
            require_once 'socialLogin.php';
        }
        // fall through
        
    default:
        $userData = array();
}

// Get status message from session
if (!empty($sessData['status']['msg'])) {
    $statusMsg = $sessData['status']['msg'];
    $statusMsgType = $sessData['status']['type'];
    unset($_SESSION['sessData']['status']);
}

MySessionHandler::commit(session_id());