<?php
if (\think\Request::instance()->isMobile()) {
define('VIEW_PATH', __DIR__ . '/../application/admin/mobile/');
} else {
define('VIEW_PATH', __DIR__ . '/../application/admin/view/');
}
function tab($step = 1, $string = ' ', $size = 4){
return str_repeat($string, $size * $step);
}
function extraconfig($arr = [], $file)
{
if (is_array($arr)) {
$filename = $file . EXT;
$filepath = APP_PATH . 'extra/' . $filename;
if (!file_exists($filepath)) {
$conf = "<?php return [];";
file_put_contents($filepath, $conf);
}
$conf = include $filepath;
foreach ($arr as $key => $value) {
$conf[$key] = $value;
}
$time = date('Y/m/d H:i:s');
$str = "<?php\r\n/**\r\n * 由公共方法extraconfig建立.\r\n * $time\r\n */\r\nreturn [\r\n";
foreach ($conf as $key => $value) {
$str .= "\t'$key' => '$value',";
$str .= "\r\n";
}
$str .= '];';
file_put_contents($filepath, $str);
return true;
} else {
return false;
}
}
function json_validate($string)
{
return is_array(json_decode($string));
}
function is_array_vd($array)
{
$s = 1;
foreach ($array as $value) {
if (is_array($value)) {
$s = 2;
foreach ($value as $item) {
if (is_array($item)) {
$s = 3;
}
}
}
}
return $s;
}
function date_rang($start_date, $end_date)
{
return array_map(function ($n) {
return date('Y-m-d', $n);
}, range(strtotime($start_date), strtotime($end_date), 24 * 3600));
}
function get_week($date)
{
$date_str = date('Y-m-d', strtotime($date));
$arr = explode("-", $date_str);
$year = $arr[0];
$month = sprintf('%02d', $arr[1]);
$day = sprintf('%02d', $arr[2]);
$hour = $minute = $second = 0;
$strap = mktime($hour, $minute, $second, $month, $day, $year);
$number_wk = date("w", $strap);
$weekArr = array("日", "一", "二", "三", "四", "五", "六");
return $weekArr[$number_wk];
}
function getCondition()
{
return $usecondition = ['status' => 1, 'isdelete' => 0];
}
function gettimes($year)
{
$smonth = 1;
$emonth = 12;
$startTime = $year . '-' . $smonth . '-1 00:00:00';
$em = $year . '-' . $emonth . '-1 23:59:59';
$endTime = date('Y-m-t H:i:s', strtotime($em));
return array('stime' => strtotime($startTime), 'etime' => strtotime($endTime));
}
function getMonthTimeToStr($yearMonth)
{
$startTime = $yearMonth."-01";
$endTime = date('Y-m-d', strtotime("$startTime +1 month -1 day"));
return ['stime' => strtotime($startTime.' 00:00:00'), 'etime' => strtotime($endTime.' 23:59:59')];
}
function getIsoWeeksInYear($year)
{
$date = new DateTime;
$date->setISODate($year, 53);
return ($date->format("W") === "53" ? 53 : 52);
}
function weekday($custom_date)
{
$week_start = date('d-m-Y', strtotime('this week monday', $custom_date));
$week_end = date('d-m-Y', strtotime('this week sunday', $custom_date));
$week_array[0] = $week_start;
$week_array[1] = $week_end;
return $week_array;
}
function array_val_chunk($array, $week)
{
$list = array();
foreach ($array as $key => $val) {
$list[$val[$week]]["data"][] = $val;
}
$ret = array();
foreach ($list as $key => $value) {
array_push($ret, $value);
}
return $ret;
}
function mp3Time($file)
{
$m = new Mp3file($file);
$a = $m->get_metadata();
return $a['Length mm:ss'] ? $a['Length mm:ss'] : 0;
}
function mp3Info($file)
{
$m = new Mp3file($file);
return $m->get_metadata();
}
function isMobile($value)
{
$rule = '/^0?(13|14|15|16|17|18|19)[0-9]{9}$/';
$result = preg_match($rule, $value);
if ($result) {
return true;
} else {
return false;
}
}
function array_count_values_keys($arr, $key_arr)
{
$key = 0;
if ($key_arr) {
foreach ($arr as $kk => $vo) {
foreach ($key_arr as $k => $item) {
if ($vo == $item) {
$key++;
}
}
}
return $key;
}
}
function timediff($begin_time, $end_time)
{
if ($begin_time < $end_time) {
$starttime = $begin_time;
$endtime = $end_time;
} else {
$starttime = $end_time;
$endtime = $begin_time;
}
$timediff = $endtime - $starttime;
$days = intval($timediff / 86400);
$remain = $timediff % 86400;
$hours = intval($remain / 3600);
$remain = $remain % 3600;
$mins = intval($remain / 60);
$secs = $remain % 60;
$res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
return $res;
}
function in_array_string($arr, $string)
{
if (isset($arr[$string])) {
return $arr[$string];
}
return "";
}
function period_time()
{
$time = [];
$time["firstDay"][] = $firstBeginDay = date("Y-m-d 00:00:00", mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')));
$time["firstDay"][] = $firstEndDay = date("Y-m-d 23:59:59", mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1);
$time["Today"][] = date("Y-m-d 00:00:00", mktime(0, 0, 0, date('m'), date('d'), date('Y')));
$time["Today"][] = date("Y-m-d 23:59:59", mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1);
$time["lastday"][] = date("Y-m-d 00:00:00", mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')));
$time["lastday"][] = date("Y-m-d 23:59:59", mktime(23, 59, 59, date('m'), date('d') + 1, date('Y')));
$time["firstWeek"][] = date("Y-m-d 00:00:00", mktime(0, 0, 0, date('m'), date('d') - date('w') + 1 - 7, date('Y')));
$time["firstWeek"][] = date("Y-m-d 23:59:59", mktime(23, 59, 59, date('m'), date('d') - date('w') + 7 - 7, date('Y')));
$time["lastWeek"][] = date('Y-m-d 00:00:00',mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y")));
$time["lastWeek"][] = date('Y-m-d 23:59:59',mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
return $time;
}
function period_cbtime()
{
$time = [];
$time["firstDay"][] = $firstBeginDay = date("Y-m-d", mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')));
$time["firstDay"][] = $firstEndDay = date("Y-m-d", mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1);
$time["Today"][] = date("Y-m-d", mktime(0, 0, 0, date('m'), date('d'), date('Y')));
$time["Today"][] = date("Y-m-d", mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1);
$time["lastday"][] = date("Y-m-d", mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')));
$time["lastday"][] = date("Y-m-d", mktime(23, 59, 59, date('m'), date('d') + 1, date('Y')));
$data = get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=0, time());
$time["firstWeek"][] = $data[0];
$time["firstWeek"][] = $data[1];
$time["lastWeek"][] = date('Y-m-d',mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y")));
$time["lastWeek"][] = date('Y-m-d',mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
return $time;
};
function week()
{
$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
return [date('Y-m-d',$beginLastweek),date('Y-m-d',$endLastweek)];
}
function get_lastweek($begin = 'beginLastweek', $end = 'endLastweek', $week_start_num = 1, $now_time = '')
{
$beginWeek = mktime(0,0,0,date("m"),date("d")-date("w")+1-14,date("Y"));
$endWeek = mktime(23,59,59,date("m"),date("d")-date("w")+7-14,date("Y"));
return [date('Y-m-d',$beginWeek),date('Y-m-d',$endWeek)];
}
function _return_Ajax($msg, $code, $data)
{
$data["msg"] = $msg;
$data["code"] = $code;
$data["data"] = $data;
echo json_encode($data);
exit();
}
function deleteDir($dir)
{
if (!$handle = @opendir($dir)) {
return false;
}
while (false !== ($file = readdir($handle))) {
if ($file !== "." && $file !== "..") {
$file = $dir . '/' . $file;
if (is_dir($file)) {
deleteDir($file);
} else {
@unlink($file);
}
}
}
@rmdir($dir);
}
function in_array_key($arr, $keyval)
{
$renturn = [];
if (!empty($arr)) {
foreach ($arr as $k => $item) {
if ($keyval === $item) {
$renturn["val"] = $item;
$renturn["keyId"] = $k;
return $renturn;
}
}
}
}
function subtext($text, $length)
{
if (mb_strlen($text, 'utf8') > $length)
return mb_substr($text, 0, $length, 'utf8') . ' …';
return $text;
}
function all_external_link($text = '', $host = '')
{
if (strpos($text, $host) !== false) {
return true;
} else {
return false;
}
}
function curl_request($url, $post = '', $cookie = '', $returnCookie = 0)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
if ($post) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
}
if ($cookie) {
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
}
curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if (curl_errno($curl)) {
return curl_error($curl);
}
curl_close($curl);
if ($returnCookie) {
list($header, $body) = explode("\r\n\r\n", $data, 2);
preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
$info['cookie'] = substr($matches[1][0], 1);
$info['content'] = $body;
return $info;
} else {
return $data;
}
}
function _today()
{
$t = time();
$start = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));
$end = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t));
return $today = ['start' => $start, 'end' => $end];
}
function _verify_script($str){
$preg = "/<script[\s\S]*?<\/script>/i";
return preg_replace($preg, "", $str);
}
function timeRegular($get)
{
$preg = '/(?:19|20)?(?:[0-9]{2})?(?:-|\s|\/|.|年)?(?:0[1-9]|1[012])(?:-|\s|\/|.|月)(?:0[1-9]|[12][0-9]|3[01])(?:日)?\s?(?:[01][0-9]|2[1-4])?:?(?:[0-6]?[0-9])?:?(?:[0-6]?[0-9])?/i';
preg_match_all($preg, $get, $time);
return $time;
}
function is_mobile(){
if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
return true;
if (isset ($_SERVER['HTTP_CLIENT']) && 'PhoneClient' == $_SERVER['HTTP_CLIENT'])
return true;
if (isset ($_SERVER['HTTP_VIA']))
return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
if (isset ($_SERVER['HTTP_USER_AGENT'])) {
$clientkeywords = array(
'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile'
);
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
return true;
}
}
if (isset ($_SERVER['HTTP_ACCEPT'])) {
if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
return true;
}
}
return false;
}
;
function _day_arr($month, $year)
{
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$dayarr = [];
for ($i = 1; $i <= $days; $i++) {
$dayarr[$i] = $i;
}
return $dayarr;
}
function strLike($str1, $str2)
{
preg_match("/".$str2."/", $str1, $regs);
if (!empty($regs)) {
return true;
} else {
return false;
}
}
function getRepeat($arr, $keys)
{
$unique_arr = array();
$repeat_arr = array();
foreach ($arr as $k => $v) {
$str = "";
foreach ($keys as $a => $b) {
$str .= "{$v[$b]},";
}
if (!in_array($str, $unique_arr)) {
$unique_arr[] = $str;
} else {
$repeat_arr[$k] = $v;
}
}
return $repeat_arr;
}
function arrays_unique($arr, $key) {
$tmp_arr = array();
foreach ($arr as $k => $v) {
if (in_array($v[$key], $tmp_arr)) {
unset($arr[$k]);
} else {
$tmp_arr[] = $v[$key];
}
}
return $arr;
}
function array3_to_array2($array){
$array = array_filter($array);
$array = array_values($array);
foreach ($array as $orderKey =>$orderVal){
$count = count($orderVal);
if($count > 1){
for ($i = 0;$i < $count;$i++){
$tempArr[] = $orderVal[$i];
}
}else{
$tempArr[] = $orderVal[0];
}
}
return $tempArr;
}
function openfiles($data){
$myfile = fopen($data, "r") or die("Unable to open file!");
$content = fread($myfile,filesize($data));
fclose($myfile);
return $content;
}
function order_date_array($array, $order, $key){
if (!$array){
return [];
}else{
$_array = $array;
}
if (!$order){
$_order = 'desc';
}else{
$_order = $order;
}
if (!$key){
$_key = 'date';
}else{
$_key = $key;
}
$new_array = [];
$array_1 = [];
$array_2 = [];
for ($t=0; $t<count($_array); $t++){
$date = strtotime($_array[$t][$_key]);
$array_1[] = $date;
$array_2[] = $date;
}
if ($_order === 'desc'){
rsort($array_2);
}else{
sort($array_2);
}
for ($r=0; $r<count($array_2); $r++){
$index = array_search($array_2[$r], $array_1);
$new_array[] = $_array[$index];
}
return $new_array;
}
function dateToUnix($date){
$datetime = new \DateTime($date);
return $datetime->format('U');
}
function unixToTime($unix,$type="Y-m-d H:i:s"){
$unix = $unix+8*3600;
$datetime = new \DateTime("@$unix");
return $datetime->format($type);
}
function _getFirstCharter($str){
if(empty($str)){return '';}
$fchar=ord($str{0});
if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0});
try{
$s1=iconv('UTF-8','gb2312',$str);
$s2=iconv('gb2312','UTF-8',$s1);
}catch (\Exception $e){
$s1=iconv('UTF-8','GBK',$str);
$s2=iconv('GBK','UTF-8',$s1);
}
$s=$s2==$str?$s1:$str;
$asc=ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319&&$asc<=-20284) return 'A';
if($asc>=-20283&&$asc<=-19776) return 'B';
if($asc>=-19775&&$asc<=-19219) return 'C';
if($asc>=-19218&&$asc<=-18711) return 'D';
if($asc>=-18710&&$asc<=-18527) return 'E';
if($asc>=-18526&&$asc<=-18240) return 'F';
if($asc>=-18239&&$asc<=-17923) return 'G';
if($asc>=-17922&&$asc<=-17418) return 'H';
if($asc>=-17417&&$asc<=-16475) return 'J';
if($asc>=-16474&&$asc<=-16213) return 'K';
if($asc>=-16212&&$asc<=-15641) return 'L';
if($asc>=-15640&&$asc<=-15166) return 'M';
if($asc>=-15165&&$asc<=-14923) return 'N';
if($asc>=-14922&&$asc<=-14915) return 'O';
if($asc>=-14914&&$asc<=-14631) return 'P';
if($asc>=-14630&&$asc<=-14150) return 'Q';
if($asc>=-14149&&$asc<=-14091) return 'R';
if($asc>=-14090&&$asc<=-13319) return 'S';
if($asc>=-13318&&$asc<=-12839) return 'T';
if($asc>=-12838&&$asc<=-12557) return 'W';
if($asc>=-12556&&$asc<=-11848) return 'X';
if($asc>=-11847&&$asc<=-11056) return 'Y';
if($asc>=-11055&&$asc<=-10247) return 'Z';
return null;
}
function getVideoUrl_one($str){
$c = substr($str, 0, 10);
$d = substr($str, - 3, 1);
$newstr = substr($str, 0, strlen($str) - 1);
$video_url = 'http://mpv.videocc.net/' . $c . '/' . $d . '/' . $newstr . '2.mp4';
return $video_url;
}
function verifyStr($value,$type,$reg=''){
switch ($type){
case 'mobile':
$reg = '/^1[3-9]{1}[0-9]{9}$/';
break;
default:
break;
}
if (preg_match($reg,$value)){
return true;
}else{
return false;
}
}
function howOld($birth) {
list($birthYear, $birthMonth, $birthDay) = explode('-', $birth);
list($currentYear, $currentMonth, $currentDay) = explode('-', date('Y-m-d'));
$age = $currentYear - $birthYear - 1;
if($currentMonth > $birthMonth || $currentMonth == $birthMonth && $currentDay >= $birthDay)
$age++;
return $age;
}
function getAgeByBirth($date1,$date2,$baseNumber=0){
$datestart= date('Y-m-d',strtotime($date1));
if(strtotime($datestart)>strtotime($date2)){
$tmp=$date2;
$date2=$datestart;
$datestart=$tmp;
}
list($Y1,$m1,$d1)=explode('-',$datestart);
list($Y2,$m2,$d2)=explode('-',$date2);
$Y=$Y2-$Y1;
$m=$m2-$m1;
$d=$d2-$d1;
if($baseNumber != 0){
$_Y = (int)$baseNumber;
$_m = explode('.',$baseNumber);
$_m = (count($_m) == 1)?0:(int)substr($_m[1],0,1);
$_m = round(($_m*12)/10);
$Y += $_Y;
$m += $_m;
if($d > 20){
$m += 1;
}
$d = 0;
}
if($d<0){
$d = $d + (int)date('t',strtotime("-1 month $date2"));
$m = $m - 1;
}
if($m<0){
$m = $m + 12;
$Y = $Y - 1;
}
if($m>11){
$m = $m - 12;
$Y = $Y + 1;
}
if($Y == 0 && $m == 0 && $d != 0){
return $d.'天';
}elseif($Y == 0 && $m != 0 && $d != 0){
return $m.'个月'.$d.'天';
}elseif($Y != 0 && $m == 0 && $d != 0){
return $Y.'岁'.$d.'天';
}else{
return $Y.'岁'.$m.'个月';
}
}
function uniquArr($array,$item){
$result = array();
foreach($array as $k=>$val){
$code = false;
foreach($result as $_val){
if($_val[$item] == $val[$item] && $val[$item] != 0){
$code = true;
break;
}
}
if(!$code){
$result[]=$val;
}
}
return $result;
}
|