-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtinyurlapi.php
71 lines (62 loc) · 2.26 KB
/
tinyurlapi.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
<?php
/*
iplogger - receive on telegram the ip info and the location of the user who clicked the link
Copyright (C) 2018 91DarioDev github.com/91DarioDev
iplogger is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
iplogger is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with iplogger. If not, see <http://www.gnu.org/licenses/>.
*/
$filename = 'timed_link.json';
if (!(file_exists($filename))){
$fp = fopen($filename, 'w');
fwrite($fp, json_encode(array_timed_link(null, null)));
fclose($fp);
}
$valid_period_link = 60; // seconds
function array_timed_link($short_link, $time){
$timed_link = array(
"link" => $short_link,
"time" => $time
);
return $timed_link;
}
// set the new timed_link and time it has been taken
function set_timed_link($short_link){
global $filename;
$time_now = time();
$timed_link_in = array_timed_link($short_link, $time_now);
$fp = fopen($filename, 'w');
fwrite($fp, json_encode($timed_link_in));
fclose($fp);
}
// get the last generated timed link
// if is null, create a new one and set it
// if is outdated, create a new one and set it
function get_timed_link($link){
global $filename;
$str = file_get_contents($filename);
$timed_link_in = json_decode($str, true);
global $valid_period_link;
if ($timed_link_in['link'] === null || ($timed_link_in['time']+$valid_period_link) < time()){
// generating new link
$new_short_link = createTinyURl($link);
set_timed_link($new_short_link);
return $new_short_link;
} else {
// no need to generate a new link
return $timed_link_in['link'];
}
}
// create a new short link
function createTinyUrl($strURL) {
$tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=" . $strURL);
return $tinyurl;
}
?>