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
|
<?php
$config = json_decode(file_get_contents('../config.json'), TRUE);
require_once('../lib/codebird-php/src/codebird.php');
\Codebird\Codebird::setConsumerKey($config['key'], $config['secret']);
\Codebird\Codebird::setBearerToken($config['token']);
$cb = \Codebird\Codebird::getInstance();
$user = $_GET['user'];
$spamFilter = FALSE;
$filterMatch = [];
if (preg_match('/^(.*)\/spamfilter$/', $user, $filterMatch)) {
$user = $filterMatch[1];
$spamFilter = TRUE;
}
if ($user) {
$cacheFile = '../cache/'.$user.'.json';
$cacheTime = file_exists($cacheFile) ? filemtime($cacheFile) : 0;
$content = '';
if ($cacheTime > strtotime('-15 minutes')) {
$content = json_decode(file_get_contents($cacheFile));
}
else {
$content = $cb->statuses_userTimeline([
'screen_name' => $user,
'count' => 200,
'exclude_replies' => TRUE
], TRUE);
if (isset($content->rate)) {
unset($content->rate);
}
file_put_contents($cacheFile, json_encode($content));
$cacheTime = time();
}
if ($content->httpstatus !== 200) {
header('HTTP/1.1 '.$content->httpstatus);
if (isset($content->error)) {
print $content->error;
die();
}
if (isset($content->errors)) {
foreach ($content->errors as $error) {
print $error->message.' ('.$error->code.')<br />';
}
die();
}
}
unset($content->httpstatus);
if ($spamFilter) {
$db = new PDO('sqlite:../spamlinks.db');
$spamQuery = $db->prepare('SELECT id FROM spamlinks WHERE username = :name');
$spamQuery->bindParam(':name', $user);
$spamQuery->execute();
$spamContent = array_map(
function($row) {
return $row[0];
},
$spamQuery->fetchAll()
);
$spamHashes = [];
$filteredContent = [];
foreach ($content as $c) {
if (!in_array($c->id_str, $spamContent)) {
$twitterURLs = FALSE;
$urls = array_filter(
array_map(
function($url) {
return $url->expanded_url;
},
$c->entities->urls
),
function($url) use(&$twitterURLs) {
$urlParts = parse_url($url);
if ($urlParts['host'] == 'twitter.com') {
$twitterURLs = TRUE;
return FALSE;
}
return TRUE;
}
);
if (!$urls) {
if (!$twitterURLs) {
$filteredContent[] = $c;
}
} else {
sort($urls);
$urlHash = md5(implode('|', $urls));
if (isset($filteredContent[$urlHash])) {
$spamHashes[] = $c->id_str;
}
$filteredContent[$urlHash] = $c;
}
}
}
usort($filteredContent, function($c1, $c2) { return strcmp($c1->id_str, $c2->id_str); });
$content = $filteredContent;
if ($spamHashes) {
foreach ($spamHashes as $hash) {
$insertQuery = $db->prepare(
'INSERT INTO spamlinks(id, username) VALUES (?, ?)'
);
$insertQuery->bindParam(1, $hash);
$insertQuery->bindParam(2, $user);
$insertQuery->execute();
}
}
}
require_once('../lib/smarty3/Smarty.class.php');
$smarty = new Smarty();
$smarty->setCacheDir('../cache/smarty');
$smarty->setCompileDir('../cache/smarty/compile');
$smarty->setTemplateDir('../templates');
$smarty->assign('cacheTime', $cacheTime);
$smarty->assign('user', $user);
$smarty->assign('content', $content);
$format = $_GET['format'];
switch ($format) {
case 'rss':
header('Content-Type: application/rss+xml');
$smarty->display('rss.tpl');
break;
case 'atom':
default:
header('Content-Type: application/atom+xml');
$smarty->display('atom.tpl');
break;
}
}
?>
|