Utils.class.php 11 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
<?php

/**
 * 根对象
 * @name Lib_Utils
 * @package Lib.Utils
 * @since 1.0
 */
class Lib_Utils
{

    /**
     * SqlMap 路径
     *
     * @return String
     */
    public static function sqlMapPath ()
    {
        return realpath(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'SqlMap');
    }

    /**
     * 结果数据
     *
     * @param $code Integer 状态码
     * @param $data mixed 数据
     * @param $message String 数据信息Tag
     * @return json      
     */
    public static function result($code, $message = null, $data = null)
    {
        $dataString = json_encode($data);
        return json_encode(array(
        	'code' => $code, 
        	'message' => $message, 
        	'data' => $data, 
        	'md5' => md5($dataString)
        ));
    }

    /**
     * 返回json格式结果
     *
     * @param $data mixed
     * @return String
     */
    public static function toJson ($data)
    {
        return json_encode($data);
    }

    /**
     * 返回xml格式结果
     *
     * @param $data mixed
     * @return String
     */
    public static function toXml ($data)
    {
        return $data;
    }

    /**
     * 转码UID
     *
     * @param $uid integer
     * @return string
     */
    public static function uid_encode ($uid)
    {
        return base_convert(intval($uid) + 10000, 10, 16);
    }

    /**
     * 解码UID
     *
     * @param $sid string
     * @return integer
     */
    public static function uid_decode ($sid)
    {
        return base_convert($sid, 16, 10) - 10000;
    }

    /**
     * 编码邀请代码
     *
     * @param $invite_id integer
     * @return string
     */
    public static function invite_encode ($invite_id)
    {
        return self::uid_encode($invite_id);
    }

    /**
     * 解码邀请代码
     *
     * @param $invite_code string
     * @return integer
     */
    public static function invite_decode ($invite_code)
    {
        return self::uid_decode($invite_code);
    }

    /**
     * 未知编码
     *
     * @param $uid Integer
     * @return String
     */
    public static function x_uid_hash ($uid)
    {
        return substr(md5($uid . "yoho"), 10, 8);
    }

    /**
     * session key 组合
     * 原:keyGen方法
     *
     * @param $uid Integer
     * @return Sring
     */
    public static function getSessionKey ($uid)
    {
        $rat = "thieve";
        $better_token = substr(md5(uniqid(mt_rand(), true)), 0, 16); // 16
        $uid_token = sprintf("%08s", base_convert($uid, 10, 16)); // 8
        $time_token = substr(md5(microtime(true)), 0, 4); // 4
        $sig_token = substr(md5($better_token . $uid_token . $time_token . $rat), 0, 4); // 4
        $session_key = $uid_token . $better_token . $time_token . $sig_token;
        return $session_key;
    }

    /**
     * 获取退出key 串
     * 原:tokenGen 方法
     *
     * @return String
     */
    public static function getUniqId ()
    {
        return md5(uniqid(mt_rand(), true) . "yoho.cn or yohobuy.com?");
    }

    /**
     * 计算年龄
     *
     * @param $birthday string 1982-02-04
     */
    public static function calculateAge ($birthday)
    {
        $iDt = strtotime($birthday);
        if ($iDt)
        {
            $aDt = getdate($iDt);
            if ($aDt['year'] < 1900)
                return false;
            $iAge = date('Y') - $aDt['year'];
            $iAge -= date('n') < $aDt['mon'] ? 1 : 0;
            $iAge = $iAge < 1 ? 1 : $iAge;
            return $iAge;
        }
        else
            return false;
    }

    public static function generateClientSignature ($paramsArray, $secret)
    {
        $str = '';
        
        ksort($paramsArray);
        // Note: make sure that the signature parameter is not already included
        // in
        // $params_array.
        foreach ($paramsArray as $k => $v)
        {
            $str .= "$k=$v";
        }
        $str .= $secret;
        
        return md5($str);
    }

    public static function arrayValue ($array, $key)
    {
        return isset($array[$key]) ? $array[$key] : '';
    }

    public static function checkIfArrayConsistsValue ($array, $key, $value)
    {
        return is_array($array) && array_key_exists($key, $array) && $array[$key] == $value;
    }

    public static function checkIfArrayIsEmpty ($array)
    {
        return is_array($array) && empty($array);
    }

    public static function sqlInStringFromArray ($aIds)
    {
        return "'" . implode("','", $aIds) . "'";
    }

    /**
     * getHashDir hash string split 3层
     *
     * @param $str string
     * @static
     *
     *
     * @access public
     * @return 图片的hash目录
     */
    public static function getHashDir ($str)
    {
        return substr($str, 0, 2) . DIRECTORY_SEPARATOR . substr($str, 2, 2) . DIRECTORY_SEPARATOR . substr($str, 4, 2) . DIRECTORY_SEPARATOR;
    }

    /**
     * getUserDir to expired
     *
     * @param $userid int
     * @static
     *
     *
     * @access public
     * @return string
     */
    public static function getUserDir ($userid)
    {
        $parts = str_split($userid, 3);
        $user_dir = implode(DIRECTORY_SEPARATOR, $parts);
        return $user_dir;
    }

    /**
     * 得到当前请求链接
     *
     * @return string
     */
    public static function getCurrentUrl ()
    {
        return $_SERVER['REQUEST_URI'];
    }

    /**
     * 根据数字最后位数散列
     *
     * @param $int Integer
     * @param $hashNum Integer 位数
     * @param $step Integer 步长
     * @return String
     */
    public static function hashLast ($int, $hashNum = 100, $step = 3)
    {
        $len = strlen($int);
        $hash = 0;
        if ($len >= $step)
        {
            $hash = (int) abs(substr($int, - $step));
            if ($hash >= $hashNum)
            {
                $hash = (int) abs(substr($int, - ($step - 1)));
            }
        }
        else
        {
            $hash = (int) $int;
        }
        if ($hash < 10)
        {
            $hash = "0" . $hash;
        }
        return $hash;
    }

    /**
     * get cookie
     *
     * @param $name String
     * @param $default String
     * @return String
     */
    public static function getCookie ($name, $default = null)
    {
        $cookie = new Util_Cookie();
        return $cookie->get($name, $default);
    }

    /**
     * 解码从数据库得到的目的
     *
     * @param $purpose integer
     * @return array there are 5 keys:1, 2, 4, 8, 16
     */
    public static function purposeDecode ($purpose)
    {
        $return = array();
        $purpose = base_convert($purpose, 10, 2);
        $purpose = sprintf("%05d", $purpose);
        $return["1"] = intval(substr($purpose, 4, 1));
        $return["2"] = intval(substr($purpose, 3, 1));
        $return["4"] = intval(substr($purpose, 2, 1));
        $return["8"] = intval(substr($purpose, 1, 1));
        $return["16"] = intval(substr($purpose, 0, 1));
        return $return;
    }

    /**
     * 编码从客户端提交的purpose数据
     *
     * @param $purpose array there must be 5 keys:1, 2, 4, 8, 16
     * @return integer
     */
    public static function purposeEncode ($purpose)
    {
        $ary = array();
        while (($v = array_pop($purpose)) !== null)
        {
            $ary[] = $v;
        }
        $purpose = implode('', $ary);
        return base_convert($purpose, 2, 10);
    }

    /**
     * 得到无符号CRC32值,并模
     *
     * @param $k varible
     * @param $mod integer
     * @return integer
     */
    public static function getUnsignedCrc32 ($k, $mod = 1)
    {
        return (int) sprintf("%u\n", crc32($k)) % $mod;
    }

    /**
     * 反相取色
     *
     * @param $color string
     * @return string
     */
    public static function invertColor ($color)
    {
        $r = intval(substr($color, 0, 2), 16);
        $g = intval(substr($color, 2, 2), 16);
        $b = intval(substr($color, 4, 2), 16);
        return str_pad(dechex(255 - $r), 2, '0') . str_pad(dechex(255 - $g), 2, '0') . str_pad(dechex(255 - $b), 2, '0');
    }

    /**
     * 反相高光取色
     *
     * @param $color string
     * @return string
     */
    public static function invertColorHighLight ($color)
    {
        return intval($color, 16) > 8388608 ? '000000' : 'FFFFFF';
    }

    public static function between ($val, $r1, $r2)
    {
        if ($val >= $r1 && $val <= $r2)
        {return true;}
        if ($val >= $r2 && $val <= $r1)
        {return true;}
        return false;
    }

    /**
     * close all open xhtml tags at the end of the string
     *
     * @param $html string
     * @return string
     * @author Milian Wolff
     */
    public static function closetags ($html)
    {
        if (! function_exists('tidy_repair_string') || empty($html))
            return $html;
        $str = tidy_repair_string($html, array('output-xhtml' => true), 'utf8');
        $str = tidy_parse_string($str, array('output-xhtml' => true), 'utf8');
        $s = '';
        $nodes = @tidy_get_body($str)->child;
        if (! is_array($nodes))
        {
            $returnVal = 0;
            return $s;
        }
        foreach ($nodes as $n)
        {
            $s .= $n->value;
        }
        return $s;
    }

    /**
     * 获取不重复随机数
     *
     * @param $min Integer 最小值
     * @param $max Integer 最大值
     * @param Integer | String $prefix 前缀
     */
    public static function randNum ($min = 1000, $max = 99999, $prefix = 0)
    {
        $tmpNum = $orderNum = array();
        $len = strlen($max);
        $tMin = $oMin = $min;
        $oMax = $max;
        for ($tMin; $tMin <= $max; ++ $tMin)
        {
            $tmpNum[$tMin] = $tMin;
        }
        for ($oMin; $oMin <= $max; ++ $oMin)
        {
            $randNum = mt_rand($oMin, $oMax);
            $rands = sprintf("%0" . $len . "d", $tmpNum[$randNum]);
            $orderNum[$rands] = $prefix;
            -- $oMax;
        }
        return $orderNum;
    }
    /**
     * 是否是邮箱格式
     * @param string $str
     * @return boolean
     */
    public static function isEmail ($str)
    {
        $str = strtolower($str);
        if (preg_match("/^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/", $str))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /**
     * 根据输入的网页版,IPHONE,ANDROID…输入网页端显示的文字
     * 
     * @param string $agent
     * @return string
     */
    public static function agentToPlatform($agent) 
    {
        if (is_string ( $agent )) 
        {
            if (preg_match ( '/(Android .+)/isU', $agent)) 
            {
                return "android";
            }
            if (stripos ( $agent, 'iPod touch' ) !== false) 
            {
                return "iphone";
            }
            if (stripos ( $agent, 'iPhone' ) !== false) 
            {
                return "iphone";
            }
        }
        return "web";
    }
}