<commit_before><?php
set_time_limit(60);
date_default_timezone_set('Asia/Taipei');
header('Accept: application/json');

include_once('config.php');

$cmd_list = array(
    'ping',
    'traceroute',
    'nslookup',
    'whois',
    'test',
    'help',
    'moo',
    'pull',
    'status',
    'log'
);

// Get telegram data
$json = file_get_contents('php://input') . PHP_EOL;
$data = json_decode($json, true);

// Log it
$time = date('Y-m-d H:i:s', time());
logging("<" .$time . "> Hook invoked\n");
logging($data);

$updateID = $data['update_id'];
$messageID = $data['message']['message_id'];
$fromID = $data['message']['from']['id'];
$chatID = $data['message']['chat']['id'];
$date = $data['message']['date'];
$userName = $data['message']['from']['username'];
$message = $data['message']['text'];

if($userName != ""){
    if(substr($message, 0, 1) == "/"){
        if( true ) { // For user check

            $cmd = str_replace('@'.BOT_NAME, '', $message);
            $message = strtolower($message);
            logging($cmd);
            $cmd = split(' ', $cmd);

            switch ($cmd[0]) {
                case "/ping":
                    if(count($cmd) == 2){
                        ping($cmd[1]);  
                    }else{
                        error(4);
                    }
                    break;

                /*case "/ping6":
                    if(count($cmd) == 2){
                        ping6($cmd[1]); 
                    }else{
                        error(4);
                    }
                    break;*/

                case "/traceroute":
                    if(count($cmd) == 2){
                        traceroute($cmd[1]);
                    }else{
                        error(4);
                    }
                    break;

                /*case "/traceroute6":
                    if(count($cmd) == 2){
                        traceroute6($cmd[1]);
                    }else{
                        error(4);
                    }
                    break;*/
                
                case "/nslookup":
                    if(count($cmd) == 3){
                        nslookup($cmd[1], $cmd[2]);
                    }if(count($cmd) == 2){
                        nslookup($cmd[1]);
                    }else{
                        error(4);
                    }
                    break;

                case "/whois":
                    if(count($cmd) == 2){
                        whois($cmd[1]);
                    }else{
                        error(4);
                    }
                    break;

                case "/test":
                    test();
                    break;

                case "/help":
                    help($cmd_list);
                    break;

                /*case "/search":
                    if(count($cmd) == 3){
                        search($cmd[1], $cmd[2]);
                    }else{
                        error(4);
                    }
                    break;*/

                case "/moo":
		            moo();
                    break;

                case "/pull":
                    git("pull github master");
                    break;

                case "/status":
                    git("status");
                    break;

                case "/log":
                    git('log --pretty=format:"%h - %an, %ar : %s"');
                    break;

                default:
                    if(strpos($message, "@".BOT_NAME)){
                        sendMsg("我沒這指令, 你想做什麼??");
                    }
                    //error(3);
                    break;
            }

        }else{
            error(2);
        }
    }else{
        if(strpos($message, "@".BOT_NAME) !== false){
            sendMsg("嗨~ Tag 我幹嘛?");
        }
    }
}

