<?php
function GetCacheID() {
global $db;
$SQL = "INSERT INTO " . TABLE_PREFIX . "y2ksw_imgcache2 (ID) VALUES (0)";
$db->query_write($SQL);
return $db->insert_id();
}
function DownloadImage($url) {
if (empty($url)) return false;
$context = stream_context_create([
'http' => [
'header' => "User-Agent: Mozilla/5.0\r\n"
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
]);
$data = @file_get_contents($url, false, $context);
if ($data === false || strlen($data) < 100) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_TIMEOUT => 20,
CURLOPT_USERAGENT => 'Mozilla/5.0',
CURLOPT_HTTPHEADER => [
'Accept: image/webp,image/apng,image/*,*/*;q=0.8',
'Referer: ' . $url
]
]);
$data = curl_exec($ch);
curl_close($ch);
}
return $data;
}
function CacheImages($message_body, $old_message, $forum_home) {
$files = [];
$n = 0;
$pos_end = -1;
while (($pos_start = strpos(strtolower($message_body), '[img]', $pos_end + 1)) !== false) {
$pos_end = strpos(strtolower($message_body), '[/img]', $pos_start + 1);
if ($pos_end === false) break;
$pos_start += 5;
$pos_end--;
$url = trim(substr($message_body, $pos_start, $pos_end - $pos_start + 1));
$files[$n]['url'] = $url;
$n++;
}
if (!$n) return $old_message;
foreach ($files as $i => $file) {
if (empty($file['url'])) continue;
if (!empty($forum_home) && strpos($file['url'], $forum_home . 'imgcache/2/') !== false) {
$files[$i]['url'] = '';
continue;
}
$matar = strtolower($file['url']);
if (preg_match("/alshafeen.site|alsh3er.com/i", $matar)) {
$files[$i]['url'] = '';
continue;
}
}
for ($i = 0; $i < $n; $i++) {
if (empty($files[$i]['url'])) continue;
for ($j = $i + 1; $j < $n; $j++) {
if ($files[$j]['url'] == $files[$i]['url']) {
$files[$j]['url'] = '';
}
}
}
foreach ($files as $file) {
if (empty($file['url'])) continue;
$content = DownloadImage($file['url']);
if (!$content) continue;
$tmp = tempnam(sys_get_temp_dir(), "img_");
file_put_contents($tmp, $content);
$info = @getimagesize($tmp);
$is_webp = false;
if ($info && isset($info['mime']) && $info['mime'] == 'image/webp') {
$is_webp = true;
}
if (!$info && strpos($file['url'], '.webp') !== false) {
$is_webp = true;
}
$id = GetCacheID();
$new_file = 'imgcache/2/' . $id . 'alsh3er.jpg';
if ($is_webp) {
$img = @imagecreatefromstring(file_get_contents($tmp));
if ($img) {
imagejpeg($img, $new_file, 90);
imagedestroy($img);
} else {
copy($tmp, $new_file);
}
} else {
copy($tmp, $new_file);
}
unlink($tmp);
AddWatermark($new_file, "alsh3er.site");
$old_message = str_replace($file['url'], $forum_home . $new_file, $old_message);
}
return $old_message;
}
function AddWatermark($imagePath, $text) {
$data = @file_get_contents($imagePath);
if (!$data) return;
$img = @imagecreatefromstring($data);
if (!$img) return;
$color = imagecolorallocatealpha($img, 255, 255, 255, 50);
imagestring(
$img,
3,
imagesx($img) - 120,
imagesy($img) - 15,
$text,
$color
);
imagejpeg($img, $imagePath, 90);
imagedestroy($img);
}
?>