namespace chief725\libs; use DateTime; use Yii; use yii\helpers\Html; class Utils { public static function header_expire($interval) { header ( "Cache-Control: public, max-age=$interval", true ); header ( "Pragma: public", true ); header ( 'Expires: ' . gmdate ( 'D, d M Y H:i:s', time () + $interval ) . ' GMT', true ); } public static function header_last_modified($last_modified_time) { header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s", $last_modified_time ) . " GMT" ); } public static function cutString($string, $length) { if (mb_strlen ( $string, 'utf8' ) <= $length) { return $string; } else { return mb_substr ( $string, 0, $length, 'utf8' ) . " ..."; } } public static function queryString($key, $default = "") { if (! empty ( $_POST [$key] )) return $_POST [$key]; if (! empty ( $_GET [$key] )) return $_GET [$key]; return $default; } public static function select(...$objs) { foreach ( $objs as $obj ) if (! empty ( $obj )) return $obj; } public static function choose(&$obj1, $obj2 = "") { return empty ( $obj1 ) ? $obj2 : $obj1; } public static function from($country_code) { if (isset ( $_SERVER ["HTTP_CF_IPCOUNTRY"] ) && ! empty ( $country_code )) { $cf_country_code = strtolower ( $_SERVER ["HTTP_CF_IPCOUNTRY"] ); $country_codes = explode ( ",", $country_code ); return in_array ( $cf_country_code, $country_codes ); } return false; } public static function contains($haystack, $needle) { if (empty ( $needle )) { return false; } if (is_array ( $needle )) { foreach ( $needle as $n ) { if (self::contains ( $haystack, $n )) { return true; } } return false; } else { return strpos ( $haystack, $needle ) !== FALSE; } } public static function icon($icon, $text = "") { return " $text"; } public static function iconfa($icon, $text = "") { return " $text"; } public static function in($stack, $needle) { return strpos ( $stack, $needle ) !== false; } public static function isAdm() { return ! Yii::$app->user->isGuest && Yii::$app->user->identity->can ( "admin" ); } public static function isAdmin() { return ! Yii::$app->user->isGuest && Yii::$app->user->identity->can ( "admin" ); } public static function isGuest() { return Yii::$app->user->isGuest; } public static function print_d($arr) { echo "
"; print_r ( $arr ); echo ""; } public static function numberAbbr($number) { $abbrevs = [ 12 => 'T', 9 => 'B', 6 => 'M', 3 => 'K', 0 => '' ]; foreach ( $abbrevs as $exponent => $abbrev ) { if (abs ( $number ) >= pow ( 10, $exponent )) { $display = $number / pow ( 10, $exponent ); $decimals = ($exponent >= 3 && round ( $display ) < 100) ? 1 : 0; $number = number_format ( $display, $decimals ) . $abbrev; break; } } return $number; } public static function now() { return date ( "Y-m-d H:i:s" ); } public static function today() { return date ( "Y-m-d" ); } public static function time($secondsAgo = 0) { return date ( "Y-m-d H:i:s", time () - $secondsAgo ); } public static function isBefore($day) { return date ( "Y-m-d" ) <= $day; } public static function isAfter($day) { return date ( "Y-m-d" ) >= $day; } public static function formatTime($time, $en = false) { $time = strtotime ( $time ); $limit = time () - $time; if ($limit < 60) { return $en ? "Just now" : "刚才"; } elseif ($limit < 3600) { return floor ( $limit / 60 ) . ($en ? " min ago" : '分钟前'); } elseif ($limit < 86400) { return floor ( $limit / 3600 ) . ($en ? " hour ago" : '小时前'); } elseif ($limit < 86400 * 365) { return floor ( $limit / 86400 ) . ($en ? " day ago" : '天前'); } elseif ($limit >= 86400) { return date ( 'Y-m-d', $time ); } } public static function formatByte($num, $precision = 0) { return str_replace ( "B", "G", self::formatInt ( $num, $precision ) ); } public static function formatInt($num, $precision = 0) { $units = explode ( ",", ",K,M,B,T" ); foreach ( range ( 0, 4 ) as $i ) { if ($num < 1000) { return round ( $num, $precision ) . $units [$i]; } $num = $num / 1000; } } public static function formatIntCN($num, $precision = 0) { $units = explode ( ",", ",万,亿,兆" ); foreach ( range ( 0, 4 ) as $i ) { if ($num < 10000) { return round ( $num, $precision ) . $units [$i]; } $num = $num / 10000; } } public static function similar($str1, $str2) { similar_text ( $str1, $str2, $percent ); return $percent; } public static function ipLink($ip) { return Html::a ( $ip, "https://www.iplocation.net/?query=$ip" ); } public static function flagImage($key, $options = [ ]) { $key = trim ( strtolower ( $key ) ); return Html::tag ( "span", null, array_merge ( [ 'class' => "flag-icon flag-icon-$key", 'style' => "font-size:2em" ], $options ) ); } public static function lessThan($num) { return rand ( 0, 99 ) < $num; } public static function array_template($array, $template) { foreach ( $array as $k => $v ) { $array [$k] = str_replace ( "{value}", $v, $template ); } return $array; } public static function cacheGet($url, $duration = 3600) { $resp = Yii::$app->cache->get ( $url ); if (empty ( $resp )) { $resp = @file_get_contents ( $url ); Yii::$app->cache->set ( $url, $resp, $duration ); } return $resp; } public static function randomSelect($arr) { return $arr [rand ( 0, count ( $arr ) - 1 )]; } public static function isRunning($key) { $fp = fopen ( Yii::getAlias ( "@app/runtime/$key" ), 'w+' ); return ! flock ( $fp, LOCK_EX | LOCK_NB ); } public static function validateDate($date, $format = 'Y-m-d') { $date = str_replace ( "/", "-", $date ); $d = DateTime::createFromFormat ( $format, $date ); return $d && $d->format ( $format ) === $date; } public static function isSpider($regex = 'bot|spider|crawl|facebook') { $agent = Utils::choose ( $_SERVER ["HTTP_USER_AGENT"] ); return isset ( $_GET ['spider'] ) || preg_match ( "/$regex/i", $agent ); } public static function emailUrl($subject = null, $body = null) { $email = Yii::$app->params ['adminEmail']; return "mailto:$email?" . http_build_query ( [ 'subject' => "", "body" => $body ?: Yii::$app->request->absoluteUrl ] ); } } ?>