-
Notifications
You must be signed in to change notification settings - Fork 0
/
NTNUI-login.php
138 lines (120 loc) · 3.77 KB
/
NTNUI-login.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
function getMemberInfo($phone, $password){
$url = ("https://api.ntnui.no/users/profile/");
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$auth = base64_encode($phone . ":" . $password);
$headers = array(
"Authorization: Basic " . $auth,
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$resp = curl_exec($curl);
curl_close($curl);
$data = json_decode($resp, true);
return $data;
}
//For debugging
// function console_log( $data ) {
// $output = "<script>console.log( '";
// $output .= json_encode(print_r($data, true));
// $output .= "' );</script>";
// echo $output;
// }
function main(){
if(!get_option('group_slug')){
return "Group name is not set in the configuration!";
}
if ( isset( $_POST['registerForm'] ) ) {
//Add +47 if number not contain country code
if(strlen($_POST['phone']) == 8 ){
$phone = "+47".$_POST['phone'];
}
else{
$phone = $_POST['phone'];
}
$memberInfo = getMemberInfo($phone, $_POST['password']);
if(isset($memberInfo["memberships"])){
foreach ($memberInfo["memberships"] as $membership){
//If NTNUI membership is active (expires after today) and part of group
if($membership["group"] == get_option('group_slug') && $memberInfo["contract_expiry_date"] > date("Y-m-d")){
//Access to rent
//Register user if not in database
$username = trim($phone, "+");
if ( !username_exists($username) && !email_exists($memberInfo["email"]) ) {
$userdata = array(
'user_login' => $username,
'user_pass' => $_POST['password'],
'user_email' => $memberInfo["email"],
'first_name' => $memberInfo["first_name"],
'last_name' => $memberInfo["last_name"],
'show_admin_bar_front' => "false",
'role' => get_option('access_type', 'subscriber'), #Preferred role fetched from preferences, default to subscriber if none is set
);
$user_id = wp_insert_user( $userdata );
}
//Login to user with the correct permissions
$user_login = $username;
$user = get_user_by("login" ,$user_login);
$user_id = $user->ID;
wp_set_current_user($user_id, $user_login);
wp_set_auth_cookie($user_id);
// Do action uses two arguments in order to work together with the woocommerce plugin.
do_action('wp_login', $user_login->$user_login, $user_login);
$response = "Login valid";
//Redirect to home page on login
header("location:". home_url());
}
}
if(!isset($response)){
$response = "No valid membership in NTNUI/".get_option('group_slug');
}
}
else{
$response = "Invalid username or password";
}
}
ob_start();
?>
<style>
#NTNUI-login {
width: 100%;
text-align: center;
}
input[type=text], input[type=password] {
max-width: 30vw;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 3px solid #ccc;
-webkit-transition: 0.5s;
transition: 0.5s;
outline: none;
}
input[type=text]:focus, input[type=password]:focus {
border: 3px solid #555;
}
input[type=submit] {
background-color: rgb(0,128,55);
border: none;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
</style>
<form method = "post" id= "NTNUI-login">
<input type="text" name="phone" autocomplete="off" placeholder="Phone (+0012345678)"><br>
<input type="password" name="password" autocomplete="off" placeholder="password"><br>
<input type="submit" name="registerForm" value="Log in">
<?php if(isset($response)){ echo $response; } ?>
</form>
<?php
return ob_get_clean();
}
add_shortcode( 'NTNUI-login', 'main' );
?>