<?php
//-----------------------------------
// Developer : @MmD_Dev
//-----------------------------------
ini_set('error_logs', 'off');
error_reporting(0);
ob_start("ob_gzhandler");
date_default_timezone_set("asia/tehran");
require_once __DIR__ . '/phpqrcode/qrlib.php';
//-----------------------------------
$botUrl = 'https://alisn.oghabhosting.ir/test/';
const Config = [
  'Token' => '8189071858:AAFZ-tJ1BxqNnPPt02FGp5rmyh5y9LWWrz0', // Token Bot
  'Channel' => '-1001884680475',
  'BotUrl' => 'https://qazii.shop/vpn/', // Url Bot
];
//-----------------------------------
function MmD_Dev($method, $datas = [])
{
  $ch = curl_init('https://api.telegram.org/bot' . Config['Token'] . '/' . $method);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
  $res = curl_exec($ch);
  curl_close($ch);
  return json_decode($res, true);
}
function Send($id, $txt, $msgid = null, $key = null)
{
  return MmD_Dev('sendMessage', [
    'chat_id' => $id,
    'text' => $txt,
    'parse_mode' => "Html",
    'message_id' => $msgid,
    'disable_web_page_preview' => true,
    'reply_markup' => $key
  ]);
}
function SendPhoto($id, $file_id, $text = null, $msgid = null, $key = null)
{
  return MmD_Dev('sendphoto', [
    'chat_id' => $id,
    'photo' => $file_id,
    'caption' => $text,
    'message_id' => $msgid,
    'disable_web_page_preview' => true,
    'reply_markup' => $key
  ]);
}
function Delete($id, $msgid)
{
  return MmD_Dev('deletemessage', [
    'chat_id' => $id,
    'message_id' => $msgid
  ]);
}
function addBorderImage($add)
{
  $border = 30;
  $im = ImageCreateFromPNG($add);
  $width = ImageSx($im);
  $height = ImageSy($im);
  $img_adj_width = $width + 2 * $border;
  $img_adj_height = $height + 2 * $border;
  $newimage = imagecreatetruecolor($img_adj_width, $img_adj_height);
  $border_color = imagecolorallocate($newimage, 255, 255, 255);
  imagefilledrectangle($newimage, 0, 0, $img_adj_width, $img_adj_height, $border_color);
  imageCopyResized($newimage, $im, $border, $border, 0, 0, $width, $height, $width, $height);
  ImagePNG($newimage, $add, 5);
}
function RandomString($count = 9, $type = "all")
{
  if ($type == "all") $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789';
  elseif ($type == "small") $characters = 'abcdef123456789';
  elseif ($type == "domain") $characters = 'abcdefghijklmnopqrstuvwxyz';

  $randstring = null;
  for ($i = 0; $i < $count; $i++) {
    $randstring .= $characters[rand(0, strlen($characters) - 1)];
  }
  return $randstring;
}
//-----------------------------------
$update = json_decode(file_get_contents('php://input'), 1);

// بررسی پیام‌های ارسالی در کانال یا چت خصوصی
if (isset($update['channel_post'])) {
  $text = $update['channel_post']['text'] ?? null;
  $chat_id = $update['channel_post']['chat']['id'] ?? null;
} elseif (isset($update['message'])) {
  $text = $update['message']['text'] ?? null;
  $chat_id = $update['message']['chat']['id'] ?? null;
}

$date = date("Y/m/d");
$time = date("H:i");

//-----------------------------------
if ($chat_id) { // بررسی وجود chat_id
  if (preg_match_all('/(vmess|vless|trojan|shadowsocks):\/\/[^➖]+/', $text, $output)) {
    foreach ($output[0] as $Qr_Link) {
      $file = RandomString() . ".png";
      $ecc = 'L';
      $pixel_Size = 16;
      $frame_Size = 1;
      QRcode::png($Qr_Link, $file, $ecc, $pixel_Size, $frame_Size);
      addBorderImage($file);
      SendPhoto($chat_id, Config['BotUrl'] . $file);
      @unlink($file);
    }
  }
}
