<?php
require_once('log.php');

class Umeng
{
    private $_dir;
    private $cookie;
    private $username;
    private $password;
    private $apps_key;


    public function __construct()
    {
        $this->apps_key = "5072000b1fa55f0e83457355";
        $this->dir = '';
        $this->cookie = __DIR__ . "/cookie/umeng.txt";
        $this->username = "admin@admin.com";
        $this->password = "***";
    }


    public function login()
    {
        $url = "https://i.umeng.com";

        //获取token
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); //保存cookie
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $content = curl_exec($ch);
        $preg = "/token: '(.*?)',/is";
        preg_match_all($preg, $content, $arr);
        $token = $arr[1][0];
        curl_close($ch);


        //模拟登录
        $login_url = "https://i.umeng.com/login/ajax_do"; //login
        $post_field = array(
            "token" => $token,
            "username" => $this->username,
            "password" => $this->password,
            "website" => "umengplus",
            "sig" => '',
            "sessionid" => '',
            "app_id" => '',
            "url" => '',
            );


        $posts = http_build_query($post_field);
        $heads = array(
        "x-requested-with: XMLHttpRequest",
        "referer: https://i.umeng.com/",
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $heads);
        curl_setopt($ch, CURLOPT_URL, $login_url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); //读取cookie
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $posts);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); //保存更新cookie


        $res = curl_exec($ch);
        curl_close($ch);
    }


    //获取渠道号
    public function get_channel($app_id)
    {
        include('db.php');
        $sql = "select * from um_channels where app_id = " . $app_id;
        $res = $pdo->query($sql);//返回影响了多少行数据
        $channels = $res->fetchAll();
        return $channels;
    }


    /* 抓取数据
    /* param: $channels 渠道号 $stats 统计参数 $day 日期 $count 查询数量/分页数 $type 数据类型
    /* return: array
    */
    public function get_info($appkey = null, $channel, $stats, $day, $count = 30, $type = 1)
    {
    /*数据接口*/

    //拼接url
    if ($type == 1) {
        $url = "http://mobile.umeng.com/apps/" . $this->apps_key . "/reports/load_table_data?";
        $info_url = $url . "page=1&per_page=" . $count . "&start_date=" . $day . "&end_date=" . $day . "&versions%5B%5D=&channels%5B%5D=" . $channel['key'] . "&segments%5B%5D=&time_unit=daily&stats=" . $stats;
    } else {
        $url = "http://mobile.umeng.com/apps/" . $this->apps_key . "/channels/load_chart_data?";
        $info_url = $url . "start_date=" . $day . "&end_date=" . $day . "&versions%5B%5D=&channels%5B%5D=&segments%5B%5D=&time_unit=daily&stats=" . $stats . "&original_data_count=24&is_compared=true&channel_ids=" . $channel['key'];
    }

    $res = $this->get_ajax($info_url);

    //解析json
    $datas = json_decode($res, true);


        if ($datas['result'] == 'success') {
        $data = $datas['stats'];
        return $data;
        } else {
        return false;
        }
    }


    //获取数据
    public function get_ajax($info_url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $info_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); //读取cookie
        $rs = curl_exec($ch); //执行cURL抓取页面内容
        curl_close($ch);
        return $rs;
    }
}