Ejemplo n.º 1
0
            $update_site_registration->setRequestOxdId($_SESSION['oxd_id']);
            $update_site_registration->setRequestAuthorizationRedirectUri(Oxd_RP_config::$authorization_redirect_uri);
            $update_site_registration->setRequestPostLogoutRedirectUri(Oxd_RP_config::$post_logout_redirect_uri);
            $update_site_registration->setRequestContacts([$_POST['your_mail']]);
            $update_site_registration->setRequestGrantTypes(Oxd_RP_config::$grant_types);
            $update_site_registration->setRequestResponseTypes(Oxd_RP_config::$response_types);
            $update_site_registration->setRequestScope(Oxd_RP_config::$scope);
            $update_site_registration->request();
            $_SESSION['oxd_id'] = $update_site_registration->getResponseOxdId();
        }
    }
    require_once './Get_authorization_url.php';
    $get_authorization_url = new Get_authorization_url();
    $get_authorization_url->setRequestOxdId($_SESSION['oxd_id']);
    $get_authorization_url->setRequestScope(Oxd_RP_config::$scope);
    $get_authorization_url->setRequestAcrValues(Oxd_RP_config::$acr_values);
    $get_authorization_url->request();
    header("Location: " . $get_authorization_url->getResponseAuthorizationUrl());
    exit;
} else {
    ?>
    <form method="post" action="/">
        <label for="your_mail">Your email. </label>
        <input type="email" name="your_mail" placeholder="Enter your email." />
        <br/><br/>
        <label for="gluu_server_url">Your Gluu server url. </label>
        <input type="url" name="gluu_server_url" placeholder="Enter Gluu server url." />
        <br/><br/>
        <input type="submit" name="submit" value="Login" />
    </form>
    <?php 
 function oxd_openid_login_validate()
 {
     if (isset($_REQUEST['option']) and strpos($_REQUEST['option'], 'getOxdSocialLogin') !== false) {
         $http = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? "https://" : "http://";
         $parts = parse_url($http . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
         parse_str($parts['query'], $query);
         $conf = get_option('oxd_config');
         if (get_option('oxd_id')) {
             if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
                 if (!exec('netstat -aon |find/i "listening" |find "' . $conf['oxd_host_port'] . '"')) {
                     echo "<script>\n\t\t\t\t\t\t\t\t\talert('Oxd server is not switched on.');location.href='" . site_url() . "';\n\t\t\t\t\t\t\t\t </script>";
                     exit;
                 }
             } else {
                 if (!exec('netstat -tulpn | grep :' . $conf['oxd_host_port'])) {
                     echo "<script>\n\t\t\t\t\t\t\t\t\talert('Oxd server is not switched on.');location.href='" . site_url() . "';\n\t\t\t\t\t\t\t\t </script>";
                     exit;
                 }
             }
         }
         $get_authorization_url = new Get_authorization_url();
         $get_authorization_url->setRequestOxdId(get_option('oxd_id'));
         $get_authorization_url->setRequestAcrValues([$_REQUEST['app_name']]);
         $get_authorization_url->request();
         wp_redirect($get_authorization_url->getResponseAuthorizationUrl());
         exit;
     }
     if (isset($_REQUEST['option']) and strpos($_REQUEST['option'], 'oxdOpenId') !== false) {
         session_start();
         $http = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? "https://" : "http://";
         $parts = parse_url($http . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
         parse_str($parts['query'], $query);
         $config_option = get_option('oxd_config');
         $conf = get_option('oxd_config');
         $get_tokens_by_code = new Get_tokens_by_code();
         $get_tokens_by_code->setRequestOxdId(get_option('oxd_id'));
         $get_tokens_by_code->setRequestCode($_REQUEST['code']);
         $get_tokens_by_code->setRequestState($_REQUEST['state']);
         $get_tokens_by_code->setRequestScopes($config_option["scope"]);
         $get_tokens_by_code->request();
         $get_tokens_by_code_array = $get_tokens_by_code->getResponseObject()->data->id_token_claims;
         $_SESSION['user_oxd_id_token'] = $get_tokens_by_code->getResponseIdToken();
         $_SESSION['user_oxd_access_token'] = $get_tokens_by_code->getResponseAccessToken();
         $_SESSION['session_states'] = $_REQUEST['session_state'];
         $_SESSION['states'] = $_REQUEST['state'];
         setcookie('user_oxd_id_token', $get_tokens_by_code->getResponseIdToken(), time() + 3600 * 24 * 100, COOKIEPATH, COOKIE_DOMAIN, false);
         setcookie('user_oxd_access_token', $get_tokens_by_code->getResponseAccessToken(), time() + 3600 * 24 * 100, COOKIEPATH, COOKIE_DOMAIN, false);
         setcookie('session_states', $_REQUEST['session_state'], time() + 3600 * 24 * 100, COOKIEPATH, COOKIE_DOMAIN, false);
         setcookie('states', $_REQUEST['state'], time() + 3600 * 24 * 100, COOKIEPATH, COOKIE_DOMAIN, false);
         $get_user_info = new Get_user_info();
         $get_user_info->setRequestOxdId(get_option('oxd_id'));
         $get_user_info->setRequestAccessToken($_SESSION['user_oxd_access_token']);
         $get_user_info->request();
         $get_user_info_array = $get_user_info->getResponseObject()->data->claims;
         $reg_first_name = '';
         $reg_last_name = '';
         $reg_email = '';
         $reg_avatar = '';
         $reg_display_name = '';
         $reg_nikname = '';
         $reg_website = '';
         $reg_middle_name = '';
         $reg_country = '';
         $reg_city = '';
         $reg_region = '';
         $reg_gender = '';
         $reg_postal_code = '';
         $reg_fax = '';
         $reg_home_phone_number = '';
         $reg_phone_mobile_number = '';
         $reg_street_address = '';
         $reg_birthdate = '';
         if ($get_user_info_array->website[0]) {
             $reg_website = $get_user_info_array->website[0];
         } elseif ($get_tokens_by_code_array->website[0]) {
             $reg_website = $get_tokens_by_code_array->website[0];
         }
         if ($get_user_info_array->nickname[0]) {
             $reg_nikname = $get_user_info_array->nickname[0];
         } elseif ($get_tokens_by_code_array->nickname[0]) {
             $reg_nikname = $get_tokens_by_code_array->nickname[0];
         }
         if ($get_user_info_array->name[0]) {
             $reg_display_name = $get_user_info_array->name[0];
         } elseif ($get_tokens_by_code_array->name[0]) {
             $reg_display_name = $get_tokens_by_code_array->name[0];
         }
         if ($get_user_info_array->given_name[0]) {
             $reg_first_name = $get_user_info_array->given_name[0];
         } elseif ($get_tokens_by_code_array->given_name[0]) {
             $reg_first_name = $get_tokens_by_code_array->given_name[0];
         }
         if ($get_user_info_array->family_name[0]) {
             $reg_last_name = $get_user_info_array->family_name[0];
         } elseif ($get_tokens_by_code_array->family_name[0]) {
             $reg_last_name = $get_tokens_by_code_array->family_name[0];
         }
         if ($get_user_info_array->middle_name[0]) {
             $reg_middle_name = $get_user_info_array->middle_name[0];
         } elseif ($get_tokens_by_code_array->middle_name[0]) {
             $reg_middle_name = $get_tokens_by_code_array->middle_name[0];
         }
         if ($get_user_info_array->email[0]) {
             $reg_email = $get_user_info_array->email[0];
         } elseif ($get_tokens_by_code_array->email[0]) {
             $reg_email = $get_tokens_by_code_array->email[0];
         }
         if ($get_user_info_array->country[0]) {
             $reg_country = $get_user_info_array->country[0];
         } elseif ($get_tokens_by_code_array->country[0]) {
             $reg_country = $get_tokens_by_code_array->country[0];
         }
         if ($get_user_info_array->gender[0]) {
             if ($get_user_info_array->gender[0] == 'male') {
                 $reg_gender = '1';
             } else {
                 $reg_gender = '2';
             }
         } elseif ($get_tokens_by_code_array->gender[0]) {
             if ($get_tokens_by_code_array->gender[0] == 'male') {
                 $reg_gender = '1';
             } else {
                 $reg_gender = '2';
             }
         }
         if ($get_user_info_array->locality[0]) {
             $reg_city = $get_user_info_array->locality[0];
         } elseif ($get_tokens_by_code_array->locality[0]) {
             $reg_city = $get_tokens_by_code_array->locality[0];
         }
         if ($get_user_info_array->postal_code[0]) {
             $reg_postal_code = $get_user_info_array->postal_code[0];
         } elseif ($get_tokens_by_code_array->postal_code[0]) {
             $reg_postal_code = $get_tokens_by_code_array->postal_code[0];
         }
         if ($get_user_info_array->phone_number[0]) {
             $reg_home_phone_number = $get_user_info_array->phone_number[0];
         } elseif ($get_tokens_by_code_array->phone_number[0]) {
             $reg_home_phone_number = $get_tokens_by_code_array->phone_number[0];
         }
         if ($get_user_info_array->phone_mobile_number[0]) {
             $reg_phone_mobile_number = $get_user_info_array->phone_mobile_number[0];
         } elseif ($get_tokens_by_code_array->phone_mobile_number[0]) {
             $reg_phone_mobile_number = $get_tokens_by_code_array->phone_mobile_number[0];
         }
         if ($get_user_info_array->picture[0]) {
             $reg_avatar = $get_user_info_array->picture[0];
         } elseif ($get_tokens_by_code_array->picture[0]) {
             $reg_avatar = $get_tokens_by_code_array->picture[0];
         }
         if ($get_user_info_array->street_address[0]) {
             $reg_street_address = $get_user_info_array->street_address[0];
         } elseif ($get_tokens_by_code_array->street_address[0]) {
             $reg_street_address = $get_tokens_by_code_array->street_address[0];
         }
         if ($get_user_info_array->birthdate[0]) {
             $reg_birthdate = $get_user_info_array->birthdate[0];
         } elseif ($get_tokens_by_code_array->birthdate[0]) {
             $reg_birthdate = $get_tokens_by_code_array->birthdate[0];
         }
         if ($get_user_info_array->region[0]) {
             $reg_region = $get_user_info_array->region[0];
         } elseif ($get_tokens_by_code_array->region[0]) {
             $reg_region = $get_tokens_by_code_array->region[0];
         }
         $username = '';
         if ($get_user_info->getResponsePreferredUsername()) {
             $username = $get_user_info->getResponsePreferredUsername();
         } else {
             $email_split = explode("@", $reg_email);
             $username = $email_split[0];
         }
         if ($reg_email) {
             if (email_exists($reg_email)) {
                 $user = get_user_by('email', $reg_email);
                 $user_id = $user->ID;
                 wp_update_user(array('ID' => $user_id, 'user_login' => $username, 'user_nicename' => $reg_nikname, 'user_email' => $reg_email, 'display_name' => $reg_display_name, 'first_name' => $reg_first_name, 'last_name' => $reg_last_name, 'user_url' => $reg_website));
                 if (get_option('oxdOpenId_gluu_login_avatar') && isset($reg_avatar)) {
                     update_user_meta($user_id, 'oxdOpenId_user_avatar', $reg_avatar);
                 }
                 do_action('wp_login', $user->user_login, $user);
                 wp_set_auth_cookie($user_id, true);
             } else {
                 if (username_exists($username)) {
                     $user = get_user_by('login', $username);
                     $user_id = $user->ID;
                     wp_update_user(array('ID' => $user_id, 'user_login' => $username, 'user_nicename' => $reg_nikname, 'user_email' => $reg_email, 'display_name' => $reg_display_name, 'first_name' => $reg_first_name, 'last_name' => $reg_last_name, 'user_url' => $reg_website));
                     if (get_option('oxdOpenId_gluu_login_avatar') && isset($reg_avatar)) {
                         update_user_meta($user_id, 'oxdOpenId_user_avatar', $reg_avatar);
                     }
                     do_action('wp_login', $user->user_login, $user);
                     wp_set_auth_cookie($user_id, true);
                 } else {
                     if (get_option('oxd_openid_auto_register_enable')) {
                         $random_password = wp_generate_password(10, false);
                         $userdata = array('user_login' => $username, 'user_nicename' => $reg_nikname, 'user_email' => $reg_email, 'user_pass' => $random_password, 'display_name' => $reg_display_name, 'first_name' => $reg_first_name, 'last_name' => $reg_last_name, 'user_url' => $reg_website);
                         $user_id = wp_insert_user($userdata);
                         $user = get_user_by('email', $reg_email);
                         if (get_option('oxdOpenId_gluu_login_avatar') && isset($reg_avatar)) {
                             update_user_meta($user_id, 'oxdOpenId_user_avatar', $reg_avatar);
                         }
                         do_action('wp_login', $user->user_login, $user);
                         wp_set_auth_cookie($user_id, true);
                     }
                 }
             }
         }
         $redirect_url = oxd_openid_get_redirect_url();
         wp_redirect($redirect_url);
         exit;
     }
     if (isset($_REQUEST['autoregister']) and strpos($_REQUEST['autoregister'], 'false') !== false) {
         if (!is_user_logged_in()) {
             oxd_openid_disabled_register_message();
         }
     }
 }