-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththread.php
199 lines (157 loc) · 6.57 KB
/
thread.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
<?php
require('assets/simple_html_dom.php');
// Function to make URLs clickable links with link previews
function makeLinksClickable($text)
{
// Define a regular expression pattern to match URLs
$pattern = '/(https?:\/\/[^\s]+)|((www\.)?[a-zA-Z0-9.-]+\.(com|net|org|edu|be|ng|gov|int|info|io|me|co|biz|tv|tech|vercel\.app)[^\s]*)/';
// Replace URLs with clickable links
$text = preg_replace_callback($pattern, function ($matches) {
$url = $matches[0];
// Check if the URL starts with http:// or https://, and add it if missing
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
// Fetch Open Graph tags for link previews
$ogTags = getOpenGraphTags($url);
// Build the link preview HTML
$linkPreview = '<a href="' . $url . '" target="_blank" class="link-preview-box">';
$linkPreview .= '<div class="link-preview-content">';
if (!empty($ogTags['og:image'])) {
$linkPreview .= '<img src="' . $ogTags['og:image'] . '" alt="Link Preview Image">';
}
$linkPreview .= '<div class="link-preview-info">';
if (!empty($ogTags['og:title'])) {
$linkPreview .= '<strong>' . $ogTags['og:title'] . '</strong><br>';
}
// if (!empty($ogTags['og:description'])) {
// $linkPreview .= '<p>' . $ogTags['og:description'] . '</p>';
// }
$linkPreview .= '</div>'; // Close link-preview-info
$linkPreview .= '</div>'; // Close link-preview-content
$linkPreview .= '</a>'; // Close link-preview-box
// Create a link with the link preview
$link = '<a href="' . $url . '" target="_blank">' . $url . '</a>';
$link .= $linkPreview;
return $link;
}, $text);
return $text;
}
// Function to fetch Open Graph tags from a URL
function getOpenGraphTags($url)
{
$curl = curl_init($url);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => 'Mozilla/5.0',
CURLOPT_SSL_VERIFYPEER => false,
]);
$html = curl_exec($curl);
if (curl_errno($curl)) {
echo "cURL Error #" . curl_errno($curl) . ": " . curl_error($curl);
exit;
}
curl_close($curl);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$ogTags = array();
// Get all meta tags with property starting with 'og:'
$metaTags = $doc->getElementsByTagName('meta');
foreach ($metaTags as $tag) {
$property = $tag->getAttribute('property');
if (strpos($property, 'og:') === 0) {
$ogTags[$property] = $tag->getAttribute('content');
}
}
return $ogTags;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$threadsUrl = $_POST["threads-url"];
// Initialize cURL session
$curl = curl_init($threadsUrl);
// Set cURL options
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => 'Mozilla/5.0', // Set a user agent to avoid blocking
CURLOPT_SSL_VERIFYPEER => false, // Ignore SSL certificate validation
]);
// Execute the cURL request
$html = curl_exec($curl);
// Check for cURL errors
if (curl_errno($curl)) {
echo "cURL Error #" . curl_errno($curl) . ": " . curl_error($curl);
exit;
}
// Close cURL session
curl_close($curl);
// Extract @username from the Threads URL
$parsedUrl = parse_url($threadsUrl);
$path = explode('/', $parsedUrl['path']);
$username = isset($path[1]) ? $path[1] : '';
// Extract image links using regular expressions
$imageLinks = [];
$profilePictureLink = [];
$pattern = '/https?:\/\/scontent\.cdninstagram\.com\/v\/[^?]+\?[^\'"\s]*/i';
preg_match_all($pattern, $html, $matches);
if (!empty($matches[0])) {
foreach ($matches[0] as $link) {
if (strpos($link, '640x640') !== false) {
$profilePictureLink[] = $link;
} else {
$profilePictureLink[] = "https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png";
}
}
}
$pattern2 = '/https?:\/\/scontent\.cdninstagram\.com\/v\/[^\s]+/i';
preg_match_all($pattern2, $html, $matches);
if (!empty($matches[0])) {
foreach ($matches[0] as $link) {
if (strpos($link, '640x640') !== true) {
$imageLinks[] = $link;
}
}
}
// Create a Simple HTML DOM object to find the og:description meta tag
$dom = str_get_html($html);
$element = $dom->find('meta[property=og:description]', 0);
if ($element !== null) {
$text = $element->content;
// Decode HTML entities to display html entities correctly
$text = htmlspecialchars_decode($text, ENT_QUOTES);
// Make URLs clickable links with link previews
$text = makeLinksClickable($text);
// Check if there are image links and if the content has any links
$hasImageLinks = !empty($imageLinks);
$hasContentLinks = preg_match('/<a\s*[^>]*href="([^"]*)"[^>]*>.*<\/a>/i', $text);
echo "<div class='centered-text'><p>". $text . "</p></div>";
// Remove trailing double quote from $imageLinks[0]
$imageLinks[0] = rtrim($imageLinks[0], '"');
$imageLinks[0] = html_entity_decode($imageLinks[0]);
$profilePictureLink[0] = rtrim($profilePictureLink[0], '"');
$profilePictureLink[0] = html_entity_decode($profilePictureLink[0]);
// $profilePictureLinks[0] = html_entity_decode($profilePictureLink[0]);
// Proxy the image through your server
$imageContents = file_get_contents($imageLinks[0]);
$imageDataUrl = 'data:image/jpeg;base64,' . base64_encode($imageContents);
$profileContents = file_get_contents($profilePictureLink[0]);
$profileImageUrl = 'data:image/jpeg;base64,' . base64_encode($profileContents);
if ($imageLinks[0] !== $profilePictureLink[0]) {
if ($hasImageLinks && !$hasContentLinks) {
echo "<img width='50%' height='auto' src='$imageDataUrl'>";
}
}
// echo $profilePictureLinks[0];
echo "<div class='bottom-left-text'>
<div class='profile-picture-frame'><img src='$profileImageUrl' alt='Profile Picture'></div>
<p>". htmlentities($username) . "</p>
</div>";
} else {
echo "Threads page not found :(";
}
// Clean up the Simple HTML DOM object
$dom->clear();
} else {
echo "Invalid request method.";
}