-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfetch_chirps.php
executable file
·113 lines (92 loc) · 4.91 KB
/
fetch_chirps.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
<?php
session_start();
// Function to make URLs clickable, embed images, and handle mentions
function makeLinksClickable($text) {
// Pattern for URLs
$urlPattern = '/\b((https?:\/\/)?([a-z0-9-]+\.)+[a-z]{2,6}(\/[^\s]*)?(\?[^\s]*)?)/i';
// Replace URLs with clickable links or images
$text = preg_replace_callback($urlPattern, function($matches) {
$url = $matches[1];
// Parse URL and query string
$parsedUrl = parse_url($url);
$path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
$query = isset($parsedUrl['query']) ? $parsedUrl['query'] : '';
// Strip "https://" and "www." from the display
$displayUrl = preg_replace('/^https?:\/\/(www\.)?/i', '', $url);
// Check for image extension in the path or query string
$imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];
$fileExtension = pathinfo($path, PATHINFO_EXTENSION);
// Check if the query string contains an image format
foreach ($imageExtensions as $extension) {
if (stripos($query, 'format=' . $extension) !== false) {
$fileExtension = $extension;
break;
}
}
if (in_array(strtolower($fileExtension), $imageExtensions)) {
// If it's an image, embed it
return '<div class="chirpImageContainer"><img class="imageInChirp" src="' . $url . '" alt="Photo"></div>';
} else {
// Otherwise, create a clickable link
return '<a class="linkInChirp" href="' . htmlspecialchars($url, ENT_QUOTES) . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($displayUrl, ENT_QUOTES) . '</a>';
}
}, $text);
// Pattern for @mentions, ensuring it doesn't match inside URLs
$mentionPattern = '/(?<!\S)@([a-zA-Z0-9_]+)(?!\S)/';
// Replace mentions with clickable profile links
$text = preg_replace_callback($mentionPattern, function($matches) {
$username = $matches[1];
$profileUrl = '/user/?id=' . htmlspecialchars($username, ENT_QUOTES);
return '<a class="linkInChirp" href="' . $profileUrl . '">@' . htmlspecialchars($username, ENT_QUOTES) . '</a>';
}, $text);
return $text;
}
// Improved error handling
try {
// Set up PDO with error mode to exception
$db = new PDO('sqlite:' . __DIR__ . '/../chirp.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Validate and sanitize inputs
$offset = isset($_GET['offset']) ? max((int)$_GET['offset'], 0) : 0;
$limit = 12;
$currentUserId = isset($_SESSION['user_id']) ? (int)$_SESSION['user_id'] : null;
// Single query to fetch chirps with user info and interaction counts
$query = '
SELECT chirps.*, users.username, users.name, users.profilePic, users.isVerified,
(SELECT COUNT(*) FROM likes WHERE chirp_id = chirps.id) AS like_count,
(SELECT COUNT(*) FROM rechirps WHERE chirp_id = chirps.id) AS rechirp_count,
(SELECT COUNT(*) FROM chirps AS replies WHERE replies.parent = chirps.id AND replies.type = "reply") AS reply_count,
(SELECT COUNT(*) FROM likes WHERE chirp_id = chirps.id AND user_id = :user_id) AS liked_by_current_user,
(SELECT COUNT(*) FROM rechirps WHERE chirp_id = chirps.id AND user_id = :user_id) AS rechirped_by_current_user
FROM chirps
INNER JOIN users ON chirps.user = users.id
WHERE chirps.type = "post"
ORDER BY chirps.timestamp DESC
LIMIT :limit OFFSET :offset';
$stmt = $db->prepare($query);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->bindValue(':user_id', $currentUserId, PDO::PARAM_INT);
$stmt->execute();
$chirps = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// Sanitize output
$row['chirp'] = makeLinksClickable(nl2br(htmlspecialchars($row['chirp'])));
$row['username'] = htmlspecialchars($row['username']);
$row['name'] = htmlspecialchars($row['name']);
$row['profilePic'] = htmlspecialchars($row['profilePic']);
$row['isVerified'] = (bool)$row['isVerified'];
// Convert the interaction counts to booleans
$row['liked_by_current_user'] = (bool)$row['liked_by_current_user'];
$row['rechirped_by_current_user'] = (bool)$row['rechirped_by_current_user'];
$chirps[] = $row;
}
// Set JSON header and output
header('Content-Type: application/json');
echo json_encode($chirps);
} catch (PDOException $e) {
// Log error details for debugging purposes
error_log($e->getMessage());
http_response_code(500); // Set HTTP response code to 500 for server error
echo json_encode(['error' => 'An error occurred while fetching chirps. Please try again later.']);
}