-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
212 lines (181 loc) · 6.69 KB
/
index.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
<?php
error_reporting(E_ALL);
require "config.php";
require "botheaders.php";
spl_autoload_register(function ($class) {
$sClassName = str_replace("\\", "/", $class);
$file = dirname(__FILE__) . "/src/" . $sClassName . ".php";
if (file_exists($file)) {
include_once $file;
return;
}
echo "\n not found: " . $file . "";
});
/**
* gets the videourl from a tweet-id using the twitter v1 api since the api v2 does not return the videourl...
*/
function getVideoUrl(\Twifer\API $api, $tweetId): string
{
// add tweet_mode=>extended in case extended_entites are missing
$result = $api->request("GET", "statuses/show/" . $tweetId,["include_entities"=>true,"tweet_mode"=>"extended"]);
$media = $result["extended_entities"]["media"][0];
// -1 because for gif's the bitrate is set to 0
$biggestBitrate = -1;
$videourl = "";
foreach ($media["video_info"]["variants"] as $variant) {
if ($variant["content_type"] != "video/mp4")
continue;
if ($variant["bitrate"] > $biggestBitrate) {
$biggestBitrate = $variant["bitrate"];
$videourl = $variant["url"];
}
}
return $videourl;
}
function generatePreview($tweetId,$preferedPhoto = 1) {
global $knownbotheaders, $config;
$useragent = strtolower($_SERVER["HTTP_USER_AGENT"]);
$uri = $_SERVER['REQUEST_URI'];
// but i'm in subfolder for development...
if (isset($config["subfolder"]) && strlen($config["subfolder"]) > 0) {
$uri = str_replace($config["subfolder"], "", $uri);
}
$twitterlink = "https://twitter.com" . $uri;
/**
* @var $isBot
* is set to ture if useragent matches with a bot
*/
$isBot = false;
foreach ($knownbotheaders as $knownbotheader) {
if (strtolower($knownbotheader) == $useragent) {
$isBot = true;
break;
}
}
if ($config["devmode"] && $config["forcebot"]) {
$isBot = true;
}
// if useragent is not a bot forward to twitter again
if ($isBot == false) {
if ($config["devmode"]) {
echo "this would forward normally to: " . $twitterlink;
} else {
header("Location: " . $twitterlink);
}
return;
}
// now that we know it's a bot check first if we have data in our cache
$result = \lc\twCache::retriveFromCache($tweetId);
$tweetFoundInCache = true;
if ($result == null) {
$tweetFoundInCache = false;
// if we are here the request comes for a bot
$twConnection = new \Twifer\API(
$config["credentials"]["consumer_key"],
$config["credentials"]["consumer_secret"],
$config["credentials"]["bearer_token"]
);
$ApiExpansions = "?expansions=attachments.media_keys,author_id&media.fields=type,url,height,width";
$result = $twConnection->request("GET", "/2/tweets/" . $tweetId . $ApiExpansions);
}
//print_r($result);
// check the type, text,photo or video
$tweetType = "unknown";
$appendSiteName = "";
if (isset($result["errors"])) {
//telegrambot doesn't like 404
//http_response_code(404);
$errortitle = strtolower( $result["errors"][0]["title"]);
if(strpos($errortitle,"authorization")> -1){
$tweetType = "private";
}else{
$tweetType = "error";
$errordetail = $result["errors"][0]["detail"];
}
} else if (!isset($result["data"]["attachments"])) {
// text only
$tweetType = "text";
} else {
if ($result["includes"]["media"][0]["type"] == "photo") {
$tweetType = "photo";
$photourl = $result["includes"]["media"][0]["url"];
$mediaCount = count($result["includes"]["media"]);
if ( $mediaCount > 1) {
$appendSiteName = "1/" . count($result["includes"]["media"]) . " 📷";
}
if($preferedPhoto > 1 && $preferedPhoto<= 4){
echo "hier";
if($mediaCount >= $preferedPhoto){
$photourl = $result["includes"]["media"][$preferedPhoto-1]["url"];
}
}
}else{
// video/animation
$tweetType = "video";
if (!isset($result["includes"]["media"][0]["url"])) {
$videourl = getVideoUrl($twConnection, $tweetId);
$result["includes"]["media"][0]["url"] = $videourl;
}else{
$videourl = $result["includes"]["media"][0]["url"];
}
}
}
if($tweetType == "private"){
if ($config["devmode"]) {
echo "this would forward normally to: " . $twitterlink;
} else {
header("Location: " . $twitterlink);
}
}elseif ($tweetType != "error") {
$sitename = "vstwitter.com " . $appendSiteName;
$title = $result["includes"]["users"][0]["name"] . " (@" . $result["includes"]["users"][0]["username"] . ")";
$description = $result["data"]["text"];
}
include "ogtemplates/" . $tweetType . ".php";
if (!$tweetFoundInCache) {
\lc\twCache::insertIntoCache($tweetId, $result);
}
}
/**
* //example links from twitter
* https://twitter.com/CincinnatiZoo/statuses/1478048570095845380
* https://twitter.com/CincinnatiZoo/status/1478048570095845380
* https://twitter.com/CincinnatiZoo/status/1478048570095845380/video/1
* https://twitter.com/i/web/status/1478048570095845380
*/
$router = new \Bramus\Router\Router();
$router->get(".+/status/(\d+)", [], function ($id){generatePreview($id);});
$router->get(".+/statuses/(\d+)", [], function ($id){generatePreview($id);});
$router->get(".+/status/(\d+)/photo/(\d+)", [], function ($id,$preferedPhoto){generatePreview($id,$preferedPhoto);});
$router->get("i/web/status/(\d+)", [], function ($id){generatePreview($id);});
/**
* forward to github to readme etc
*/
$router->get("/", [], function () use($config) {
$url = "https://github.com/littleCdev/vstwitter";
if ($config["devmode"]) {
echo "this would forward normally to: " . $url;
} else {
header("Location: " . $url);
}
});
/**
* redict user to twitter since i don't know what they wanted to see
*/
$router->get(".*", [], function () use($config) {
$uri = $_SERVER['REQUEST_URI'];
if (isset($config["subfolder"]) && strlen($config["subfolder"]) > 0) {
$uri = str_replace($config["subfolder"], "", $uri);
}
$twitterlink = "https://twitter.com" . $uri;
if ($config["devmode"]) {
echo "this would forward normally to: " . $twitterlink;
} else {
header("Location: " . $twitterlink);
}
});
try{
$router->run();
}catch(Exception $e){
error_log($e->getMessage());
}