-
Notifications
You must be signed in to change notification settings - Fork 0
/
con.php
147 lines (115 loc) · 3.2 KB
/
con.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
<?php
# http://www.php.net/manual/en/function.http-get-request-body.php#77305
function get_request_body() {
$body = @file_get_contents('php://input');
return $body;
}
# http://www.php.net/manual/en/function.http-response-code.php#107261
function set_response_code($code, $text) {
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
header($protocol . ' ' . $code . ' ' . $text);
}
function array_items($tab) {
$items = array();
foreach($tab as $key=>$value) {
$items[] = array($key, $value);
}
return $items;
}
function save_url($url, $time) {
$_SESSION["url"] = $url;
$_SESSION["time"] = $time;
}
function retrieve_url() {
if(!isset($_SESSION["url"]) || !isset($_SESSION["time"]))
return array(NULL, NULL);
return array($_SESSION["url"], $_SESSION["time"]);
}
function delete_url() {
session_destroy();
}
function get_key_value($cookiestr) {
$tab = explode('=', $cookiestr, 2);
if(count($tab) == 2) {
$tab[0] = trim($tab[0]);
return $tab;
} else {
return array('', trim($cookiestr));
}
}
function get_cookies($cookiestr) {
if($cookiestr === "") return array();
$cookies = array();
foreach(explode(';', $cookiestr) as $cookie) {
$cookies[] = get_key_value($cookie);
}
return $cookies;
}
function save_cookies($data, $cookies) {
if(array_key_exists("client", $data))
$client_str = $data["client"];
else
$client_str = "";
$data["client"] = get_cookies($client_str);
$data["httponly"] = array();
foreach($cookies as $cookie) {
if(in_array($cookie, $data["client"]))
continue;
$data["httponly"][] = $cookie;
}
return $data;
}
function post_url() {
$time = time();
$data = json_decode(get_request_body(), true);
if($data === NULL) {
set_response_code(400, "Bad Request");
echo "Incorrect data";
return;
}
$data = save_cookies($data, array_items($_COOKIE));
$encoded = json_encode($data);
save_url($encoded, $time);
$url = '?text='.session_id();
echo json_encode($url);
}
function get_url() {
list($json_data, $time) = retrieve_url();
delete_url();
if($json_data === NULL) {
set_response_code(403, "Forbidden");
echo "Incorrect key";
return;
}
if(!$time || (time() - $time > 15)) {
set_response_code(403, "Forbidden");
echo "URL expired.";
return;
}
$data = json_decode($json_data, true);
if($data === NULL) {
set_response_code(403, "Forbidden");
echo "Incorrect key";
return;
}
header("Location: ".$data["url"]);
foreach($data["client"] as $cookie) {
list($key, $value) = $cookie;
setcookie($key, $value);
}
foreach($data["httponly"] as $cookie) {
list($key, $value) = $cookie;
setcookie($key, $value, 0, "", "", "", true);
}
}
ini_set("session.use_cookies",0);
ini_set("session.use_trans_sid",1);
if(array_key_exists("text", $_GET))
session_id($_GET['text']);
session_start();
if(array_key_exists("text", $_GET)) {
get_url($_GET["text"]);
} else {
post_url();
}
?>