function run_shell_cmd($cmd, $param, $do_sprint = true) {

    if($do_sprint)
        $cmd = sprintf($cmd, $param);<commit_msg>Add burn, curl and SITCON alert<commit_after><?php
set_time_limit(60);
date_default_timezone_set('Asia/Taipei');
header('Accept: application/json');

srand(time());

include_once('config.php');

$cmd_list = array(
    'ping',
    'traceroute',
    'nslookup',
    'whois',
    'test',
    'help',
    'moo',
    'pull',
    'status',
    'log',
    'curl',
    'burn',
    '燒毀',
);

$dict = array(
    '靈的轉移',
    '巫術的權勢',
    '被東方閃電擄去了',
    '耶穌的寶血塗抹潔淨！聖靈的活水沖洗乾淨！',
    '拿勝利寶劍！',
    '斷開魂結，斷開鎖鏈！斷開一切的牽連！',
    '燒毀同性戀的網羅'
);

// Get telegram data
$json = file_get_contents('php://input') . PHP_EOL;
$data = json_decode($json, true);

// Log it
$time = date('Y-m-d H:i:s', time());
logging("<" .$time . "> Hook invoked\n");
logging($data);

$updateID = $data['update_id'];
$messageID = $data['message']['message_id'];
$fromID = $data['message']['from']['id'];
$chatID = $data['message']['chat']['id'];
$date = $data['message']['date'];
$userName = $data['message']['from']['username'];
$message = $data['message']['text'];

if($userName != ""){
    
    if(substr($message, 0, 1) == "/") {
        if( true ) { // For user check

            $cmd = str_replace('@'.BOT_NAME, '', $message);
            $message = strtolower($message);
            logging($cmd);
            $cmd = split(' ', $cmd);

            switch ($cmd[0]) {
                case "/ping":
                    if(count($cmd) == 2){
                        ping($cmd[1]);  
                    }else{
                        error(4);
                    }
                    break;

                /*case "/ping6":
                    if(count($cmd) == 2){
                        ping6($cmd[1]); 
                    }else{
                        error(4);
                    }
                    break;*/

                case "/traceroute":
                    if(count($cmd) == 2){
                        traceroute($cmd[1]);
                    }else{
                        error(4);
                    }
                    break;

                /*case "/traceroute6":
                    if(count($cmd) == 2){
                        traceroute6($cmd[1]);
                    }else{
                        error(4);
                    }
                    break;*/
                
                case "/nslookup":
                    if(count($cmd) == 3){
                        nslookup($cmd[1], $cmd[2]);
                    }if(count($cmd) == 2){
                        nslookup($cmd[1]);
                    }else{
                        error(4);
                    }
                    break;

                case "/whois":
                    if(count($cmd) == 2){
                        whois($cmd[1]);
                    }else{
                        error(4);
                    }
                    break;

                case "/test":
                    test();
                    break;

                case "/curl":
                    curl($cmd[1]);
                    //run_shell_cmd('curl %s', $cmd[1]);
                    break;

                case "/help":
                    help($cmd_list);
                    break;

                /*case "/search":
                    if(count($cmd) == 3){
                        search($cmd[1], $cmd[2]);
                    }else{
                        error(4);
                    }
                    break;*/

                case "/moo":
		            moo();
                    break;

                case "/pull":
                    git("pull github master");
                    break;

                case "/status":
                    git("status");
                    break;

                case "/log":
                    git('log --pretty=format:"%h - %an, %ar : %s"');
                    break;

                case "/burn":
                case "/燒毀":
                    burn($dict);
                    break;

                default:
                    if(strpos($message, "@".BOT_NAME)){
                        sendMsg("我沒這指令, 你想做什麼??");
                    }
                    //error(3);
                    break;
            }

        }else{
            error(2);
        }
    }
    else {

        // 訊息不是 / 開頭

        if(strpos($message, "@".BOT_NAME) !== false){
            sendMsg("嗨~ Tag 我幹嘛?");
        }

        if ( preg_match('/[Ss][Ii][Tt][Cc][Oo][Nn][f]{0,1}/',$message) === 1 && 
             preg_match('/SITCON/',$message) !== 1 ) {
             $msg = '@' .$userName . PHP_EOL;
             $msg = ' SITCON 全大寫！';
             sendMsg($msg);
        }
    }
}

function curl($url) {

    if( filter_var($url, FILTER_VALIDATE_IP) ||
        filter_var(gethostbyname($url), FILTER_VALIDATE_IP) ||
        is_domain($url)) {

        run_shell_cmd('curl %s', $url);

    } else {

        error(4);
        
    }
}

function burn($burn_dict) {

    $index = rand(0, count($burn_dict));

    sendMsg($burn_dict[$index]);

}

function run_shell_cmd($cmd, $param, $do_sprint = true) {

    if($do_sprint)
        $cmd = sprintf($cmd, $param);
{'fraction_non_alphanumeric': 0.16293007769145393, 'fraction_numerical': 0.008879023307436182, 'mean_word_length': 0.9900618374558304, 'pattern_counts': {'":': 29, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6995234', 'n_tokens_mistral': 2823, 'n_tokens_neox': 2567, 'n_words': 439}