<?php

require_once('../system/bootstrap.php');

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

/** @var PDO $db */
$db = $container->get(PDO::class);

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


$idbot = $config['bot_id'];
$botname = $config['bot_name']; 
$total = $db->query("SELECT COUNT(*) FROM `bot`;")->fetchColumn();
if($total > 0) { 
    $req = $db->query("SELECT * FROM `bot` 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); 
            $list = array($txt, $txt1, $txt2, $txt3); 
            $rtxt = rand(0, 5); 
            if(preg_match('|'.$key.'|', $txtcheck)) { 
                $bot = $list[$rtxt]; 
            } 
        } 
    } 
    $bot = str_replace('{user}', $systemUser->name, $bot); 
    $bot = str_replace('{text}', $msg, $bot); 
    $bot = str_replace('{reply}', '[quote='.$systemUser->name.']'.$msg.'[/quote]', $bot); 
    $time = time()+5; 
    if($bot) { 
        $db->query("INSERT INTO `guest` SET 
            `adm` = '$admset', 
            `time` = '$time', 
            `user_id` = '$idbot', 
            `name` = '$botname', 
            `text` = " . $db->quote($bot) . ", 
            `ip` = 'gratisan cuy', 
            `browser` = 'gratisan cuy' 
        "); 
    } 
}