svrjs-mods-directory/includes/utils.php
2024-12-27 15:05:54 +01:00

159 lines
3.9 KiB
PHP

<?php
if (!defined('SVRJS_MOD_DIRECTORY')) die;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
function sendEmail($recipients, $subject, $message)
{
// If no recipients, return true
if (count($recipients) == 0) return true;
$mail = new PHPMailer(false);
// Email server data (SMTP or PHP mail function)
if (EMAIL_SMTP) {
$mail->isSMTP();
$mail->Host = EMAIL_SMTP_HOST;
if (EMAIL_SMTP_AUTHENTICATED) {
$mail->SMTPAuth = true;
$mail->Username = EMAIL_SMTP_USERNAME;
$mail->Password = EMAIL_SMTP_PASSWORD;
}
if (EMAIL_SMTP_ENCRYPTION == 1) {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
} elseif (EMAIL_SMTP_ENCRYPTION == 2) {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
}
$mail->Port = EMAIL_SMTP_PORT;
} else {
$mail->isMail();
}
// Email sender and recipient
$mail->setFrom(EMAIL_FROM, EMAIL_FROM_NAME);
foreach ($recipients as $recipient) {
if (isset($recipient['name'])) $mail->addAddress($recipient['address'], $recipient['name']);
else $mail->addAddress($recipient['address']);
}
// Email message
$mail->Subject = $subject;
$mail->Body = $message;
return $mail->send();
}
function shortenDescription($description)
{
if (strlen($description) <= 160) {
return $description;
} else {
return substr($description, 0, 157) . '...';
}
}
function formatFileSize($size)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$formattedSize = $size;
for ($i = 0; $size >= 1024 && $i < count($units) - 1; $i++) {
$size /= 1024;
$formattedSize = round($size, 2);
}
return $formattedSize . $units[$i];
}
function checkHCaptcha($hCaptchaResponse)
{
if (!HCAPTCHA_ENABLED) return true;
$data = array(
'secret' => HCAPTCHA_SECRET_KEY,
'response' => $hCaptchaResponse
);
$verify = curl_init();
if (!$verify) {
return false;
}
curl_setopt($verify, CURLOPT_URL, 'https://hcaptcha.com/siteverify');
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);
if (!$response) {
return false;
}
$responseData = json_decode($response, true);
if ($responseData && $responseData['success']) {
return true;
} else {
return false;
}
}
function checkStopForumSpam($username, $email)
{
if (!STOPFORUMSPAM_ENABLED) return true;
$data = array(
'username' => $username,
'email' => $email,
'ip' => $_SERVER['REMOTE_ADDR'],
'json'
);
$ch = curl_init();
if (!$ch) {
return null;
}
curl_setopt($ch, CURLOPT_URL, 'http://api.stopforumspam.org/api');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (!$response) {
return null;
}
$responseData = json_decode($response, true);
if ($responseData && $responseData['success']) {
return !(
(STOPFORUMSPAM_CHECK_USERNAME && $responseData && $responseData['username'] && $responseData['username']['appears'] && $responseData['username']['frequency'] >= STOPFORUMSPAM_THRESHOLD) ||
(STOPFORUMSPAM_CHECK_EMAIL && $responseData && $responseData['email'] && $responseData['email']['appears'] && $responseData['email']['frequency'] >= STOPFORUMSPAM_THRESHOLD) ||
(STOPFORUMSPAM_CHECK_IP && $responseData && $responseData['ip'] && $responseData['ip']['appears'] && $responseData['ip']['frequency'] >= STOPFORUMSPAM_THRESHOLD)
);
} else {
return null;
}
}
function setupHeaders()
{
// Disable caching
header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");
header("Pragma: no-cache");
if (COMPRESSION_ENABLED) {
header("Vary: Accept-Encoding, Cookie");
} else {
header("Vary: Cookie");
}
// Remove "Expires" header
header_remove("Expires");
}