<?php
// обновление ботов 
if(!defined('ANTIBOT')) die('access denied');

$title = abTranslate('ChatGPT Bots');
$content = '';

// добавление/обновление ботов в белом списке:
if (isset($_POST['update_chatgpt'])) {
$ab_config['chatgpt_urls'] = [
'https://openai.com/searchbot.json', 
'https://openai.com/chatgpt-user.json', 
'https://openai.com/gptbot.json'
];

$ab_config['chatgpt_cidr'] = array();
foreach ($ab_config['chatgpt_urls'] as $ab_config['chatgpt_url']) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ab_config['chatgpt_url']);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, 'AntiBot');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
$actual = @json_decode(trim(curl_exec($ch)), true);
curl_close($ch);

$allok = 1;
if (is_array($actual['prefixes'])) {
foreach ($actual['prefixes'] as $actual['prefix']) {
$ab_config['chatgpt_cidr'][$actual['prefix']['ipv4Prefix']] = $actual['prefix']['ipv4Prefix'];
}
} else {
$allok = 0;
}
}
if ($allok == 1) {
// удаление старых:
$del = $antibot_db->exec("DELETE FROM ipv4rules WHERE comment='ChatGPT Bot';");
// добавление новых:
foreach ($ab_config['chatgpt_cidr'] as $ab_config['chatgpt_ip']) {
$AbIpRange = AbIpRange($ab_config['chatgpt_ip']);
$add = @$antibot_db->exec("INSERT INTO ipv4rules (priority, search, ip1, ip2, rule, comment, expires) VALUES ('1', '".$ab_config['chatgpt_ip']."', '".AbIp2num($AbIpRange[0])."', '".AbIp2num($AbIpRange[1])."', 'allow', 'ChatGPT Bot', '9999999999');");
}
$content .= '<div class="alert alert-success" role="alert">
'.abTranslate('Update has been successful.').'
<script>document.location.href="?'.$abw.$abp.'=rules";</script>
</div>';
} else {
$content .= '<div class="alert alert-danger" role="alert">
'.abTranslate('Update failed.').'
<script>document.location.href="?'.$abw.$abp.'=rules";</script>
</div>';
}
} elseif (isset($_POST['remove_chatgpt'])) {
// удаление старых:
$del = $antibot_db->exec("DELETE FROM ipv4rules WHERE comment='ChatGPT Bot';");
$content .= '<div class="alert alert-success" role="alert">
'.abTranslate('Update has been successful.').'
<script>document.location.href="?'.$abw.$abp.'=rules";</script>
</div>';
}
