<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Index extends CI_Controller
{
    private $endpoint = 'cn-guangzhou.log.aliyuncs.com';
    private $accessKeyId = 'LTAI4G9HFUkuYzYCBhPK4n2w';
    private $accessKey = 'Yh5vtntTF9JCX8yylvcRlwzO4Prbzb';
    private $project = 'globalhoghlights';
    //private $logstore = 'globalhoghlights';
    private $token = "";
    private $Log_Client;
    private $logstore = array(
        'cht' => 'globalhoghlights'
        ,
                'ghtobzg' => 'globalhoghlights'
        ,
                        'ghtobhw' => 'globalhoghlights'
        ,
        'ah' => 'asiahighlights'
        ,
        'gh' => 'globalhighlights'
        ,
        'ct' => 'chinatravel'
        ,
        'yz' => 'yangtzeriver'
        ,
        'ts' => 'trainspread_com'
        ,
        'gl' => 'guilinchina'
        ,
        'sht' => 'shanghaihighlights'
        ,
        'gm' => 'chinarundreisen'
        ,
        'gmhw' => 'chinarundreisen'
        ,
        'jp' => 'arachina'
        ,
        'jphw' => 'arachina'
        ,
        'ru' => 'chinahighlights_ru'
        ,
        'ruhw' => 'chinahighlights_ru'
        ,
        'it' => 'viaggio-in-cina'
        ,
        'ithw' => 'viaggio-in-cina'
        ,
        'vac' => 'viaje-a-china'
        ,
        'vachw' => 'viaje-a-china'
        ,
        'vc' => 'voyageschine'
        ,
        'vchw' => 'voyageschine'
        ,
        'gh_vac' => 'globalhighlights_es'
        ,
        'gh_jp' => 'globalhighlights_jp'
        ,
        'gh_vc' => 'globalhighlights_fr'
        ,
        'gh_ru' => 'globalhighlights_ru'
        ,
        'gh_gm' => 'globalhighlights_de'
        ,
        'gh_it' => 'globalhighlights_it'
    ); //站点日志存储库，每个网站对应一个

    public function __construct()
    {
        parent::__construct();
        $this->load->model('wwwlogs_model');
        $this->load->library('Aliyun_log_lib');
        $this->Log_Client = new Aliyun_Log_Client($this->endpoint, $this->accessKeyId, $this->accessKey, $this->token);
    }

    public function index()
    {
        $data = array();
        $orders = $this->wwwlogs_model->get_update_list(15);
        // print_r($orders);
        if ($orders) {
            foreach ($orders as $item) {
                $site_code = strtolower($item->COLI_WebCode);
                //判断是否为国际站点
                if (in_array($site_code, array('gm', 'gmhw', 'jp', 'jphw', 'ru', 'ruhw', 'it', 'ithw', 'vac', 'vachw', 'vc', 'vchw', 'gh_vac', 'gh_jp', 'gh_vc', 'gh_gm', 'gh_ru', 'gh_it'))) {
                    try {
                        $this->orders_view_path_int($item->COLI_SN, $site_code, $item->COLI_SenderIP, 0, $item->COLI_OrderDetailText);
                    } catch (Exception $err) {
                        print_r($err);
                    }
                } else {
                    $this->orders_view_path($item->COLI_SN, $site_code, $item->COLI_SenderIP);
                }
                echo 'COLI_WebCode:' . $item->COLI_WebCode . ' COLI_ID:' . $item->COLI_ID . ' COLI_SenderIP:' . $item->COLI_SenderIP . ' COLI_SN:' . $item->COLI_SN . ' COLI_ApplyDate:' . $item->COLI_ApplyDate. '<br/>';
            }
            // echo 'test async v6';
        }

        // test
        // try {
        //     $this->orders_view_path_int();
        // } catch (Exception $err) {
        //     print_r($err);
        // }

        $this->load->view('welcome');
    }

    //定时抓取订单日志
    public function orders_view_path($COLI_SN, $sitecode = 'cht', $ip, $offset = 0)
    {
        $query_string = "
			* | select time_local,http_referer,request_uri,http_user_agent,http_x_forwarded_for
			 remote_addr,request_method,request_time,status,body_bytes_sent,upstream_response_time from log where
				request_uri NOT  like '%.jpg'
				and request_uri NOT  like '%.JPG'
				and request_uri NOT  like '%.png'
				and request_uri NOT  like '%.gif'
				and request_uri NOT  like '%.css'
				and request_uri NOT  like '%.webp'
				and request_uri NOT  like '%.js'
				and request_uri NOT  like '%.woff'
				and request_uri NOT  like '%.ttf'
				and request_uri NOT  like '%.eot'
				and request_uri NOT  like '%.ico'
				and request_uri NOT  like '/guide-use.php%'
				and request_uri NOT  like '/index.php%'
				and request_uri NOT  like '/secureforms/form_token'
				and request_uri NOT  like '/ip/'
				and request_uri NOT  like '/ajax/getads/'
				and request_uri NOT  like '/public/template/footer.html'
				and request_uri NOT  like '/public/%'
				and (remote_addr='$ip' OR  http_x_forwarded_for like '%$ip%')
			"; //ORDER  BY  time_local ASC   时间精度不够，会导致相同排序错误，只能用系统默认排序
        /*
        remote_addr :'23.99.99.139'
        AND  NOT request_uri :"/secureforms/form_token"
        AND  NOT request_uri :".jpg"
        AND NOT request_uri :".png"
        AND NOT request_uri :".gif"
        AND NOT request_uri :"guide-use.php"
        AND NOT request_uri : "/index.php"
        and remote_addr='$ip'
         */
        $from = time() - 604800; //往前 604800(7天), 1296000(15天)
        $to = time();
        $request = new Aliyun_Log_Models_GetLogsRequest('globalhoghlights', $this->logstore[$sitecode], $from, $to, '', $query_string, 100, $offset, true);
        try {
            $LogData = new StdClass;
            $LogData->wl_sitecode = $sitecode;
            $LogData->wl_COLI_SN = $COLI_SN; //订单SN
            $response = $this->Log_Client->getLogs($request);
            foreach ($response->getLogs() as $log) {
                foreach ($log->getContents() as $key => $value) {
                    $LogData->$key = $value;
                }
                $this->wwwlogs_model->add('wwwlogs', $LogData);
            }
        } catch (Aliyun_Log_Exception $ex) {
            print_r($ex);
        } catch (Exception $ex) {
            print_r($ex);
        }
    }

    //定时抓取订单日志 - 国际站
    public function orders_view_path_int($COLI_SN, $sitecode = 'cht', $ip, $offset = 0, $order_text)
    {
        // 测试数据 - 2
        //$COLI_SN = 1004250;
        //$sitecode = 'gmhw';
        //$ip = '2a00:6020:41c7:3900:20c1:3c5c:c854:7095';
        //$offset = 0;
        //$order_text = 'Gaid-> GA1.2.1747854879.1682254516 <-Gaid
        //YandexId-> 1682254516819066352 <-YandexId
        //clino :g-de-in-2
        //startdate :2023-08-06
        //adultnum :4
        //Reiseroute Klasse :4 sterne hotel
        //tourname :Rundreisen Nordindien 3 Wochen
        //tages :21
        //Reiseroutennummer :14083
        //Geschlecht :
        //Name :Firestone
        //nationality :?sterreich
        //E-Mail :AnaAnaki@hotmail.com
        //Telefon :+4369918253609
        //来源页面 :https://www.chinarundreisen.com/indien/top-indien-reiseziele/
        //
        //from:android
        //Mozilla/5.0 (Linux; Android 9; SM-G950F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36 X-Middleton/1';

        // 匹配clientId的正则表达式
        $yarndexPat = "/YandexId->(.+?)<-YandexId/";
        // 检查文本是否匹配正则表达式
        if (preg_match($yarndexPat, $order_text, $yandexMatches)) {
            $client_id = trim($yandexMatches[1]);
        } else {
            $gaPat = "/Gaid->(.+?)<-Gaid/";
            if (preg_match($gaPat, $order_text, $gaMatches)) {
                $client_id = trim($gaMatches[1]);
            } else {
                $client_id = "-- Match not found --";
            }
        }
        // echo $client_id;

        $query_string = "
			* | select time_local,http_referer,request_uri,http_user_agent,http_x_forwarded_for,
            remote_addr,request_method,request_time,status,body_bytes_sent,upstream_response_time from log where
                request_uri like '/io/?event=pageview%'
                and (remote_addr='$ip' OR  http_x_forwarded_for like '%$ip%' OR request_uri like '%$client_id%')
                ORDER BY __time__ DESC
			"; // 同时使用IP和用户ID
        //echo '<code>'.$query_string.'</code><br>';
        $from = time() - 604800 * 2; //往前 604800(7天), 1296000(15天)
        $to = time();
        $request = new Aliyun_Log_Models_GetLogsRequest('globalhoghlights', $this->logstore[$sitecode], $from, $to, '', $query_string, 100, $offset, true);
        try {
            $LogData = new StdClass;
            $LogData->wl_sitecode = $sitecode;
            $LogData->wl_COLI_SN = $COLI_SN; //订单SN
            $response = $this->Log_Client->getLogs($request);
            foreach ($response->getLogs() as $log) {
                foreach ($log->getContents() as $key => $value) {
                    $LogData->$key = $value;
                }

                // 调整数据: http_referer -> request_uri, request_uri.ref -> http_referer
                if (isset($LogData->http_referer) && isset($LogData->request_uri)) {
                    $request_uri = $LogData->request_uri;
                    $http_referer = $LogData->http_referer;

                    // http_referer -> request_uri
                    $LogData->request_uri = $LogData->http_referer;
                    // request_uri.ref -> http_referer
                    $query_string_tmp = urldecode($request_uri);
                    parse_str($query_string_tmp, $params);
                    if (isset($params['ref'])) {
                        $LogData->http_referer = $params['ref'];
                    } else {
                        $LogData->http_referer = '(not set)';
                    }

                }
                
                //echo '<code>'.print_r($LogData, true).'</code><br>';
                $add_status = $this->wwwlogs_model->add('wwwlogs', $LogData);
                //echo '<code> add_status => '.$add_status.'</code><br>';
            }
        } catch (Aliyun_Log_Exception $ex) {
            print_r($ex);
        } catch (Exception $ex) {
            print_r($ex);
        }
    }

    public function getLogs(Aliyun_Log_Client $client, $project, $logstore)
    {
        $topic = '';
        $from = time() - 604800; //往前7天
        $to = time();
        $request = new Aliyun_Log_Models_GetLogsRequest($project, $logstore, $from, $to, $topic, "", 100, 0, false);

        try {
            $response = $client->getLogs($request);
            foreach ($response->getLogs() as $log) {
                print $log->getTime() . "\t";
                foreach ($log->getContents() as $key => $value) {
                    print $key . ":" . $value . "\t";
                }
                print "\n";
            }
        } catch (Aliyun_Log_Exception $ex) {
            logVarDump($ex);
        } catch (Exception $ex) {
            logVarDump($ex);
        }
    }

    //测试接口权限 - lmr
    public function test($COLI_SN, $sitecode = 'cht', $ip, $offset = 0, $order_text)
    {
        // 测试数据 - 2
        //$COLI_SN = 1004250;
        //$sitecode = 'gmhw';
        //$ip = '2a00:6020:41c7:3900:20c1:3c5c:c854:7095';
        //$offset = 0;
        //$order_text = 'Gaid-> GA1.2.1747854879.1682254516 <-Gaid
        //YandexId-> 1682254516819066352 <-YandexId
        //clino :g-de-in-2
        //startdate :2023-08-06
        //adultnum :4
        //Reiseroute Klasse :4 sterne hotel
        //tourname :Rundreisen Nordindien 3 Wochen
        //tages :21
        //Reiseroutennummer :14083
        //Geschlecht :
        //Name :Firestone
        //nationality :?sterreich
        //E-Mail :AnaAnaki@hotmail.com
        //Telefon :+4369918253609
        //来源页面 :https://www.chinarundreisen.com/indien/top-indien-reiseziele/
        //
        //from:android
        //Mozilla/5.0 (Linux; Android 9; SM-G950F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36 X-Middleton/1';

        // 匹配clientId的正则表达式
        $yarndexPat = "/YandexId->(.+?)<-YandexId/";
        // 检查文本是否匹配正则表达式
        if (preg_match($yarndexPat, $order_text, $yandexMatches)) {
            $client_id = trim($yandexMatches[1]);
        } else {
            $gaPat = "/Gaid->(.+?)<-Gaid/";
            if (preg_match($gaPat, $order_text, $gaMatches)) {
                $client_id = trim($gaMatches[1]);
            } else {
                $client_id = "-- Match not found --";
            }
        }
        // echo $client_id;

        $query_string = "
			* | select time_local,http_referer,request_uri,http_user_agent,http_x_forwarded_for,
            remote_addr,request_method,request_time,status,body_bytes_sent,upstream_response_time from log where
                request_uri like '/io/?event=pageview%'
                and (remote_addr='$ip' OR  http_x_forwarded_for like '%$ip%' OR request_uri like '%$client_id%')
                ORDER BY __time__ DESC
			"; // 同时使用IP和用户ID
        $from = time() - 604800 * 2; //往前 604800(7天), 1296000(15天)
        $to = time();
        $request = new Aliyun_Log_Models_GetLogsRequest('globalhoghlights', $this->logstore[$sitecode], $from, $to, '', $query_string, 100, $offset, true);
        try {
            $LogData = new StdClass;
            $LogData->wl_sitecode = $sitecode;
            $LogData->wl_COLI_SN = $COLI_SN; //订单SN
            $response = $this->Log_Client->getLogs($request);
            foreach ($response->getLogs() as $log) {
                foreach ($log->getContents() as $key => $value) {
                    $LogData->$key = $value;
                }

                // 调整数据: http_referer -> request_uri, request_uri.ref -> http_referer
                if (isset($LogData->http_referer) && isset($LogData->request_uri)) {
                    $request_uri = $LogData->request_uri;
                    $http_referer = $LogData->http_referer;

                    // http_referer -> request_uri
                    $LogData->request_uri = $LogData->http_referer;
                    // request_uri.ref -> http_referer
                    $query_string_tmp = urldecode($request_uri);
                    parse_str($query_string_tmp, $params);
                    if (isset($params['ref'])) {
                        $LogData->http_referer = $params['ref'];
                    } else {
                        $LogData->http_referer = '(not set)';
                    }

                }

                print_r($LogData);
                // $this->wwwlogs_model->add('wwwlogs', $LogData);
            }
        } catch (Aliyun_Log_Exception $ex) {
            print_r($ex);
        } catch (Exception $ex) {
            print_r($ex);
        }
    }

}