464 lines
13 KiB
PHP
464 lines
13 KiB
PHP
<?php
|
||
class S
|
||
{
|
||
private static function get_route_param( $var )
|
||
{
|
||
$var = trim((string) $var);
|
||
if ( $var === '' || empty($_SERVER['REQUEST_URI']) )
|
||
return null;
|
||
|
||
$uriPath = explode('?', (string) $_SERVER['REQUEST_URI'], 2)[0];
|
||
$segments = explode('/', trim($uriPath, '/'));
|
||
|
||
foreach ( $segments as $segment )
|
||
{
|
||
if ( strpos($segment, '=') === false )
|
||
continue;
|
||
|
||
list($key, $value) = explode('=', $segment, 2);
|
||
if ( trim((string) $key) === $var )
|
||
return urldecode((string) $value);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
private static function get_rewrite_tail_param( $var )
|
||
{
|
||
$var = trim((string) $var);
|
||
$queryString = (string) ( $_SERVER['QUERY_STRING'] ?? '' );
|
||
|
||
if ( $var === '' || $queryString === '' )
|
||
return null;
|
||
|
||
$parts = explode('/', $queryString);
|
||
foreach ( $parts as $part )
|
||
{
|
||
if ( strpos($part, '=') === false )
|
||
continue;
|
||
|
||
list($key, $value) = explode('=', $part, 2);
|
||
if ( trim((string) $key) === $var )
|
||
return urldecode((string) $value);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
private static function log_mail_event( $level, $message, $context = [] )
|
||
{
|
||
global $settings;
|
||
|
||
if ( empty($settings['mail_debug_log_enabled']) )
|
||
return;
|
||
|
||
$rootPath = dirname(__DIR__);
|
||
$logFile = $rootPath . DIRECTORY_SEPARATOR . 'mail_debug.log';
|
||
|
||
$time = date('Y-m-d H:i:s');
|
||
$line = '[' . $time . '] [' . strtoupper((string) $level) . '] ' . (string) $message;
|
||
|
||
if ( is_array( $context ) && !empty( $context ) )
|
||
{
|
||
$line .= ' | ' . json_encode( $context, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
|
||
}
|
||
|
||
$line .= PHP_EOL;
|
||
$written = @file_put_contents( $logFile, $line, FILE_APPEND );
|
||
|
||
if ( $written === false )
|
||
{
|
||
if ( !empty($settings['mail_debug_error_log_enabled']) )
|
||
error_log( 'Mail log write error: ' . $logFile . ' | ' . $line );
|
||
}
|
||
}
|
||
|
||
public static function array_unique_multi( $array, $key )
|
||
{
|
||
$temp_array = [];
|
||
foreach ($array as &$v) {
|
||
if (!isset($temp_array[$v[$key]]))
|
||
$temp_array[$v[$key]] =& $v;
|
||
}
|
||
$array = array_values($temp_array);
|
||
return $array;
|
||
|
||
}
|
||
|
||
public function number_display( $value )
|
||
{
|
||
return number_format( (float) $value, 2, ',', ' ' ) . ' zł';
|
||
}
|
||
|
||
public static function prepar_request( $query )
|
||
{
|
||
if ( is_array( $query ) && !empty( $query ) )
|
||
{
|
||
foreach ( $query as $key => $value )
|
||
$query[$key] = urlencode( $key ) . '=' . urlencode( $value );
|
||
return implode( '&', $query );
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
public static function seo( $val, $delete_rhombs = false )
|
||
{
|
||
$val = (string) $val;
|
||
$array_rep1 = array( '*', '_', ' ', '+', '"', "'", '?', '-', ',', '!', '~', '<', '>', '@', '#', '$', '%', '^', '&', '*' . '(', ')' . '-', '=', '\\', '|', '[', ']', ':', '(', ')' );
|
||
$array_rep2 = array( '-', '-', '-', '-', '', '', '', '-', '-', '', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '', '-', '-', '-', '-', '-', '-', '-', '-' );
|
||
|
||
$val = self::noPl( $val );
|
||
$val = str_replace( $array_rep1, $array_rep2, $val );
|
||
$val = strtolower( $val );
|
||
$val = preg_replace( '/(-){2,}/', '-', $val );
|
||
if ( $delete_rhombs )
|
||
$val = str_replace( '/', '', $val );
|
||
|
||
return $val;
|
||
}
|
||
|
||
static public function no_pl_excel( $val )
|
||
{
|
||
$val = (string) $val;
|
||
$table = Array(
|
||
"\xc4\x85" => "a", "\xc4\x84" => "A", "\xc4\x87" => "c", "\xc4\x86" => "C",
|
||
"\xc4\x99" => "e", "\xc4\x98" => "E", "\xc5\x82" => "l", "\xc5\x81" => "L",
|
||
"\xc3\xb3" => "o", "\xc3\x93" => "O", "\xc5\x9b" => "s", "\xc5\x9a" => "S",
|
||
"\xc5\xbc" => "z", "\xc5\xbb" => "Z", "\xc5\xba" => "z", "\xc5\xb9" => "Z",
|
||
"\xc5\x84" => "n", "\xc5\x83" => "N"
|
||
);
|
||
|
||
$val = strtr( $val, $table );
|
||
|
||
return $val;
|
||
}
|
||
|
||
public static function noPL( $val )
|
||
{
|
||
$val = (string) $val;
|
||
$table = array(
|
||
"А" => "a", "Б" => "b", "В" => "v", "Г" => "g", "Д" => "d",
|
||
"Е" => "e", "Ё" => "yo", "Ж" => "zh", "З" => "z", "И" => "i",
|
||
"Й" => "j", "К" => "k", "Л" => "l", "М" => "m", "Н" => "n",
|
||
"О" => "o", "П" => "p", "Р" => "r", "С" => "s", "Т" => "t",
|
||
"У" => "u", "Ф" => "f", "Х" => "kh", "Ц" => "ts", "Ч" => "ch",
|
||
"Ш" => "sh", "Щ" => "sch", "Ъ" => "", "Ы" => "y", "Ь" => "",
|
||
"Э" => "e", "Ю" => "yu", "Я" => "ya", "а" => "a", "б" => "b",
|
||
"в" => "v", "г" => "g", "д" => "d", "е" => "e", "ё" => "yo",
|
||
"ж" => "zh", "з" => "z", "и" => "i", "й" => "j", "к" => "k",
|
||
"л" => "l", "м" => "m", "н" => "n", "о" => "o", "п" => "p",
|
||
"р" => "r", "с" => "s", "т" => "t", "у" => "u", "ф" => "f",
|
||
"х" => "kh", "ц" => "ts", "ч" => "ch", "ш" => "sh", "щ" => "sch",
|
||
"ъ" => "", "ы" => "y", "ь" => "", "э" => "e", "ю" => "yu",
|
||
"я" => "ya", " " => "-", "." => "", "," => "",
|
||
":" => "", ";" => "", "—" => "", "–" => "-"
|
||
);
|
||
|
||
$val = strtr( $val, $table );
|
||
|
||
$val = iconv( 'UTF-8', 'ASCII//TRANSLIT', $val );
|
||
|
||
$table = Array(
|
||
"\xc4\x85" => "a", "\xc4\x84" => "A", "\xc4\x87" => "c", "\xc4\x86" => "C",
|
||
"\xc4\x99" => "e", "\xc4\x98" => "E", "\xc5\x82" => "l", "\xc5\x81" => "L",
|
||
"\xc3\xb3" => "o", "\xc3\x93" => "O", "\xc5\x9b" => "s", "\xc5\x9a" => "S",
|
||
"\xc5\xbc" => "z", "\xc5\xbb" => "Z", "\xc5\xba" => "z", "\xc5\xb9" => "Z",
|
||
"\xc5\x84" => "n", "\xc5\x83" => "N"
|
||
);
|
||
|
||
$val = strtr( $val, $table );
|
||
|
||
return $val;
|
||
}
|
||
|
||
public static function alert( $text )
|
||
{
|
||
\S::set_session( 'alert', $text );
|
||
}
|
||
|
||
public static function hash( $qtd )
|
||
{
|
||
$Caracteres = 'ABCDEFGHIJKLMOPQRSTUVXWYZ0123456789';
|
||
$QuantidadeCaracteres = strlen($Caracteres);
|
||
$QuantidadeCaracteres--;
|
||
|
||
$Hash =NULL;
|
||
for ( $x = 1; $x <= $qtd; $x++ )
|
||
{
|
||
$Posicao = rand( 0, $QuantidadeCaracteres );
|
||
$Hash .= substr( $Caracteres, $Posicao, 1 );
|
||
}
|
||
|
||
return $Hash;
|
||
}
|
||
|
||
public static function sort_array_of_array( &$array, $subfield, $type = SORT_ASC )
|
||
{
|
||
$sortarray = array();
|
||
foreach ( $array as $key => $row )
|
||
$sortarray[$key] = $row[$subfield];
|
||
|
||
array_multisort( $sortarray, $type, $array );
|
||
}
|
||
|
||
public static function json_to_array( $json )
|
||
{
|
||
$values = [];
|
||
$values_tmp = json_decode( $json, true );
|
||
|
||
if ( is_array( $values_tmp ) ) foreach ( $values_tmp as $val )
|
||
{
|
||
if ( isset( $values[ $val['name'] ] ) )
|
||
{
|
||
if ( is_array( $values[ $val['name'] ] ) )
|
||
$values[ $val['name'] ][] = $val['value'];
|
||
else
|
||
$values[ $val['name'] ] = array( $values[ $val['name'] ], $val['value'] );
|
||
}
|
||
else
|
||
$values[ $val['name'] ] = $val['value'];
|
||
}
|
||
return $values;
|
||
}
|
||
|
||
public static function get_session( $var )
|
||
{
|
||
return $_SESSION[ $var ] ?? null;
|
||
}
|
||
|
||
public static function del_session( $var ) {
|
||
unset( $_SESSION[ $var ] );
|
||
}
|
||
|
||
public static function set_session( $var, $val )
|
||
{
|
||
$_SESSION[ $var ] = $val;
|
||
}
|
||
|
||
public static function get( $var )
|
||
{
|
||
if ( isset( $_POST[ $var ] ) )
|
||
{
|
||
return $_POST[ $var ];
|
||
}
|
||
if ( isset( $_GET[ $var ] ) )
|
||
{
|
||
$value = $_GET[ $var ];
|
||
|
||
if ( is_string($value) && strpos($value, '/') !== false )
|
||
{
|
||
$segments = explode('/', $value);
|
||
$head = trim((string) ($segments[0] ?? ''));
|
||
if ( $head !== '' )
|
||
return urldecode($head);
|
||
}
|
||
|
||
return $value;
|
||
}
|
||
|
||
$routeValue = self::get_route_param( $var );
|
||
if ( $routeValue !== null )
|
||
{
|
||
return $routeValue;
|
||
}
|
||
|
||
$rewriteTailValue = self::get_rewrite_tail_param( $var );
|
||
if ( $rewriteTailValue !== null )
|
||
{
|
||
return $rewriteTailValue;
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
public static function pre( $data , $type = '' )
|
||
{
|
||
$data = str_replace( 'Array
|
||
(' , '' , $data );
|
||
$data = str_replace( ')' , '' , $data );
|
||
|
||
echo '<pre';
|
||
if ( $type == 'error' )
|
||
echo ' style="color: #cc0000;" ';
|
||
else if ( $type == 'info' )
|
||
echo ' style="color: #2c539e;" ';
|
||
else
|
||
echo ' style="color: #8fc400;" ';
|
||
echo '>' . print_r( $data , true ) . '</pre>';
|
||
}
|
||
|
||
public static function email_check( $email )
|
||
{
|
||
return filter_var( $email, FILTER_VALIDATE_EMAIL );
|
||
}
|
||
|
||
private static function normalize_attachments( $file )
|
||
{
|
||
$attachments = [];
|
||
|
||
if ( is_array( $file ) )
|
||
{
|
||
foreach ( $file as $file_tmp )
|
||
{
|
||
if ( !empty( $file_tmp ) && file_exists( $file_tmp ) )
|
||
$attachments[] = $file_tmp;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if ( !empty( $file ) && file_exists( $file ) )
|
||
$attachments[] = $file;
|
||
}
|
||
|
||
return $attachments;
|
||
}
|
||
|
||
private static function send_email_native( $email, $subject, $text, $attachments, $settings )
|
||
{
|
||
$from = trim((string) ($settings['email_from'] ?? 'bilety@brzezovka.pl'));
|
||
$replyTo = trim((string) ($settings['email_reply_to'] ?? $from));
|
||
$bccEmail = trim((string) ($settings['email_bcc'] ?? ''));
|
||
|
||
$headers = [];
|
||
$headers[] = 'From: ' . $from;
|
||
$headers[] = 'Reply-To: ' . $replyTo;
|
||
if ( $bccEmail !== '' && self::email_check( $bccEmail ) )
|
||
$headers[] = 'Bcc: ' . $bccEmail;
|
||
$headers[] = 'MIME-Version: 1.0';
|
||
|
||
$encodedSubject = '=?UTF-8?B?' . base64_encode((string) $subject) . '?=';
|
||
|
||
if ( empty($attachments) )
|
||
{
|
||
$headers[] = 'Content-Type: text/html; charset=UTF-8';
|
||
$ok = @mail($email, $encodedSubject, (string) $text, implode("\r\n", $headers));
|
||
return (bool) $ok;
|
||
}
|
||
|
||
$boundary = 'boundary_' . md5(uniqid((string) mt_rand(), true));
|
||
$headers[] = 'Content-Type: multipart/mixed; boundary="' . $boundary . '"';
|
||
|
||
$body = '';
|
||
$body .= '--' . $boundary . "\r\n";
|
||
$body .= "Content-Type: text/html; charset=UTF-8\r\n";
|
||
$body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
|
||
$body .= (string) $text . "\r\n";
|
||
|
||
foreach ( $attachments as $attachment )
|
||
{
|
||
$content = @file_get_contents($attachment);
|
||
if ( $content === false )
|
||
continue;
|
||
|
||
$filename = basename($attachment);
|
||
$body .= '--' . $boundary . "\r\n";
|
||
$body .= 'Content-Type: application/octet-stream; name="' . $filename . '"' . "\r\n";
|
||
$body .= "Content-Transfer-Encoding: base64\r\n";
|
||
$body .= 'Content-Disposition: attachment; filename="' . $filename . '"' . "\r\n\r\n";
|
||
$body .= chunk_split(base64_encode($content)) . "\r\n";
|
||
}
|
||
|
||
$body .= '--' . $boundary . "--\r\n";
|
||
|
||
$ok = @mail($email, $encodedSubject, $body, implode("\r\n", $headers));
|
||
return (bool) $ok;
|
||
}
|
||
|
||
public static function send_email( $email, $subject, $text, $file = '' )
|
||
{
|
||
global $settings;
|
||
|
||
$email = trim((string) $email);
|
||
if ( !self::email_check( $email ) )
|
||
{
|
||
self::log_mail_event( 'error', 'Mail send aborted - invalid recipient', [
|
||
'to' => $email,
|
||
'subject' => $subject
|
||
] );
|
||
return false;
|
||
}
|
||
|
||
$attachments = self::normalize_attachments( $file );
|
||
|
||
$mail = new PHPMailer;
|
||
$mail -> IsSMTP();
|
||
$mail -> SMTPAuth = true;
|
||
$mail -> Host = $settings['email_host'];
|
||
$mail -> Port = $settings['email_port'];
|
||
$mail -> Username = $settings['email_login'];
|
||
$mail -> Password = $settings['email_password'];
|
||
$mail -> CharSet = "UTF-8";
|
||
$mail -> SMTPOptions = array(
|
||
'ssl' => array(
|
||
'verify_peer' => false,
|
||
'verify_peer_name' => false,
|
||
'allow_self_signed' => true
|
||
)
|
||
);
|
||
$mail -> From = 'bilety@brzezovka.pl';
|
||
$mail -> FromName = 'KOMPLEKS TURYSTYCZNY BRZEZÓVKA';
|
||
$mail -> addAddress( $email, $email );
|
||
$mail -> addReplyTo( 'bilety@brzezovka.pl', 'KOMPLEKS TURYSTYCZNY BRZEZÓVKA' );
|
||
$mail -> isHTML( true );
|
||
|
||
$bccEmail = trim((string) ($settings['email_bcc'] ?? ''));
|
||
if ( $bccEmail !== '' && self::email_check( $bccEmail ) )
|
||
{
|
||
$mail -> addBCC( $bccEmail );
|
||
}
|
||
|
||
$mail -> Subject = $subject;
|
||
$mail -> Body = $text;
|
||
foreach ( $attachments as $attachment )
|
||
$mail -> AddAttachment( $attachment );
|
||
|
||
self::log_mail_event( 'info', 'Mail send attempt', [
|
||
'to' => $email,
|
||
'subject' => $subject,
|
||
'attachments' => $attachments
|
||
] );
|
||
|
||
$isSent = $mail -> send();
|
||
|
||
if (!$isSent) {
|
||
self::log_mail_event( 'error', 'Mail send failed', [
|
||
'to' => $email,
|
||
'subject' => $subject,
|
||
'error' => $mail->ErrorInfo
|
||
] );
|
||
if ( !empty($settings['mail_debug_error_log_enabled']) )
|
||
error_log('Mail send error to ' . $email . ': ' . $mail->ErrorInfo);
|
||
|
||
self::log_mail_event( 'info', 'Mail send fallback attempt (native mail)', [
|
||
'to' => $email,
|
||
'subject' => $subject
|
||
] );
|
||
|
||
$fallbackSent = self::send_email_native( $email, $subject, $text, $attachments, $settings );
|
||
if ( $fallbackSent )
|
||
{
|
||
self::log_mail_event( 'info', 'Mail send fallback success (native mail)', [
|
||
'to' => $email,
|
||
'subject' => $subject
|
||
] );
|
||
return true;
|
||
}
|
||
|
||
$lastError = error_get_last();
|
||
self::log_mail_event( 'error', 'Mail send fallback failed (native mail)', [
|
||
'to' => $email,
|
||
'subject' => $subject,
|
||
'error' => is_array($lastError) ? ($lastError['message'] ?? '') : ''
|
||
] );
|
||
} else {
|
||
self::log_mail_event( 'info', 'Mail send success', [
|
||
'to' => $email,
|
||
'subject' => $subject
|
||
] );
|
||
}
|
||
|
||
return $isSent;
|
||
}
|
||
}
|