<?php
/*
Plugin Name: Website Membership
Description: Website membership functions for user sign-up, password reminder, login, user specific content, and login only content
Version: 1.04
Requires at least: 2.08
*/

// UPDATE THESE VALUES

$GLOBALS['WEBSITE_LOGIN_LOGIN_FORM_URL']  = 'login/user/user-login.php';             // url to login form
$GLOBALS['WEBSITE_LOGIN_SIGNUP_URL']      = 'login/user/user-signup.php';            // signup url linked to from the login page
$GLOBALS['WEBSITE_LOGIN_REMINDER_URL']    = 'login/user/user-password-request.php';   // password reminder url linked to from the login page
$GLOBALS['WEBSITE_LOGIN_RESET_URL']       = '/claims/login/user/user-password-reset.php';    // password reminder url linked to from the login page
$GLOBALS['WEBSITE_LOGIN_PROFILE_URL']     = 'login/user/user-profile.php';           // url to "edit my profile" page

$GLOBALS['WEBSITE_LOGIN_POST_LOGIN_URL']  = '';                 // user gets redirected to last page that called websiteLogin_redirectToLogin() or here if none
$GLOBALS['WEBSITE_LOGIN_POST_LOGOFF_URL'] = '';    // user gets redirected to last page that called websiteLogin_redirectToLogin() or here if none


// DON'T UPDATE ANYTHING BELOW THIS LINE

$GLOBALS['WEBSITE_MEMBERSHIP_PLUGIN']  = true;
if (!defined('START_SESSION')) { define('START_SESSION', true); }
addAction('viewer_postinit', '_websiteLogin_init', null, 0);

//
function _websiteLogin_init() {
  if (defined('IS_CMS_ADMIN')) { return; } // only run this form website viewers, not CMS admin pages
  require_once SCRIPT_DIR . "/lib/login_functions.php";

  // load current user
  $GLOBALS['CURRENT_USER'] = _websiteLogin_getCurrentUser(); //

  // perform website login actions
  if (@$_REQUEST['action'] == 'logoff')               { _websiteLogin_logoff(); }
  if (@$_REQUEST['action'] == 'login')                { _websiteLogin_login(); }

}



// if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); }
function websiteLogin_redirectToLogin($returnAfterLogin = true) {

  // remember page they're trying to access
  if ($returnAfterLogin) { setPrefixedCookie('lastUrl', thisPageUrl()); }
  // redirect to login
  redirectBrowserToURL("{$GLOBALS['WEBSITE_LOGIN_LOGIN_FORM_URL']}?loginRequired=1");
  exit;
}


//
function websiteLogin_setLoginTo($username, $password) {
  user_createLoginSession($username, $password);
}


// load user from database with cookie login details
function _websiteLogin_getCurrentUser() {

  // load current user
  $user = user_loadWithSession();
  if (!$user) { user_eraseLoginSession(); } // remove login session on invalid login

  // error checking - logoff expired and disabled users
  if (@$user['disabled'])      { alert(t("Your account has been disabled.")); }
//if (@$user['isExpired'])     { alert(t("Your account has expired.")); } // future: maybe we should add an expires url where user gets redirect when their account has expired? For subscription renewal
  if (alert()) {
    user_eraseLoginSession();
    return false;
  }

  //
  return $user;
}


//
function _websiteLogin_login() {
  global $CURRENT_USER;

  // create login session
  user_createLoginSession(@$_REQUEST['username'], @$_REQUEST['password']);

  // check for valid login
  $CURRENT_USER = _websiteLogin_getCurrentUser();
  if (alert()) { return; }

  // error checking
  if      (!@$_REQUEST['username']) { alert("Please enter a username!<br/>\n"); }
  else if (!@$_REQUEST['password']) { alert("Please enter a password!<br/>\n"); }
  else if (!$CURRENT_USER)          { alert("Invalid username or password!<br/>\n"); }
  if (function_exists('wsms_login_errorchecking')) { alert(wsms_login_errorchecking($CURRENT_USER)); }
  if (alert()) {
    //  $CURRENT_USER = false;
    return;
  }

  // clear form values
  $_REQUEST['username'] = '';
  $_REQUEST['password'] = '';


  // redirect on success
  if      ($lastUrl = getPrefixedCookie('lastUrl'))  { $postLoginUrl = $lastUrl; }
  //else if (@$_SERVER['HTTP_REFERER'])                { $postLoginUrl = $_SERVER['HTTP_REFERER']; }  //This will always be the user-login page.
  else if ($GLOBALS['WEBSITE_LOGIN_POST_LOGIN_URL']) { $postLoginUrl = $GLOBALS['WEBSITE_LOGIN_POST_LOGIN_URL']; }
  else                                               { $postLoginUrl = thisPageUrl(); }

  removePrefixedCookie('lastUrl');
  redirectBrowserToURL($postLoginUrl);
  exit;

}


// remove login cookies
function _websiteLogin_logoff() {

  // get logoff url
  if ($GLOBALS['WEBSITE_LOGIN_POST_LOGOFF_URL']) {
    $logoffUrl = $GLOBALS['WEBSITE_LOGIN_POST_LOGOFF_URL'];
  }
  else {
    $logoffUrl = thisPageUrl();
    $logoffUrl = preg_replace('/\baction=logoff\b/', '', $logoffUrl); // prevent redirect loop
  }

  // logoff and redirect
  user_logoff($logoffUrl);
  exit;
}

//
function websiteLogin_pluginDir() {
  return dirname(__FILE__);
}


?>
