-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfollow.php
50 lines (43 loc) · 1.41 KB
/
follow.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
<?php
require_once("_micro_micro.php");
require_once("_user.php");
$user_target_id = $_GET["user"];
$user_current_id = user_current_id();
// User logged in?
if (!$user_current_id) {
header("Location: /register", true, 302);
exit();
}
// Target user exists
$stmt = db()->prepare("SELECT * FROM users WHERE id=:id");
$stmt->execute(["id" => $user_target_id]);
$user_target = $stmt->fetch();
if (!$user_target) {
header("Location: /", true, 302);
exit();
}
// Already following?
$stmt = db()->prepare("SELECT COUNT(1) as follow_count FROM follows WHERE user_id=:user_id AND user_target_id=:user_target_id");
$stmt->execute(["user_id" => $user_current_id, "user_target_id" => $user_target_id]);
$followed_by_me = $stmt->fetch()["follow_count"] > 0;
// Follow / UnFollow
if ($followed_by_me) {
db()->prepare("
DELETE FROM follows
WHERE user_id=:user_id AND user_target_id=:user_target_id
")
->execute(["user_id" => $user_current_id, "user_target_id" => $user_target_id]);
} else {
db()->prepare("
REPLACE INTO follows (user_id, user_target_id)
VALUES (:user_id, :user_target_id)
")
->execute(["user_id" => $user_current_id, "user_target_id" => $user_target_id]);
}
// Go Back
if (isset($_GET["return_url"])) {
header("Location: " . $_GET["return_url"], true, 302);
exit();
}
header("Location: /@" . $user_target["slug"], true, 302);
exit();