-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.php
More file actions
90 lines (63 loc) · 2.39 KB
/
functions.php
File metadata and controls
90 lines (63 loc) · 2.39 KB
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
<?php
//testcommit
include_once('alphaID.php');
$config = parse_ini_file('config.ini', true);
$functions = array('shorten', 'fetch');
if (in_array($_REQUEST['func'], $functions))
$_REQUEST['func']();
function increment(){ // runtalan: incomplete and probably moving to the fetch function...
global $config;
$ret = array('status' => 'fail');
$dbh = new PDO($config['coolsitebro']['driver'] . ':host=' . $config['coolsitebro']['host'] . ';dbname=' . $config['coolsitebro']['dbname'],
$config['coolsitebro']['username'], $config['coolsitebro']['password']);
$code = alphaID($_REQUEST['code'], true);
$sth = $dbh->prepare("UPDATE redirects SET visit=visit+1 WHERE id=?");
}
function shorten() {
global $config;
$ret = array('status' => 'fail');
$dbh = new PDO($config['coolsitebro']['driver'] . ':host=' . $config['coolsitebro']['host'] . ';dbname=' . $config['coolsitebro']['dbname'],
$config['coolsitebro']['username'], $config['coolsitebro']['password']);
$url = $_REQUEST['url'];
$private = $_REQUEST['private'] || false;
if ( !(strpos($url, "http://") === 0) || !(strpos($url, "https://") === 0) ) $url = "http://" . $url;
$sth = $dbh->prepare("insert into redirects (url, private) values (?, ?)");
if ( !$sth ) {
$err = $dbh->errorInfo();
$ret['reason'] = $err[2]; // $err is an array full of 2 useless items followed by descriptive text
}
else if ( !$sth->execute(array($url, $private)) ) {
$err = $sth->errorInfo();
$ret['reason'] = $err[2];
}
else {
$ret['status'] = 'success';
$ret['data'] = alphaID($dbh->lastInsertId());
}
echo json_encode($ret);
}
function fetch() {
global $config;
$ret = array('status' => 'fail');
$dbh = new PDO($config['coolsitebro']['driver'] . ':host=' . $config['coolsitebro']['host'] . ';dbname=' . $config['coolsitebro']['dbname'],
$config['coolsitebro']['username'], $config['coolsitebro']['password']);
$code = alphaID($_REQUEST['code'], true);
$sth = $dbh->prepare("select url from redirects where id=?");
if ( !$sth ) {
$err = $dbh->errorInfo();
$ret['reason'] = $err[2]; // $err is an array full of 2 useless items followed by descriptive text
}
else if ( !$sth->execute(array($code)) ) {
$err = $sth->errorInfo();
$ret['reason'] = $err[2];
}
else {
$ret['status'] = 'success';
$url = $sth->fetchColumn();
}
if ( $ret['status'] == 'fail')
echo json_encode($ret);
else
header("Location: " . $url);
}
?>