-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.php
94 lines (76 loc) · 2.26 KB
/
main.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
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/secrets.php';
## imgur settings
$imgur = new \adamroe\Imgur\API($imgur_api_key, $imgur_api_secret);
## bit.ly settings
$use_bitly = true;
$bitly = new \Hpatoio\Bitly\Client($bitly_api_key);
if (isset($_GET['method'])) {
# we are doing something
switch ($_GET['method']) {
case 'getToken':
showTokens();
break;
case 'shorten':
shortenLink();
break;
case 'upload':
if (isset($_GET['code'])) {
$tokens = $imgur->authorize(false, $_GET['code']);
} else {
die("No code provided");
}
/* if (!isset($_POST['message']))
die "No message provided!";
if (!isset($_POST['source']))
die "No source provided!";
if (!isset($_FILES['media']))
die "No media provided!"; */
uploadPicture();
break;
}
} else {
if (isset($_GET['code'])) {
global $imgur;
// user is authorised, so lets instantiate a connection to Imgur
$tokens = $imgur->authorize(false, $_GET['code']);
showTokens($tokens);
} else {
echo "<br/><br/><br/><center><h1>";
// GET parameter doesn't exist, so we will have to ask user to allow access for our application
$imgur->authorize();
}
}
function showTokens($t) {
$script_url = "https://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
$script_var = "?" . "code=" . $_GET['code'];
$script_var .= "&" . "method=upload";
echo "<h1>Congratulations, you are connected to Imgur</h1>";
echo "<p>To add this uploader to Tweetbot and upload pictures as " . $t['account_username'] . ", use the following URL in settings:<p>";
echo "<p><code>" . $script_url . $script_var . "</code></p>";
}
function shortenLink($l) {
global $bitly, $use_bitly;
$output = $l;
if ($use_bitly) {
$post['longUrl'] = $l;
$shortened = $bitly->Shorten($post);
$output = $shortened['url'];
}
return $output;
}
function uploadPicture() {
global $imgur;
$post = "";
if (isset($_POST['message'])) {
$post['description'] = $_POST['message'];
}
if (isset($_FILES['media']['name'])) {
$post['name'] = $_FILES['media']['name'];
}
$uploaded = $imgur->upload()->file($_FILES['media']['tmp_name'], $post);
$image_link = $uploaded['data']['link'];
$shortened_link = shortenLink($image_link);
echo json_encode(array("url" => $shortened_link));
}