<?php

/*
 * @name        JMY CMS
 * @link        https://jmy.su/
 * @copyright   Copyright (C) 2012-2025 JMY LTD
 * @license     LICENSE.txt (see attached file)
 * @version     VERSION.txt (see attached file)
 * @author      Komarov Ivan
 * @edited      Orlov Alexsey
 */

if (!defined('ACCESS')) {
    header('Location: /');
    exit;
}
global $core, $db, $adminTpl, $admin_conf, $config, $email_conf, $feedback_conf, $lang;
loadConfig('feedback');
loadLang('feedback');
require(ROOT . 'etc/email.config.php');
if (!empty($feedback_conf['keywords'])) $core->tpl->keywords = $feedback_conf['keywords'];
if (!empty($feedback_conf['description'])) $core->tpl->description = $feedback_conf['description'];
switch ($url[1] ?? null) {
    default:
        set_title(array($lang['feedback']));
        $core->tpl->loadFile('feedback');
        $core->tpl->setVar('CAPTCHA', captcha_image());
        $core->tpl->setVar('FILE_SIZE', formatfilesize($feedback_conf['file_size']));
        $core->tpl->setVar('FORMATS', $feedback_conf['formats']);
        $core->tpl->sources = preg_replace_callback("#\\[load_attach](.*?)\\[/load_attach]#is", function ($matches) use ($feedback_conf) {
            if ($feedback_conf['allow_attach']) return $matches[1];
        }, $core->tpl->sources);
        $core->tpl->end();
        break;
    case 'send':
        set_title(array($lang['feedback'] . $config['divider'] . $lang['sendingmess']));
        if (captcha_check('securityCode')) {
            $title = isset($_POST['title']) ? filter($_POST['title'], 'html') : '';
            $email = isset($_POST['email']) ? filter($_POST['email']) : $core->auth->user_info['email'];
            $s_group = $core->auth->user_info['group'] ?? '';
            $rows = $core->row_data('groups', 'id ="' . $s_group . '"');
            $groups = $rows['name'] ?? $lang['user_no_register'];
            $name = isset($_POST['name']) ? filter($_POST['name']) : '';
            $nick = $core->auth->user_info['nick'] ?? '';
            $ip = $_SERVER['REMOTE_ADDR'] ?? getenv('HTTP_X_FORWARDED_FOR');
            $messages = isset($_POST['message']) ? filter($_POST['message'], 'html') : '';
            if (isset($title) && !empty($messages)) {
                $core->ins_data('feedback', 'title,email,message,name,nick,ip,status,date', '"' . $title . '","' . $email . '","' . $messages . '","' . $name . '","' . $nick . '","' . $ip . '", 0,"' . time() . '"');
                if ($email_conf['feed_mail'] == 1) {
                    $query = $core->select_data('email', false, 'id ASC');
                    while ($result = $db->getRow($query)) {
                        $message = $result['feed_mail_text'] > 0 ? str_replace(array('&lt;', '&gt;', '{%username_to%}', '{%username_from%}', '{%ip%}', '{%date%}', '{%group%}', '{%email%}', '{%text%}', '{%url%}'), array('<', '>', 'Администратор', $name . $nick, $ip, formatDate(time(), true), $groups, $email, $messages, $config['url']), $result['feed_mail_text']) : str_replace(array('\p', '\n', '\r', '<br>', '<br/>', '&nbsp;', '{%username_to%}', '{%username_from%}', '{%ip%}', '{%date%}', '{%group%}', '{%email%}', '{%text%}', '{%url%}'), array('""', '""', '""', '\n', '\n', '\n', 'Администратор', $name . $nick, $ip, formatDate(time(), true), $groups, $email, $messages, $config['url']), $result['feed_mail_text']);
                        if ($feedback_conf['allow_attach'] == 1) {
                            if (isset($_FILES['attach']) && $foo = new Upload($_FILES['attach'])) {
                                if ($feedback_conf['file_size'] >= $_FILES['attach']['size']) {
                                    $fileInfo = explode('.', $_FILES['attach']['name']);
                                    $fileFormat = mb_strtolower(end($fileInfo));
                                    $formats = explode(',', $feedback_conf['formats']);
                                    if (in_array($fileFormat, $formats)) {
                                        $file_content = $foo->Process();
                                        if ($foo->processed) {
                                            $foo->Clean();
                                            $message .= "content-type: application/octet-stream;";
                                            $message .= 'name="' . basename($_FILES['attach']['name']) . "\n";
                                            $message .= "content-transfer-encoding:base64\n";
                                            $message .= "content-disposition:attachment;\n\n";
                                            $message .= chunk_split(base64_encode($file_content)) . "\n";
                                        }
                                    }
                                }
                            }
                        }
                        if (!empty($title)) {
                            mailSMTP($config['support_mail'], $result['feed_mail_text'], $title, $message);
                        } else {
                            $core->tpl->info($lang['sendfalse'] . '   <strong><a href="javascript:history.go(-1)"><b>' . $lang['go_back'] . '</b></a></strong>');
                        }
                    }
                }
                $core->tpl->info($lang['sendok']);
                header("refresh: 5; url=" . $config['url']);
            } else {
                $core->tpl->info($lang['sendfalse']);
            }
        } else {
            $core->tpl->info($lang['captchafalse']);
        }
        break;
}