Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ######################################################################
- # vis_user_create_login_cookie()
- #
- ######################################################################
- function vis_user_create_login_cookie( $sessionToken ) {
- if ( VIS_DEBUG_MODE ) {
- echo "$ Creating login cookie... <br>";
- echo "-> Session Token: $sessionToken <br>";
- }
- # Validate 'session_token' from the parameters and store it in a cookie
- $userObject = msc_get_userId( $sessionToken );
- if ( empty( $userObject ) ) {
- if ( VIS_DEBUG_MODE ) {
- echo "<p> $ ERROR: Invalid Session Token / Sever down? </p>";
- }
- }
- $userId = $userObject["id"];
- $userRole = $userObject["role"];
- # Reducing tuckshop name
- if ( $userRole == "tuckshop_user" ) {
- $userRole = "tuckshop";
- }
- # Generating sessionId and saving it to a cookie
- $sessionId = vis_create_sessionid( $userId, $userRole, $sessionToken );
- if ( VIS_DEBUG_MODE ) {
- echo "-> User Id: $userId <br>";
- echo "-> User Role: $userRole <br>";
- echo "-> Session Id: $sessionId <br>";
- echo "<br><p>$ Saving cookie... <br>";
- }
- $ret = setcookie( VIS_SESSIONID_COOKIE_NAME, $sessionId, time() + 3600, "/", false );
- if ( ! $ret ) {
- if ( VIS_DEBUG_MODE ) {
- echo "-> $ ERROR: Cookies are blocked due to unexpected output.</p>";
- }
- exit();
- }
- if ( VIS_DEBUG_MODE ) {
- echo "-> Cookie saved successfuly.</p>";
- }
- $userObject = array(
- "userId" => $userId,
- "userRole" => $userRole,
- );
- return $userObject;
- }
- ########################################################################
- ## vis_user_create()
- ## Creates a user from MSC data.
- ########################################################################
- function vis_user_create( $userId, $userRole ) {
- $userObject;
- $userWpId;
- if ( $userRole == "customer" ) {
- if ( VIS_DEBUG_MODE ) {
- echo "$ Requesting customer (parent)... <br>";
- }
- $userObject = msc_get_customer($userId);
- $userWpId = VIS_GENERIC_CUSTOMER_ID;
- } elseif ( $userRole == "tuckshop" ) {
- if ( VIS_DEBUG_MODE ) {
- echo "$ Requesting user (tuckshop/school)... <br>";
- }
- $userObject = msc_get_user($userId);
- $userWpId = VIS_GENERIC_TUCKSHOP_ID;
- } else {
- if ( VIS_DEBUG_MODE ) {
- echo "<p> Invalid user role. </p>";
- }
- return;
- }
- $userFName = $userObject["firstname"];
- $userLName = $userObject["lastname"];
- $userEmail = $userObject["email"];
- $userAccountUrl = $userObject["accountUrl"];
- $userEditUrl = $userObject["editUrl"];
- if ( VIS_DEBUG_MODE ) {
- echo "<p> -> User firstname: $userFName <br>";
- echo " -> User lastname: $userLName <br>";
- echo " -> User Email: $userEmail <br>";
- echo " -> User Account URL: $userAccountUrl <br>";
- echo " -> User Edit URL: $userEditUrl </p>";
- }
- $wpUserObject = get_user_by( "email", $userEmail );
- if ( $wpUserObject ) {
- if ( VIS_DEBUG_MODE ) {
- echo "$ Old user. Found id: " . $wpUserObject->get('ID') . "<br>";
- }
- $userWpId = $wpUserObject->get('ID');
- $userMscRoles = get_user_meta($userWpId, VIS_MSCROLES_METADATA_FIELD, true);
- if ( ! in_array( $userRole, $userMscRoles ) ) {
- array_push( $userMscRoles, $userRole );
- }
- } else {
- if ( VIS_DEBUG_MODE ) {
- echo "$ User not found. Creating new account with MSC data...<br>";
- }
- $userWpId = vis_user_register($userId, $userEmail, $userFName, $userLName, array($userRole));
- $userMscRoles = get_user_meta($userWpId, VIS_MSCROLES_METADATA_FIELD, true);
- }
- // Save the MSC user ID on the WP User meta
- //add_user_meta( $userWpId, 'msc_user_id', $userId, true );
- if ( VIS_DEBUG_MODE ) {
- echo "$ User current MSC roles: ";
- var_dump( $userMscRoles );
- echo "<br>";
- }
- }
- ########################################################################
- ## vis_user_register ()
- ## Responsible to register a new user into WP database.
- ########################################################################
- function vis_user_register( $userId, $userEmail, $userFName, $userLName, $userMscRoles ) {
- // Generate the password and create the user
- $password = wp_generate_password( 12, false );
- $userWpId = wp_create_user( $userEmail, $password, $userEmail );
- if( is_wp_error( $userWpId ) ) {
- echo $userWpId->get_error_message();
- return 0;
- }
- // Set the nickname
- wp_update_user(
- array(
- 'ID' => $userWpId,
- 'nickname' => $userEmail,
- 'first_name' => $userFName,
- 'last_name' => $userLName
- ));
- // Set the role
- $user = new WP_User( $userWpId );
- $user->set_role('msc_user');
- return $userWpId;
- }
- ########################################################################
- ## vis_user_login()
- ## Logs in a user by its Id in the WP site.
- ########################################################################
- function vis_user_login() {
- # Decrypt cookie and load saved data
- $cryptedSessionId = $_COOKIE[VIS_SESSIONID_COOKIE_NAME];
- $sessionId = vis_decrypt_key($cryptedSessionId);
- $userId = vis_get_userid_from_sessionid($sessionId);
- $userRole = vis_get_userrole_from_sessionid($sessionId);
- $sessionToken = vis_get_sessiontoken_from_sessionid($sessionId);
- if ( VIS_DEBUG_MODE ) {
- echo "<p> -> Session Id: $sessionId <br>";
- echo " -> User Id: $userId <br>";
- echo " -> User Role: $userRole <br>";
- echo " -> Session Token: $sessionToken </p>";
- }
- $userWpId = vis_user_sync( $userId, $userRole );
- if ( $userWpId > 0 ) {
- if ( VIS_DEBUG_MODE ) {
- echo "$ Loading user with id: $userWpId ...<br>";
- }
- wp_set_auth_cookie( $userWpId, false, is_ssl() );
- // Setting msc_role on Session to WooCommerce payments
- if ( $userRole == "tuckshop" ) {
- $userRole = "tuckshop_user";
- } else {
- $api_key = get_option( 'msc-login_msc_general_msc_api_key' );
- $data = array(
- 'api_key' => $api_key,
- 'session_token' => $sessionToken,
- 'id' => $userId
- );
- $options = get_option( 'vis_settings' );
- $api_url = $options['vis_msc_url'] . "/index.php?route=api/customer/getSameSchoolTuckshopUsers";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $api_url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_POST, true);
- $result = curl_exec($ch);
- curl_close($ch);
- $result = json_decode( $result, true );
- $tk_users = $result["data"][0]["tuckshop_users"];
- $tuckshop_users = array();
- if( ! empty( $tk_users ) ) {
- foreach( $tk_users as $value ) {
- $user = get_users( array(
- 'number' => 1,
- 'meta_key' => 'msc_tuckshop_id',
- 'meta_value' => $value
- ) );
- if( ! empty($user[0] ) ) {
- $user_id = $user[0]->data->ID;
- $tuckshop_users[] = $user_id;
- }
- }
- }
- if( empty( $tuckshop_users ) ) {
- $tuckshop_users = "empty";
- }
- $_SESSION['tuckshop_users'] = $tuckshop_users;
- }
- $_SESSION['msc_role'] = $userRole;
- if ( VIS_DEBUG_MODE ) {
- echo "$ Setting MSC_ROLE on session: " . $_SESSION['msc_role'] . "...<br>";
- }
- // Save the MSC user ID to the WP user meta
- //update_user_meta( $userWpId, 'msc_user_id', $userId );
- }
- header("Refresh:0");
- exit();
- }
- ########################################################################
- ## vis_user_sync()
- ##
- ########################################################################
- function vis_user_sync( $userId, $userRole ) {
- // Requesting email from MSC api
- $userObject;
- $userWpId;
- if ( $userRole == "customer" ) {
- if ( VIS_DEBUG_MODE ) {
- echo "<p> $ Requesting customer (parent) #$userId... </p>";
- }
- $userObject = msc_get_customer( $userId );
- } elseif ( $userRole == "tuckshop" ) {
- if ( VIS_DEBUG_MODE ) {
- echo "<p> $ Requesting user (tuckshop/school) #$userId... </p>";
- }
- $userObject = msc_get_user( $userId );
- } else {
- if ( VIS_DEBUG_MODE ) {
- echo "<p> ERROR: Invalid user role. Unable to login user. </p>";
- }
- return;
- }
- $userFName = $userObject["firstname"];
- $userLName = $userObject["lastname"];
- $userEmail = $userObject["email"];
- $userAccountUrl = $userObject["accountUrl"];
- $userEditUrl = $userObject["editUrl"];
- if ( VIS_DEBUG_MODE ) {
- echo "<p> $ User firstname: $userFName <br>";
- echo " $ User lastname: $userLName <br>";
- echo " $ User Email: $userEmail <br>";
- echo " $ User Account URL: $userAccountUrl <br>";
- echo " $ User Edit URL: $userEditUrl </p>";
- }
- // Find user by given email
- $wpUserObject = get_user_by( "email", $userEmail );
- // If a user to registered with that email, get the user ID
- if ( $wpUserObject ) {
- if (VIS_DEBUG_MODE) {
- echo "$ Found user with WP id: " . $wpUserObject->get('ID') . "<br>";
- }
- $userWpId = $wpUserObject->get('ID');
- } else {
- if ( VIS_DEBUG_MODE ) {
- echo "<p> $ User not found. Creating account from MSC...</p>";
- }
- $userWpId = vis_user_register( $userId, $userEmail, $userFName, $userLName, array( $userRole ) );
- }
- // Did we fail to register the account?
- if ( is_wp_error( $userWpId ) ) {
- // Tell us why
- echo $userWpId->get_error_message();
- }
- vis_user_set_metadata($userWpId, $userId, $userRole, $userAccountUrl);
- return $userWpId;
- }
- function vis_user_set_metadata( $userWpId, $userId, $userRole, $userAccountUrl ) {
- $userMscRoles = get_user_meta( $userWpId, VIS_MSCROLES_METADATA_FIELD, true );
- if ( false == $userMscRoles ) {
- $userMscRoles = array( $userRole );
- } else {
- if ( ! in_array( $userRole, $userMscRoles ) ) {
- array_push( $userMscRoles, $userRole );
- }
- }
- # Add msc_roles metadata array field
- $ret = add_user_meta( $userWpId, VIS_MSCROLES_METADATA_FIELD, $userMscRoles, true );
- if ( VIS_DEBUG_MODE ) {
- echo "<br>RET: $ret <br>";
- echo "$ MSC Roles: ";
- var_dump( $userMscRoles );
- echo "<br>";
- }
- # Add msc Ids to the metadata
- if ( $userRole == "customer" ) {
- add_user_meta( $userWpId, "msc_customer_id", $userId, true );
- }
- if ( $userRole == "tuckshop" ) {
- add_user_meta( $userWpId, "msc_tuckshop_id", $userId, true );
- }
- if ( VIS_DEBUG_MODE ) {
- $ret = get_user_meta( $userWpId, "msc_customer_id", true );
- echo "$ MSC Customer Id: $ret";
- echo "<br>";
- $ret = get_user_meta( $userWpId, "msc_tuckshop_id", true );
- echo "$ MSC Tuckshop Id: $ret";
- echo "<br>";
- }
- # Add user account url to the metadata
- update_user_meta( $userWpId, "msc_account_url", $userAccountUrl );
- if ( VIS_DEBUG_MODE ) {
- $ret = get_user_meta( $userWpId, "msc_account_url", true );
- echo "<br>$ Variable MSC Account Url: $userAccountUrl";
- echo "<br>$ Saved MSC Account Url: $ret";
- echo "<br>";
- }
- }
- ########################################################################
- ## vis_user_logout()
- ## Logs out the current WP user.
- ########################################################################
- function vis_user_logout()
- {
- if (VIS_DEBUG_MODE) {
- echo "$ Logging out... <br>";
- }
- $ret = setcookie(VIS_SESSIONID_COOKIE_NAME, "", time() + 1, "/", false);
- if ($ret) {
- if (VIS_DEBUG_MODE) {
- echo "$ Login cookie deleted successfuly.<br>";
- }
- }
- wp_logout();
- header("Refresh:0");
- if (VIS_DEBUG_MODE) {
- echo "$ User logged out successfuly!<br>";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement