-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.php
213 lines (164 loc) · 5.55 KB
/
session.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
ini_set('display_errors',true);
date_default_timezone_set('Africa/Johannesburg');
/*
SESSION data email, username, timestamp, token, token-exp
*/
// Global variables
define('SESSION_DURATION',3); // number of days
define('TOKEN_DURATION', 13);
require 'pages.php';
//require 'CustomSessionHandler.php';
function aunthenticate(){
$branch=0;
if (empty($_SESSION)){ // check if a session existed before
$output.="\n session variables are not set \ncurrent session id:".session_id();
$branch=1;
}
else{
if (is_session_valid()){
update_session_time();
$branch=3; //go to home page
}
else{
delete_session();
$branch=2; //go to login
}
}
if($branch==1 or $branch==2){
header("Location: login.php");
}
else {
// proceed to
}
}
function customer_restricted(){
if (empty($_SESSION)){
return;
}
else if($_SESSION["type"]!="customer"){
header("Location: login.php");
exit();
}
}
function company_restricted(){
if (empty($_SESSION)){
return;
}
else if($_SESSION["type"]!="company"){
header("Location: login.php");
exit();
}
}
function is_session_valid(){
if(empty($_SESSION['timestamp']) or empty($_SESSION['token-issue']) ){
return FALSE;
}
$session = duration($_SESSION['timestamp']);
$token = duration($_SESSION['token-issue']);
if ($session>SESSION_DURATION){
return false;
}
else if($token>TOKEN_DURATION){
return false;
}
else if(empty($_SESSION["token"])){
return false;
}
else{
return true;
}
}
function delete_session(){
session_unset();
session_destroy();
}
function verify_inputs(){
return true;
}
function duration($date){
$session = date_create_from_format("d.m.y",$date);
$current = date_create();
$diff =date_diff($current,$session);
$value =$diff->format("%a");
$value = intval($value);
return $value;
}
function create_session($access){
//SESSION data email, username, timestamp, token, token-issue
$_SESSION['timestamp']= update_session_time();
$_SESSION['token-issue']=update_session_time();
$_SESSION['email'] = $_POST['form-email'];
$_SESSION['token'] = $access->access_token;
$_SESSION['type'] = $access->accountType;
}
function update_session_time(){
$current_date = date_create();
$date_string = date_format($current_date,"d.m.y");
return $date_string;
}
function login(){
//login
$url="http://ec2-52-32-172-4.us-west-2.compute.amazonaws.com/Token";
$fields = array(
'grant_type'=>"password",
'username' => $_POST["form-email"],
'password' => $_POST["pswd"]
);
$fields_string="";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
//echo $result;
//exit();
$msg = json_decode($result);
//login the user
if ($code=="200"){
$response = Array($code,$result,$msg->accountType);
//custom_handler_init();
//return $status;
session_start();
session_set_cookie_params (3600*24*365);
create_session($msg);
return $response;
}
//throw error message
else{
$response = Array($code,$msg->error_description);
echo $msg->error_description." ".$code;
//exit;
header("Location: login.php?login=true&error=".urlencode($msg->error_description." Error code".$code));
}
}
/*function custom_handler_init(){
$handler = new CustomSessionHandler();
$status = session_set_save_handler(
array($handler, "open"),
array($handler, "close"),
array($handler, "read"),
array($handler, "write"),
array($handler, "destroy"),
array($handler, "gc")
);
register_shutdown_function('session_write_close');
return $status;
}
*/
function init(){
ini_set('display_errors',true);
require 'components.php';
//custom_handler_init();
session_start();
}
/**
Authorization: OAuth oauth_callback="http%3A%2F%2Ftariro.com", oauth_consumer_key="SIaKQTGJhdcSkZ5zzfJMtSPYz", oauth_nonce="NjM1ODg2MzgwMDAwNjc3NDUz", oauth_signature="YEkp%2FBl5Wf%2BeujlOFQYuM2iOJO4%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1453034000", oauth_version="1.0"
Authorization: OAuth oauth_nonce="NjM1ODg0MTc3Nzg1MjczMTg4", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1452813779", oauth_consumer_key="SIaKQTGJhdcSkZ5zzfJMtSPYz", oauth_callback="http%3A%2F%2Ftariro.com", oauth_signature="e8rHIQRaF%2B7Pv2NwnP7fK3xvsp4%3D", oauth_version="1.0"
OAuth oauth_callback="http%3A%2F%2Flocalhost%2Fsign‐in‐with‐twitter%2F", oauth_consumer_key="cChZNFj6T5R0TigYB9yd1w", oauth_nonce="ea9ec8429b68d6b77cd5600adbbb0456", oauth_signature="F1Li3tvehgcraF8DMJ7OyxO4w9Y%3D", oauth_signature_method="HMAC‐SHA1", oauth_timestamp="1318467427", oauth_version="1.0"
*/
?>