<?php
/*
 * JohnCMS NEXT Mobile Content Management System (http://johncms.com)
 *
 * For copyright and license information, please see the LICENSE.md
 * Installing the system or redistributions of files must retain the above copyright notice.
 *
 * @link        http://johncms.com JohnCMS Project
 * @copyright   Copyright (C) JohnCMS Community
 * @license     GPL-3
 */

defined('_IN_JOHNCMS') or die('Error: restricted access');

/** @var Psr\Container\ContainerInterface $container */
$container = App::getContainer();

/** @var Johncms\Api\UserInterface $systemUser */
$systemUser = $container->get(Johncms\Api\UserInterface::class);

/** @var Johncms\Api\BbcodeInterface $bbcode */
$bbcode = $container->get(Johncms\Api\BbcodeInterface::class);


/** @var Johncms\Api\ToolsInterface $tools */
$tools = $container->get(Johncms\Api\ToolsInterface::class);

/** @var Johncms\Api\ConfigInterface $config */
$config = $container->get(Johncms\Api\ConfigInterface::class);

/** @var Johncms\Api\EnvironmentInterface $config */
$env = $container->get(Johncms\Api\EnvironmentInterface::class);

/** @var Zend\I18n\Translator\Translator $translator */
$translator = $container->get(Zend\I18n\Translator\Translator::class);
$translator->addTranslationFilePattern('gettext', __DIR__ . '/locale', '/%s/default.mo');

$idbot = $config['botidc'];
$botnamec = $config['botnamec'];
$from = $systemUser->id ? $systemUser->name : $db->quote($name);
$total = $db->query("SELECT COUNT(*) FROM `guard`;")->fetchColumn();
if($total > 0) {
$req = $db->query("SELECT * FROM `guard` ORDER BY `id` LIMIT $total");
while ($check = $req->fetch()) {
for ($i=1; $i<=$total; $i++) {
$key = strtolower($check['key']);
$txtcheck = strtolower($msg);
$txt = $check['text'];
$txt1 = ($check['txt1'] ? $check['txt1'] : $txt);
$txt2 = ($check['txt2'] ? $check['txt2'] : $txt);
$txt3 = ($check['txt3'] ? $check['txt3'] : $txt);
$txt4 = ($check['txt4'] ? $check['txt4'] : $txt);
$txt5 = ($check['txt5'] ? $check['txt5'] : $txt);
$list = array($txt, $txt1, $txt2, $txt3, $txt4, $txt5);
$rtxt = rand(0, 5);
if(preg_match('|'.$key.'|', $txtcheck)) {
$bot = $list[$rtxt];
}
}
}
$bot = str_replace('{user}', $from, $bot);
$bot = str_replace('{text}', $msg, $bot);
$bot = str_replace('{reply}', '[quote='.$from.']'.$msg.'[/quote]', $bot);
$time = time()+5;
if($bot) {
$db->prepare("INSERT INTO `guest` SET
`adm` = ?,
`time` = ?,
`user_id` = ?,
`name` = ?,
`text` = ?,
`ip` = ?,
`browser` = ?,
`otvet` = ''
")->execute([
$admset,
$time,
$idbot,
$botnamec,
$bot,
$env->getIp(),
$env->getUserAgent(),
]);
}
}
?>