-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.php
51 lines (45 loc) · 1.71 KB
/
config.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
<?php
const TARGETS_PATH = __DIR__ . '/../targets.json';
function readTargets(): array {
return json_decode(
file_exists(TARGETS_PATH)
? file_get_contents(TARGETS_PATH)
: '{}', true);
}
function writeTargets(array $data): void {
uasort($data, function ($a, $b): int {
return strcmp($a['user'], $b['user']);
});
$f = fopen(TARGETS_PATH, 'w');
fwrite($f, json_encode($data, JSON_PRETTY_PRINT));
fclose($f);
}
if (basename(__FILE__) == basename($_SERVER['SCRIPT_FILENAME'])) {
header('Content-Type: text/plain');
$targets = readTargets();
switch ($_SERVER['REQUEST_METHOD']) {
case 'PUT':
if (!isset($_GET['u'])) die('Username not specified!');
require __DIR__ . '/API.php';
$api = new API();
$res = $api->userByScreenName($_GET['u']);
if ($res == '') die("Couldn't connect to Twitter!");
$res = json_decode($res);
if (!property_exists($res, 'data') || !property_exists($res->data, 'user'))
die("Such a user doesn't exist!");
$user = $res->data->user->result;
if (!array_key_exists($_GET['id'], $targets))
$targets[$user->rest_id] = array('user' => $user->legacy->screen_name, 'last' => 0);
else
$targets[$user->rest_id]['user'] = $user->legacy->screen_name;
echo 'Done';
break;
case 'DELETE':
if (!isset($_GET['t'])) die('Target not specified!');
if (!array_key_exists($_GET['t'], $targets)) die("Target doesn't exist!");
unset($targets[$_GET['t']]);
echo 'Done';
break;
}
writeTargets($targets);
}