<?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      Orlov Alexsey
 */

if (!defined('ACCESS')) {
    header('Location: /');
    exit;
}
global $core, $db, $adminTpl, $admin_conf, $config, $email_conf, $complaint_conf, $lang;
loadConfig('complaint');
loadLang('complaint');
require(ROOT . 'etc/email.config.php');
switch (isset($url[1]) ? $url[1] : null) {
    case 'send':
        $title = isset($_POST['title']) ? filter($_POST['title'], 'html') : '';
        $email = isset($_POST['email']) ? filter($_POST['email']) : $core->auth->user_info['email'];
        $s_group = isset($core->auth->user_info['group']) ? $core->auth->user_info['group'] : '';
        $rows = $core->row_data('groups', 'id ="' . $s_group . '"');
        $groups = isset($rows['name']) ? $rows['name'] : $lang['user_no_register'];
        $name = isset($_POST['name']) ? filter($_POST['name']) : '';
        $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : getenv('HTTP_X_FORWARDED_FOR');
        $messages = isset($_POST['message']) ? filter($_POST['message'], 'html') : '';
        if (isset($title) && !empty($messages)) {
            $core->ins_data('complaint', 'title,email,message,name,nick,ip,status,date', '"' . $title . '","' . $email . '","' . $messages . '","' . $name . '","' . $core->auth->user_info['nick'] . '","' . $ip . '", 0,"' . time() . '"');
            if ($email_conf['comp_mail'] == 1) {
                $query = $core->select_data('email', false, 'id ASC');
                while ($result = $db->getRow($query)) {
                    $message = $result['comp_mail_text'] > 0 ? str_replace(array('&lt;', '&gt;', '{%username_to%}', '{%username_from%}', '{%ip%}', '{%date%}', '{%group%}', '{%email%}', '{%text%}', '{%url%}'), array('<', '>', 'Администратор', $name . $core->auth->user_info['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 . $core->auth->user_info['nick'], $ip, formatDate(time(), true), $groups, $email, $messages, $config['url']), $result['feed_mail_text']);
                    if ($complaint_conf['allow_attach'] == 1) {
                        if (isset($_FILES['attach']) && $foo = new Upload($_FILES['attach'])) {
                            if ($complaint_conf['file_size'] >= $_FILES['attach']['size']) {
                                $fileInfo = explode('.', $_FILES['attach']['name']);
                                $fileFormat = mb_strtolower(end($fileInfo));
                                $formats = explode(',', $complaint_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['comp_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['complaintok']);
            //   header("refresh: 5; url=" . $config['url'] ."");
        } else {
            $core->tpl->info($lang['complaintfalse']);
        }

        break;
}