<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2016/12/21
 * Time: 下午4:06
 */
require_once (BASE_DATA_PATH . '/api/phpqrcode/phpqrcode.php');

class util
{
    const min_memno = 500;
    const max_memno = 2000;
    const passwd = 'xmmz@lrlz';

    public static function member_avatar($path)
    {
        if (is_null($path) || empty($path)) {
            $img = 'female.png';
            return RESOURCE_SITE_URL . '/mobile/defimg/' . $img;
        } else {
            if (strncasecmp($path, "http://", strlen("http://")) == 0) {
                $url = $path;
            } else {
                $url = UPLOAD_SITE_URL . "/shop/avatar{$path}";
            }
            return $url;
        }
    }

    public static function goods_thumb_image($path, $store_id)
    {
        return cthumb($path, 480, $store_id);
    }

    public static function goods_spec($item)
    {
        $keyval = unserialize($item);
        if($keyval == false) {
            return "";
        }

        foreach ($keyval as $key => $val) {
            return $val;
        }
        return "";
    }

    public static function nickname($nick, $mobile, $name)
    {
        if (isset($nick) && !empty($nick)) {
            return $nick;
        }

        if (isset($name) && !empty($name)) {
            return $name;
        }

        if (isset($mobile) && !empty($mobile)) {
            if (strlen($mobile) == 11) {
                return substr_replace($mobile, '****', 3, 4);
            }
        }

        return '';
    }
    public static function mem_no()
    {
        $number = self::mb_incr('member_no',1,self::min_memno);
        if($number > self::max_memno) {
            self::mb_del_incr('member_no');
            $start_number = mt_rand(self::min_memno,2 * self::min_memno);
            $number = self::mb_incr('member_no',1,$start_number);
        }

        return $number;
    }

    private static function mb_incr($key,$val,$def_value = 0)
    {
        return incrcache('global_incr',$key,$val,'mb_',$def_value);
    }
    private static function mb_del_incr($key)
    {
        return del_incr('global_incr',$key,'mb_');
    }

    public static function start_with($haystack, $needle)
    {
        $length = strlen($needle);
        return (substr($haystack, 0, $length) === $needle);
    }

    public static function end_with($haystack, $needle)
    {
        $length = strlen($needle);
        if ($length == 0) {
            return true;
        }

        return (substr($haystack, -$length) === $needle);
    }
    public static function imgsize($url)
    {
        if (util::start_with($url,BASE_SITE_URL) == false) {
            return @getimagesize($url);
        }
        else {
            $length = strlen(BASE_SITE_URL);
            $src = BASE_ROOT_PATH . substr($url, $length, strlen($url) - $length);
            $size = @getimagesize($src);
            if($size == false) {
                return @getimagesize($url);
            } else {
                return $size;
            }
        }
    }
    public static function http_add_params($url, $params)
    {
        if (!empty($params)) {
            $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
        }

        return $url;
    }
    public static function from_wechat()
    {
        $agent = request_helper::http_useragent();
        $pos = strpos($agent,"MicroMessenger");
        if($pos === false) {
            return false;
        } else {
            return ($pos >= 0);
        }
    }
    public static function encrypt_data($plaintext)
    {
        return encrypt("{$plaintext}",util::passwd);
    }
    public static function decrypt_data($ciphertext)
    {
        return decrypt($ciphertext,util::passwd);
    }

    public static function qrcode($url,$name)
    {
        if(empty($url) || empty($name)) return false;

        $save_path = BASE_UPLOAD_PATH . DS.ATTACH_MQRCODE.DS . $name;
        QRcode::png($url,$save_path,QR_ECLEVEL_H);
        return $save_path;
    }

    public static function qrcode_path($url,$save_path)
    {
        if(empty($url) || empty($save_path)) return false;
        QRcode::png($url,$save_path,QR_ECLEVEL_H);
        return $save_path;
    }
    public static function base64url_encode($plainText)
    {
        $base64 = base64_encode($plainText);
        $base64url = strtr($base64, ['+' => '-','/' => '_','=' => ',']);
        return $base64url;
    }

    public static function base64url_decode($plainText)
    {
        $base64url = strtr($plainText, ['-' => '+','_' => '/',',' => '=']);
        $base64 = base64_decode($base64url);
        return $base64;
    }
    public static function first_day_secs($input)
    {
        return strtotime(date('Y-m-d',$input));
    }
    public static function last_day_secs($input)
    {
        return strtotime(date('Y-m-d',$input)) + 86399;
    }
}