<?
// 긴글 자르기
function cut_str($str, $len, $suffix = "…")
{
    $s = substr($str, 0, $len);
    $cnt = 0; // 마지막 글자에서 잘린 후 남겨진 바이트 수

    $s_len = strlen($s);

    // UTF-8 마지막 글자 깨짐 처리

    for ($i = 0; $i < $s_len; $i++) {

        $oc = ord($s[$i]);

        if (($oc & 0xF8) == 0xF0) // 4byte
        {
            if ($i + 4 >= $s_len) {$cnt = ($s_len - $i) % 4;
                break;} else {
                $i += 3;
            }

        } else if (($oc & 0xF0) == 0xE0) // 3byte
        {
            if ($i + 3 >= $s_len) {$cnt = ($s_len - $i) % 3;
                break;} else {
                $i += 2;
            }

        } else if (($oc & 0xE0) == 0xC0) // 2byte
        {
            if ($i + 2 >= $s_len) {$cnt = ($s_len - $i) % 2;
                break;} else {
                $i++;
            }

        } else // 1byte
        {
            $cnt = 0;
        }

    }
    if ($cnt) {
        $s = substr($s, 0, $s_len - $cnt);
    }

    if (strlen($s) >= strlen($str)) {
        $suffix = "";
    }

    return $s . $suffix;
}

//문자열 처리 trim,htmlspecialchars,addslashes
function mk_trim()
{
    $nums = func_num_args();
    $row = func_get_args();
    for ($i = 0; $i < $nums; $i++) {
        $row[$i] = addslashes($row[$i]);
        $row[$i] = htmlspecialchars($row[$i]);
        $row[$i] = trim($row[$i]);
        return $row[$i];
    }
}

//DB 테이블 생성 확인
function exist_table($table_name)
{
    $result = mysql_query("show tables like '" . $table_name . "'");
    $row = mysql_fetch_array($result, MYSQL_NUM);
    if ($row == false) {
        return false;
    }

    return true;
}

// 실행시간 체크
function getmicrotime()
{
    list($usec, $sec) = explode(' ', microtime());
    return ((float) $usec + (float) $sec);
}

//패스워드 암호화
function sha_256($user_passwd)
{
    $return = hash('sha256', $user_passwd);
    return $return;
}

//get response
function get_response($host, $request)
{
    $fp = fsockopen($host, 80);
    if (!$fp) {flush();
    } else {
        fputs($fp, $request);
        flush();
        $response = "";
        while (!feof($fp)) {$response .= fgets($fp, 1024);}
    }
    fclose($fp);
    return $response;
}

//용량계산
function size($size)
{
    $unit = array(" Byte", " KB", " MB", " GB");
    if (!$size) {
        return "0" . $unit[0];
    }

    if ($size < 1024) // 바이트
    {
        return $size . $unit[0];
    }

    if ($size > 1024 && $size < 1024 * 1024) // 킬로바이트
    {
        return sprintf("%0.1f " . $unit[1], $size / 1024);
    }

    if ($size > 1024 * 1024 && $size < 1024 * 1024 * 1024) // 메가바이트
    {
        return sprintf("%0.1f " . $unit[2], $size / (1024 * 1024));
    }

    if ($size > 1024 * 1024 * 1000 && $size < 1024 * 1024 * 1024 * 1024) // 기가바이트
    {
        return sprintf("%0.1f " . $unit[3], $size / (1024 * 1024 * 1000));
    }

}

//프록시 서버 체크-실제 아이피 가져오기
function use_ip()
{
    $reverse = 0;
    $e = 0;
    $hostip = getenv("HTTP_X_FORWARDED_FOR");
    $hostip = $hostip ? $hostip : getenv("REMOTE_ADDR");
    $check = $reverse ? @gethostbyaddr($hostip) : "";
    $hostip = $check ? $check : $hostip;
    $gateway_ip = getenv("REMOTE_ADDR");
    return $hostip;
}

//GMT 시간 표시
function GMT($time_zone)
{
    switch ($time_zone) {
        case 'Kwajalein':$gmt = "-12";
            break;
        case 'Pacific/Midway':$gmt = "-11";
            break;
        case 'Pacific/Honolulu':$gmt = "-10";
            break;
        case 'America/Anchorage':$gmt = "-9";
            break;
        case 'America/Los_Angeles':$gmt = "-8";
            break;
        case 'America/Denver':$gmt = "-7";
            break;
        case 'America/Tegucigalpa':$gmt = "-6";
            break;
        case 'America/New_York':$gmt = "-5";
            break;
        case 'America/Caracas':$gmt = "-4.30";
            break;
        case 'America/Halifax':$gmt = "-4";
            break;
        case 'America/St_Johns':$gmt = "-3.30";
            break;
        case 'America/Argentina/Buenos_Aires':$gmt = "-3";
            break;
        case 'America/Sao_Paulo':$gmt = "-3";
            break;
        case 'Atlantic/South_Georgia':$gmt = "-2";
            break;
        case 'Atlantic/Azores':$gmt = "-1";
            break;
        case 'Europe/Dublin':$gmt = "0";
            break;
        case 'Europe/Belgrade':$gmt = "+1";
            break;
        case 'Europe/Minsk':$gmt = "+2";
            break;
        case 'Asia/Kuwait':$gmt = "+3";
            break;
        case 'Asia/Tehran':$gmt = "+3.30";
            break;
        case 'Asia/Muscat':$gmt = "+4";
            break;
        case 'Asia/Yekaterinburg':$gmt = "+5";
            break;
        case 'Asia/Kolkata':$gmt = "+5.30";
            break;
        case 'Asia/Katmandu':$gmt = "+5.45";
            break;
        case 'Asia/Dhaka':$gmt = "+6";
            break;
        case 'Asia/Rangoon':$gmt = "+6.30";
            break;
        case 'Asia/Krasnoyarsk':$gmt = "+7";
            break;
        case 'Asia/Brunei':$gmt = "+8";
            break;
        case 'Asia/Seoul':$gmt = "+9";
            break;
        case 'Australia/Darwin':$gmt = "+9.30";
            break;
        case 'Australia/Canberra':$gmt = "+10";
            break;
        case 'Asia/Magadan':$gmt = "+11";
            break;
        case 'Pacific/Fiji':$gmt = "+12";
            break;
        case 'Pacific/Tongatapu':$gmt = "+13";
            break;
    }
    return $gmt;
}
