";
return $html;
}
function mo_openid_decrypt_sanitize($param) {
if(strcmp($param,'null')!=0 && strcmp($param,'')!=0){
$customer_token = get_option('mo_openid_customer_token');
$decrypted_token = decrypt_data($param,$customer_token);
// removes control characters and some blank characters
$decrypted_token_sanitise = preg_replace('/[\x00-\x1F][\x7F][\x81][\x8D][\x8F][\x90][\x9D][\xA0][\xAD]/', '', $decrypted_token);
//strips space,tab,newline,carriage return,NUL-byte,vertical tab.
return trim($decrypted_token_sanitise);
}else{
return '';
}
}
function mo_openid_link_account( $username, $user ){
if($user){
$userid = $user->ID;
}
mo_openid_start_session();
$user_email = isset($_SESSION['user_email']) ? sanitize_text_field($_SESSION['user_email']):'';
$social_app_identifier = isset($_SESSION['social_user_id']) ? sanitize_text_field($_SESSION['social_user_id']):'';
$social_app_name = isset($_SESSION['social_app_name']) ? sanitize_text_field($_SESSION['social_app_name']):'';
//if user is coming through default wordpress login, do not proceed further and return
if(isset($userid) && empty($social_app_identifier) && empty($social_app_name) ) {
return;
}
elseif(!isset($userid)){
return;
//wp_die('No user is returned.');
}
global $wpdb;
$db_prefix = $wpdb->prefix;
$linked_email_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM ".$db_prefix."mo_openid_linked_user where linked_email = \"%s\" AND linked_social_app = \"%s\"",$user_email,$social_app_name));
// if a user with given email and social app name doesn't already exist in the mo_openid_linked_user table
if(!isset($linked_email_id)){
mo_openid_insert_query($social_app_name,$user_email,$userid,$social_app_identifier);
}
}
function mo_openid_insert_query($social_app_name,$user_email,$userid,$social_app_identifier){
// check if none of the column values are empty
if(!empty($social_app_name) && !empty($user_email) && !empty($userid) && !empty($social_app_identifier)){
date_default_timezone_set('Asia/Kolkata');
$date = date('Y-m-d H:i:s');
global $wpdb;
$db_prefix = $wpdb->prefix;
$table_name = $db_prefix. 'mo_openid_linked_user';
$result = $wpdb->insert(
$table_name,
array(
'linked_social_app' => $social_app_name,
'linked_email' => $user_email,
'user_id' => $userid,
'identifier' => $social_app_identifier,
'timestamp' => $date,
),
array(
'%s',
'%s',
'%d',
'%s',
'%s'
)
);
if($result === false){
wp_die('Error in insert query');
$wpdb->show_errors();
$wpdb->print_error();
exit;
}
}
}
function mo_openid_send_email($user_id='', $user_url=''){
if( get_option('mo_openid_email_enable') == 1) {
global $wpdb;
$admin_mail = get_option('mo_openid_admin_email');
$user_name = ($user_id == '') ? "##UserName##" : ($wpdb->get_var($wpdb->prepare("SELECT user_login FROM {$wpdb->users} WHERE ID = %d", $user_id)));
$content = get_option('mo_openid_register_email_message');
$subject = "[" . get_bloginfo('name') . "] New User Registration - Social Login";
$content = str_replace('##User Name##', $user_name, $content);
$headers = "Content-Type: text/html";
wp_mail($admin_mail, $subject, $content, $headers);
}
}
function mo_openid_disabled_register_message() {
$message = get_option('mo_openid_register_disabled_message').' Go to Home Page';
wp_die($message);
}
function mo_openid_get_redirect_url() {
$current_url = isset($_COOKIE["redirect_current_url"]) ? $_COOKIE["redirect_current_url"]:'';
$pos = strpos($_SERVER['REQUEST_URI'], '/openidcallback');
if ($pos === false) {
$url = str_replace('?option=moopenid','',$_SERVER['REQUEST_URI']);
$current_url = str_replace('?option=moopenid','',$current_url);
} else {
$temp_array1 = explode('/openidcallback',$_SERVER['REQUEST_URI']);
$url = $temp_array1[0];
$temp_array2 = explode('/openidcallback',$current_url);
$current_url = $temp_array2[0];
}
$option = get_option( 'mo_openid_login_redirect' );
$redirect_url = site_url();
if( $option == 'same' ) {
if(!is_null($current_url)){
if(strpos($current_url,get_option('siteurl').'/wp-login.php')!== false)
{
$redirect_url=get_option('siteurl');
}
else
$redirect_url = $current_url;
}
else{
if(isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off'){
$http = "https://";
} else {
$http = "http://";
}
$redirect_url = urldecode(html_entity_decode(esc_url($http . $_SERVER["HTTP_HOST"] . $url)));
if(html_entity_decode(esc_url(remove_query_arg('ss_message', $redirect_url))) == wp_login_url() || strpos($_SERVER['REQUEST_URI'],'wp-login.php') !== FALSE || strpos($_SERVER['REQUEST_URI'],'wp-admin') !== FALSE){
$redirect_url = site_url().'/';
}
}
} else if( $option == 'homepage' ) {
$redirect_url = site_url();
} else if( $option == 'dashboard' ) {
$redirect_url = admin_url();
} else if( $option == 'custom' ) {
$redirect_url = get_option('mo_openid_login_redirect_url');
}else if($option == 'relative') {
$redirect_url = site_url() . (null !== get_option('mo_openid_relative_login_redirect_url')?get_option('mo_openid_relative_login_redirect_url'):'');
}
if(strpos($redirect_url,'?') !== FALSE) {
$redirect_url .= get_option('mo_openid_auto_register_enable') ? '' : '&autoregister=false';
} else{
$redirect_url .= get_option('mo_openid_auto_register_enable') ? '' : '?autoregister=false';
}
return $redirect_url;
}
function mo_openid_redirect_after_logout($logout_url) {
if(get_option('mo_openid_logout_redirection_enable')){
$logout_redirect_option = get_option( 'mo_openid_logout_redirect' );
$redirect_url = site_url();
if( $logout_redirect_option == 'homepage' ) {
$redirect_url = $logout_url . '&redirect_to=' .home_url() ;
}
else if($logout_redirect_option == 'currentpage'){
if(isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off'){
$http = "https://";
} else {
$http = "http://";
}
$redirect_url = $logout_url . '&redirect_to=' . $http . $_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI'];
}
else if($logout_redirect_option == 'login') {
$redirect_url = $logout_url . '&redirect_to=' . site_url() . '/wp-admin' ;
}
else if($logout_redirect_option == 'custom') {
$redirect_url = $logout_url . '&redirect_to=' . site_url() . (null !== get_option('mo_openid_logout_redirect_url')?get_option('mo_openid_logout_redirect_url'):'');
}
return $redirect_url;
}else{
return $logout_url;
}
}
function mo_openid_login_redirect($username = '', $user = NULL){
mo_openid_start_session();
if(is_string($username) && $username && is_object($user) && !empty($user->ID) && ($user_id = $user->ID) && isset($_SESSION['mo_login']) && $_SESSION['mo_login']){
$_SESSION['mo_login'] = false;
wp_set_auth_cookie( $user_id, true );
if($_SESSION['registered_user'] =='1' && get_option("mo_openid_registration_redirection_enable")=='1')
{
$redirect_url =get_option("mo_openid_relative_register_redirect_url");
}
else{
$redirect_url = mo_openid_get_redirect_url();
}
wp_redirect($redirect_url);
exit;
}
}
function send_otp_token($email){
$otp = wp_rand(1000,99999);
$customerKey = get_option('mo_openid_admin_customer_key');
$stringToHash = $customerKey . $otp;
$transactionId = hash("sha512", $stringToHash);
//wp_email function will come here
$subject= '['.get_bloginfo('name').'] Verify your email';
$message=str_replace('##otp##', $otp, get_option('custom_otp_msg'));
$response = wp_mail($email, $subject,$message);
if($response){
mo_openid_start_session();
$_SESSION['mo_otptoken'] = true;
$_SESSION['sent_on'] = time();
$content = array('status' => 'SUCCESS','tId' => $transactionId);
}
else
$content = array('status' => 'FAILURE');
return $content;
}
function validate_otp_token($transactionId,$otpToken){
mo_openid_start_session();
$customerKey = get_option('mo_openid_admin_customer_key');
if($_SESSION['mo_otptoken']){
$pass = checkTimeStamp($_SESSION['sent_on'],time());
$pass = checkTransactionId($customerKey, $otpToken, $transactionId, $pass);
if($pass)
$content = array('status' => 'SUCCESS');
else
$content = array('status' => 'FAILURE');
unset($_SESSION['$mo_otptoken']);
}
else
$content = array('status' =>'FAILURE');
return $content;
}
/*
* This function checks the time otp was sent to and the time
* user is validating the otp. The time difference shouldn't be
* more that 60 seconds.
*
* @param $sentTime - the time otp was sent to
* @param $validatedTime - the time otp was validated
*/
function checkTimeStamp($sentTime,$validatedTime){
$diff = round(abs($validatedTime - $sentTime) / 60,2);
if($diff>5)
return false;
else
return true;
}
/**
* This function checks and compares the transaction set in session
* and one generated during validation. Both need to match for the
* otp to be validated.
*
* @param $customerKey - the customer key of the user
* @param $otpToken - otp token entered by the user
* @param $transactionId - the transaction id in session
* @param $pass - the boolean value passed after the time check
*/
function checkTransactionId($customerKey,$otpToken,$transactionId,$pass){
if(!$pass){
return false;
}
$stringToHash = $customerKey . $otpToken;
$txtID = hash("sha512", $stringToHash);
if($txtID == $transactionId)
return true;
}
function mo_openid_filter_app_name($decrypted_app_name)
{
$decrypted_app_name = strtolower($decrypted_app_name);
$split_app_name = explode('_', $decrypted_app_name);
//check to ensure login starts at the click of social login button
if(empty($split_app_name[0])){
wp_die(get_option('mo_manual_login_error_message'));
}
else {
return $split_app_name[0];
}
}
function mo_openid_account_linking($messages) {
if(isset( $_GET['option']) && $_GET['option'] == 'disable-social-login' ){
$messages = '
'.get_option('mo_account_linking_message').'
';
}
return $messages;
}
function mo_openid_customize_logo(){
$logo ="
";
return $logo;
}
//delete rows from account linking table that correspond to deleted user
function mo_openid_delete_account_linking_rows($user_id){
global $wpdb;
$db_prefix = $wpdb->prefix;
$result = $wpdb->get_var($wpdb->prepare("DELETE from ".$db_prefix."mo_openid_linked_user where user_id = %s ",$user_id));
if($result === false){
wp_die(get_option('mo_delete_user_error_message'));
$wpdb->show_errors();
$wpdb->print_error();
exit;
}
}
function mo_openid_update_role($user_id='', $user_url=''){
// save the profile url in user meta // this was added to save facebook url in user meta as it is more than 100 chars
update_user_meta($user_id, 'moopenid_user_profile_url',$user_url);
$user = get_user_by('ID',$user_id);
if(get_option('mo_openid_login_role_mapping') && !(empty($user)) ){
$user->set_role( get_option('mo_openid_login_role_mapping') );
}
}
function mo_openid_get_wp_style(){
$path = site_url();
$path .= '/wp-admin/load-styles.php?c=1&dir=ltr&load%5B%5D=dashicons,buttons,forms,l10n,login&ver=4.8.1';
return $path;
}
function mo_openid_delete_profile_column($value, $columnName, $userId){
if('mo_openid_delete_profile_data' == $columnName){
global $wpdb;
$socialUser = $wpdb->get_var($wpdb->prepare('SELECT id FROM '. $wpdb->prefix .'mo_openid_linked_user WHERE user_id = %d ', $userId));
if($socialUser > 0 && !get_user_meta($userId,'mo_openid_data_deleted')){
return 'Delete';
}
else
return '
NA
';
}
}
add_action('manage_users_custom_column', 'mo_openid_delete_profile_column', 9, 3);
if(get_option('mo_openid_logout_redirection_enable') == 1){
add_filter( 'logout_url', 'mo_openid_redirect_after_logout',0,1);
}
function mo_openid_add_custom_column($columns){
$columns['mo_openid_delete_profile_data'] = 'Delete Social Profile Data';
return $columns;
}
function mo_openid_delete_social_profile_script(){
?>