Created
June 28, 2016 00:54
-
-
Save ano/62cdaad395b85a28e808b13fe4acce4c to your computer and use it in GitHub Desktop.
Google Single Sign On into Machform with Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* CUSTOM_CODE Machform Integration code. | |
* | |
* @since Twenty Fifteen 1.0 | |
*/ | |
add_action('init', 'machformGetUser', 1); | |
add_action('wp_logout', 'machformEndSession'); | |
add_action('wp_login', 'machformStartSession', 10, 2); | |
function machformGetUser() { | |
if(!session_id()) { | |
session_start(); | |
} | |
} | |
function machformEndSession() { | |
session_destroy(); | |
} | |
function machformStartSession($user_login, $user){ | |
$_SESSION['user_email'] = $user->user_email; | |
$_SESSION['user_logged_in'] = $user_login; | |
$_SESSION['full_name'] = $user->display_name; | |
$_SESSION['machform_logout']= 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
PSUEDO CODE | |
----------- | |
Check wordpress user is logged in and get their email address | |
Check viviki user exists | |
if user exists | |
log them in | |
else | |
create user | |
logged in | |
*/ | |
session_start(); | |
/* Wordpress */ | |
$wordpress_relpath = '../dashboard'; | |
define('WP_USE_THEMES', false); | |
require("{$wordpress_relpath}/wp-blog-header.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); | |
var_dump($row); | |
} | |
//log Wordpress User In | |
logWordpressUserIn($dbh, $row); | |
print_r('User Logged In'); | |
//redirect | |
header("Location: http{$ssl_suffix}://".$_SERVER['HTTP_HOST'].mf_get_dirname($_SERVER['PHP_SELF'])."/index.php"); | |
} | |
else{ | |
$_SESSION['mf_logged_in'] = false; | |
add_action('wp_logout', 'machformEndSession'); | |
echo "Opps, Something went wrong. Contact Ano Tisam at FFA."; | |
} | |
function createVivikiUser($dbh){ | |
//create local account using info from LDAP | |
$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 Viviki User Exists | |
*/ | |
function checkVivikiUserExists($dbh, $user_email){ | |
//if user authenticated within Wordpress, check if the local user account already exist 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 Viviki User | |
*/ | |
function logWordpressUserIn($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); | |
} | |
/** | |
* Login Viviki User | |
*/ | |
function loginVivikiUser($user_email){ | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
/* Wordpress */ | |
$wordpress_relpath = '../dashboard'; | |
define('WP_USE_THEMES', false); | |
require("{$wordpress_relpath}/wp-blog-header.php"); | |
wp_logout(); | |
session_destroy(); | |
echo "Wordpress is now logged out! <a href='../wordpress/'>here</a> <br />"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment