<?php

require_once FRAMEWORK_PATH . 'protected/extensions/function_extend.php';
require_once FRAMEWORK_PATH . 'protected/extensions/format_extend.php';


function getAwsSeverIp() {
    $output = array();
    $return_var = array();
    $ret = exec("ifconfig", $output, $return_var);

    $ips = [];
    foreach ($output as $row) {
        $matches = [];
        preg_match('/inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/', $row, $matches);
        if ($matches) {
            $ip = $matches[1];
            $ips[$ip] = $ip;
        }
    }

    unset($ips['127.0.0.1']);
    return current($ips);
}


/**
 * 发送告警信息
 */
function alermErrorMsg($msg) {
    Tool::err($msg);
    if (ENV == ENV_DEV) {
        Eos::log("【error】$msg");
        return false;
    }

    $msg = trim($msg);
    if (empty($msg)) {
        return false;
    }
    $amc_url = 'http://61.160.36.226/default/recordMsg';
    $data = [
        "code"=> CODE_UNKNOW_ERROT,
        "code_msg"=> $msg,
        "app_id"=> 'dw_box',
        "server_ip"=> getAwsSeverIp() ?: '127.0.0.1',
        "alerm_type"=> 'ERROR',
        "time"=> time()
    ];

    $appKey = 'a67362f15b13cf5df4ac695b39844c84';
    $tmpArr = $data;
    $tmpArr['app_key'] = $appKey;
    sort($tmpArr, SORT_STRING);
    $tmpStr = implode($tmpArr);
    $signStr = sha1($tmpStr);
    $data['sign'] = $signStr;

    $header = 'Host: amc-admin.duowan.com';
    $ret = (new dwHttp)->post($amc_url, $data, 5, $header);
    $ret = json_decode($ret, true);
    if ($ret['result']) {
        return true;
    } else {
        var_dump("【error】告警错误：" . $ret['msg']);
        return false;
    }
}

/**
 * 判断是否手机请求
 * @return boolean [description]
 */
function is_mobile_request() {
    if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
        return true;
    }

    // But WP7 is also Windows, with a slightly different characteristic
    if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false) {
        return true;
    }

    return false;
}

function awsReplaceArrImg(&$arr, $key) {
    foreach ($arr as $i => $data) {
        $arr[$i][$key] = awsReplaceImg($data[$key]);
    }
}

function awsReplaceImg($content) {
    $domain = 'image.meechat.me';
    $ua = $_SERVER["HTTP_USER_AGENT"];
    if (strpos($ua,'MSIE ') === false) {
        $scheme = 'https';
    } else {
        $scheme = 'http';
    }

    $from = [
        '//w2.meechat.me/',
        '//w3.meechat.me/',
    ];

    $to = [
        "//{$domain}/ips/w2.meechat.me/",
        "//{$domain}/ips/w3.meechat.me/",
    ];

    $content = str_replace($from, $to, $content);
    // 替换为http请求
    if ($scheme == 'https') {
        $content = str_replace("http://{$domain}/", "{$scheme}://{$domain}/", $content);
    }

    return $content;
}

function coverReplaceArrImage(&$arr, $key) {
    foreach ($arr as $i => $data) {
        $arr[$i][$key] = coverReplaceImage($data[$key]);
    }
}

function coverReplaceImage($src, $w = 180) {
    $url = awsReplaceImg($src);
    if ($url != $src) {
        if (false !== strpos($url, '?')) {
            $url .= '&';
        } else {
            $url .= '?';
        }
        $url .= "imageview/0/w/{$w}";
        $supportWebp = strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') || $_COOKIE['_webp'];
        if ($supportWebp) {
            $url .= '/format/webp';
        }
    }

    return $url;
}
