webmaster
Here's some code that might help
Basically the code
1. Receives the username and email stored in a $_SESSION variable (this would be passed in via WHMCS / cPanel)
2. Check if the already user exists, if so log them in
3. Otherwise create a new user
<?php
// Machform
require('includes/init.php');
require('config.php');
require('includes/db-core.php');
require('includes/helper-functions.php');
//use adLDAP\adLDAP,adLDAP\adLDAPException;
require('lib/password-hash.php');
//require_once('lib/adLDAP/adLDAP.php');
$ssl_suffix = mf_get_ssl_suffix();
$dbh = mf_connect_db();
if($_SESSION['user_email'] && $_SESSION['machform_logout'] == 0){
$user_email = $_SESSION['user_email'];
$user_fullname = $_SESSION['full_name'];
//Check Wordpress User Exists in Viviki
$row = checkVivikiUserExists($dbh, $user_email);
//if user does not exist create them
if(empty($row)) {
createVivikiUser($dbh);
}
//log User In
logExternalUserIn($dbh, $row);
$machform_redirect = "http{$ssl_suffix}://".$_SERVER['HTTP_HOST'].mf_get_dirname($_SERVER['PHP_SELF'])."/manage_forms.php";
print_r('User Logged In <br />');
print_r("<a href='{$machform_redirect}'>{$machform_redirect}</a>");
//redirect
header("Location: ". $machform_redirect);
}
else{
$_SESSION['mf_logged_in'] = false;
add_action('wp_logout', 'machformEndSession');
echo "Opps, Something went wrong. Contact you're webmaster.";
}
function createUser($dbh){
//create local account using info from External Application
$priv_administer = 0;
$priv_new_forms = 1;
$priv_new_themes = 1;
$query = "INSERT INTO
`".MF_TABLE_PREFIX."users`(
`user_email`,
`user_password`,
`user_fullname`,
`priv_administer`,
`priv_new_forms`,
`priv_new_themes`,
`status`)
VALUES (?, ?, ?, ?, ?, ?, ?);";
$params = array(
$_SESSION['user_email'],
'',
$_SESSION['full_name'],
$priv_administer,
$priv_new_forms,
$priv_new_themes,
1);
mf_do_query($query,$params,$dbh);
$user_id = (int) $dbh->lastInsertId();
return $user_id;
}
//Check User Exists
function checkUserExists($dbh, $user_email){
//check if the user from the external application already exists or not
//if not exist, create the account
$query = "SELECT
`user_password`,
`user_id`,
`priv_administer`,
`priv_new_forms`,
`priv_new_themes`,
`tsv_enable`,
`tsv_secret`,
`login_attempt_date`,
`login_attempt_count`
FROM
`".MF_TABLE_PREFIX."users`
WHERE
`user_email`=? and `status`=1";
$params = array($user_email);
$sth = mf_do_query($query,$params,$dbh);
$row = mf_do_fetch_result($sth);
return $row;
}
// Check Load External User
function logExternalUserIn($dbh, $row){
//load existing user data
$stored_password_hash = $row['user_password'];
$user_id = $row['user_id'];
$priv_administer = (int) $row['priv_administer'];
$priv_new_forms = (int) $row['priv_new_forms'];
$priv_new_themes = (int) $row['priv_new_themes'];
$tsv_enable = (int) $row['tsv_enable'];
$tsv_secret = $row['tsv_secret'];
$login_attempt_date = $row['login_attempt_date'];
$login_attempt_count = $row['login_attempt_count'];
//reset login counter
$query = "UPDATE ".MF_TABLE_PREFIX."users
SET
login_attempt_date = NULL,
login_attempt_count = 0
WHERE
user_id = ?";
$params = array($user_id);
mf_do_query($query,$params,$dbh);
//regenerate session id for protection against session fixation
session_regenerate_id();
//set the session variables for the user=========
$_SESSION['mf_logged_in'] = true;
$_SESSION['mf_user_id'] = $user_id;
$_SESSION['mf_user_privileges']['priv_administer'] = $priv_administer;
$_SESSION['mf_user_privileges']['priv_new_forms'] = $priv_new_forms;
$_SESSION['mf_user_privileges']['priv_new_themes'] = $priv_new_themes;
//===============================================
//update last_login_date and last_ip_address
$last_login_date = date("Y-m-d H:i:s");
$last_ip_address = $_SERVER['REMOTE_ADDR'];
$query = "UPDATE ".MF_TABLE_PREFIX."users set last_login_date=?,last_ip_address=? WHERE `user_id`=?";
$params = array($last_login_date,$last_ip_address,$user_id);
mf_do_query($query,$params,$dbh);
return $user_id;
}
function updateUserName($user_fullname,$user_email){
//update user fullname from LDAP into local users table
$query = "UPDATE ".MF_TABLE_PREFIX."users
SET user_fullname = ?
WHERE `user_email`=? and `status`=1";
$params = array($user_fullname,$user_email);
mf_do_query($query,$params,$dbh);
}
?>