functions.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. <?php
  2. // +—————————————————————————————————————————————————————————————————————
  3. // | Created by Yunbao
  4. // +—————————————————————————————————————————————————————————————————————
  5. // | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
  6. // +—————————————————————————————————————————————————————————————————————
  7. // | Author: https://gitee.com/yunbaokeji
  8. // +—————————————————————————————————————————————————————————————————————
  9. // | Date: 2022-02-17
  10. // +—————————————————————————————————————————————————————————————————————
  11. /* Redis链接 */
  12. function connectionRedis(){
  13. $REDIS_HOST= DI()->config->get('app.REDIS_HOST');
  14. $REDIS_AUTH= DI()->config->get('app.REDIS_AUTH');
  15. $REDIS_PORT= DI()->config->get('app.REDIS_PORT');
  16. $redis = new Redis();
  17. $redis -> pconnect($REDIS_HOST,$REDIS_PORT);
  18. if (strlen($REDIS_AUTH)>0){
  19. $redis -> auth($REDIS_AUTH);
  20. }
  21. return $redis;
  22. }
  23. /* 设置缓存 */
  24. function setcache($key,$info){
  25. $config=getConfigPri();
  26. if($config['cache_switch']!=1){
  27. return 1;
  28. }
  29. DI()->redis->set($key,json_encode($info));
  30. DI()->redis->expire($key, $config['cache_time']);
  31. return 1;
  32. }
  33. /* 设置缓存 可自定义时间*/
  34. function setcaches($key,$info,$time=0){
  35. DI()->redis->set($key,json_encode($info));
  36. if($time > 0){
  37. DI()->redis->expire($key, $time);
  38. }
  39. return 1;
  40. }
  41. /* 获取缓存 */
  42. function getcache($key){
  43. $config=getConfigPri();
  44. if($config['cache_switch']!=1){
  45. $isexist=false;
  46. }else{
  47. $isexist=DI()->redis->Get($key);
  48. }
  49. return json_decode($isexist,true);
  50. }
  51. /* 获取缓存 不判断后台设置 */
  52. function getcaches($key){
  53. $isexist=DI()->redis->Get($key);
  54. return json_decode($isexist,true);
  55. }
  56. /* 删除缓存 */
  57. function delcache($key){
  58. $isexist=DI()->redis->del($key);
  59. return 1;
  60. }
  61. /* 密码检查 */
  62. function passcheck($user_pass) {
  63. /* 必须包含字母、数字 */
  64. $preg='/^(?=.*[A-Za-z])(?=.*[0-9])[a-zA-Z0-9~!@&%#_]{6,20}$/';
  65. $isok=preg_match($preg,$user_pass);
  66. if($isok){
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. /* 检验手机号 */
  72. function checkMobile($mobile){
  73. $ismobile = preg_match("/^1[3|4|5|6|7|8|9]\d{9}$/",$mobile);
  74. if($ismobile){
  75. return 1;
  76. }else{
  77. return 0;
  78. }
  79. }
  80. /* 随机数 */
  81. function random($length = 6 , $numeric = 0) {
  82. PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
  83. if($numeric) {
  84. $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
  85. } else {
  86. $hash = '';
  87. $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
  88. $max = strlen($chars) - 1;
  89. for($i = 0; $i < $length; $i++) {
  90. $hash .= $chars[mt_rand(0, $max)];
  91. }
  92. }
  93. return $hash;
  94. }
  95. function Post($curlPost,$url){
  96. $curl = curl_init();
  97. curl_setopt($curl, CURLOPT_URL, $url);
  98. curl_setopt($curl, CURLOPT_HEADER, false);
  99. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  100. curl_setopt($curl, CURLOPT_NOBODY, true);
  101. curl_setopt($curl, CURLOPT_POST, true);
  102. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  103. $return_str = curl_exec($curl);
  104. curl_close($curl);
  105. return $return_str;
  106. }
  107. function xml_to_array($xml){
  108. $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
  109. if(preg_match_all($reg, $xml, $matches)){
  110. $count = count($matches[0]);
  111. for($i = 0; $i < $count; $i++){
  112. $subxml= $matches[2][$i];
  113. $key = $matches[1][$i];
  114. if(preg_match( $reg, $subxml )){
  115. $arr[$key] = xml_to_array( $subxml );
  116. }else{
  117. $arr[$key] = $subxml;
  118. }
  119. }
  120. }
  121. return $arr;
  122. }
  123. /* 发送验证码*/
  124. function sendCode($mobile,$code){
  125. $rs = array('code' => 0, 'msg' => '', 'info' => array());
  126. $config = getConfigPri();
  127. if(!$config['sendcode_switch']){
  128. $rs['code']=667;
  129. $rs['msg']='123456';
  130. return $rs;
  131. }
  132. $res=sendCodeByTencentSms($country_code,$mobile,$code);//腾讯云
  133. return $res;
  134. }
  135. //腾讯云短信
  136. function sendCodeByTencentSms($nationCode,$mobile,$code){
  137. require_once API_ROOT."/../sdk/tencentSms/index.php";
  138. $rs=array();
  139. $configpri = getConfigPri();
  140. $appid=$configpri['tencent_sms_appid'];
  141. $appkey=$configpri['tencent_sms_appkey'];
  142. $smsSign_dl = $configpri['tencent_sms_signName'];
  143. $templateId_dl=$configpri['tencent_sms_templateCode'];
  144. $smsSign = $smsSign_dl;
  145. $templateId = $templateId_dl;
  146. $sender = new \Qcloud\Sms\SmsSingleSender($appid,$appkey);
  147. $params = [$code]; //参数列表与腾讯云后台创建模板时加的参数列表保持一致
  148. $result = $sender->sendWithParam($nationCode, $mobile, $templateId, $params, $smsSign, "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
  149. //file_put_contents(API_ROOT.'/../log/sendCode_tencent_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 提交参数信息 result:'.json_encode($result)."\r\n",FILE_APPEND);
  150. $arr=json_decode($result,TRUE);
  151. if($arr['result']==0 && $arr['errmsg']=='OK'){
  152. //setSendcode(array('type'=>'1','account'=>$mobile,'content'=>"验证码:".$code."---国家区号:".$nationCode));
  153. $rs['code']=0;
  154. }else{
  155. $rs['code']=1002;
  156. $rs['msg']=$arr['errmsg'];
  157. // $rs['msg']='验证码发送失败';
  158. }
  159. return $rs;
  160. }
  161. /* curl get请求 */
  162. function curl_get($url){
  163. $curl = curl_init();
  164. curl_setopt($curl, CURLOPT_URL, $url);
  165. curl_setopt($curl, CURLOPT_HEADER, false);
  166. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  167. curl_setopt($curl, CURLOPT_NOBODY, true);
  168. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  169. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
  170. $return_str = curl_exec($curl);
  171. curl_close($curl);
  172. return $return_str;
  173. }
  174. /* 检测文件后缀 */
  175. function checkExt($filename){
  176. $config=array("jpg","png","jpeg");
  177. $ext = pathinfo(strip_tags($filename), PATHINFO_EXTENSION);
  178. return empty($config) ? true : in_array(strtolower($ext), $config);
  179. }
  180. /* 密码加密 */
  181. function setPass($pass){
  182. $authcode='rCt52pF2cnnKNB3Hkp';
  183. $pass="###".md5(md5($authcode.$pass));
  184. return $pass;
  185. }
  186. /* 去除NULL 判断空处理 主要针对字符串类型*/
  187. function checkNull($checkstr){
  188. $checkstr=trim($checkstr);
  189. $checkstr=urldecode($checkstr);
  190. if(get_magic_quotes_gpc()==0){
  191. $checkstr=addslashes($checkstr);
  192. }
  193. if( strstr($checkstr,'null') || (!$checkstr && $checkstr!=0 ) ){
  194. $str='';
  195. }else{
  196. $str=$checkstr;
  197. }
  198. $str=htmlspecialchars($str);
  199. return $str;
  200. }
  201. /* 去除emoji表情 */
  202. function filterEmoji($str){
  203. $str = preg_replace_callback(
  204. '/./u',
  205. function (array $match) {
  206. return strlen($match[0]) >= 4 ? '' : $match[0];
  207. },
  208. $str);
  209. return $str;
  210. }
  211. /* 公共配置 */
  212. function getConfigPub() {
  213. $key='getConfigPub';
  214. $config=getcaches($key);
  215. $config=false;
  216. if(!$config){
  217. $config= DI()->notorm->option
  218. ->select('option_value')
  219. ->where("option_name='site_info'")
  220. ->fetchOne();
  221. $config=json_decode($config['option_value'],true);
  222. if($config){
  223. setcaches($key,$config);
  224. }
  225. }
  226. if(isset($config['live_time_coin'])){
  227. if(is_array($config['live_time_coin'])){
  228. }else if($config['live_time_coin']){
  229. $config['live_time_coin']=preg_split('/,|,/',$config['live_time_coin']);
  230. }else{
  231. $config['live_time_coin']=array();
  232. }
  233. }else{
  234. $config['live_time_coin']=array();
  235. }
  236. if(isset($config['login_type'])){
  237. if(is_array($config['login_type'])){
  238. }else if($config['login_type']){
  239. $config['login_type']=preg_split('/,|,/',$config['login_type']);
  240. }else{
  241. $config['login_type']=array();
  242. }
  243. }else{
  244. $config['login_type']=array();
  245. }
  246. if(isset($config['live_type'])){
  247. if(is_array($config['live_type'])){
  248. }else if($config['live_type']){
  249. $live_type=preg_split('/,|,/',$config['live_type']);
  250. foreach($live_type as $k=>$v){
  251. $live_type[$k]=preg_split('/;|;/',$v);
  252. }
  253. $config['live_type']=$live_type;
  254. }else{
  255. $config['live_type']=array();
  256. }
  257. }else{
  258. $config['live_type']=array();
  259. }
  260. return $config;
  261. }
  262. /* 私密配置 */
  263. function getConfigPri() {
  264. $key='getConfigPri';
  265. $config=getcaches($key);
  266. if(!$config){
  267. $config= DI()->notorm->option
  268. ->select('option_value')
  269. ->where("option_name='configpri'")
  270. ->fetchOne();
  271. $config=json_decode($config['option_value'],true);
  272. if($config){
  273. setcaches($key,$config);
  274. }
  275. }
  276. return $config;
  277. }
  278. /**
  279. * 返回带协议的域名
  280. */
  281. function get_host(){
  282. $config=getConfigPub();
  283. return $config['site'];
  284. }
  285. /**
  286. * 转化数据库保存的文件路径,为可以访问的url
  287. */
  288. function get_upload_path($file){
  289. if($file==''){
  290. return $file;
  291. }
  292. if(strpos($file,"http")===0){
  293. return html_entity_decode($file);
  294. }else if(strpos($file,"/")===0){
  295. $filepath= get_host().$file;
  296. return html_entity_decode($filepath);
  297. }else{
  298. $fileinfo=explode("_",$file);//上传云存储标识:qiniu:七牛云;aws:亚马逊
  299. $storage_type=$fileinfo[0];
  300. $start=strlen($storage_type)+1;
  301. if($storage_type=='qiniu'){ //七牛云
  302. $space_host= DI()->config->get('app.Qiniu.space_host');
  303. $file=substr($file,$start);
  304. $filepath=$space_host."/".$file;
  305. return html_entity_decode($filepath);
  306. }else{
  307. $uptype=DI()->config->get('app.uptype');
  308. if($uptype==1){
  309. $space_host= DI()->config->get('app.Qiniu.space_host');
  310. $filepath=$space_host."/".$file;
  311. }else{
  312. $filepath= get_host().'/upload/'.$file;
  313. }
  314. return html_entity_decode($filepath);
  315. }
  316. }
  317. }
  318. /* 判断权限 */
  319. function isAdmin($uid,$liveuid) {
  320. if($uid<0){
  321. return 30;
  322. }
  323. if($uid==$liveuid){
  324. return 50;
  325. }
  326. $isuper=isSuper($uid);
  327. if($isuper){
  328. return 60;
  329. }
  330. $isexist=DI()->notorm->live_manager
  331. ->select("*")
  332. ->where('uid=? and liveuid=?',$uid,$liveuid)
  333. ->fetchOne();
  334. if($isexist){
  335. return 40;
  336. }
  337. return 30;
  338. }
  339. /* 判断token */
  340. function checkToken($uid,$token) {
  341. $userinfo=getcaches("token_".$uid);
  342. if(!$userinfo){
  343. $userinfo=DI()->notorm->user_token
  344. ->select('token,expire_time')
  345. ->where('user_id = ?', $uid)
  346. ->fetchOne();
  347. if($userinfo){
  348. // setcaches("token_".$uid,$userinfo);
  349. }
  350. }
  351. if(!$userinfo || $userinfo['token']!=$token || $userinfo['expire_time']<time()){
  352. return 700;
  353. }
  354. return 0;
  355. }
  356. /* 用户基本信息 */
  357. function getUserInfo($uid,$type=0) {
  358. if($uid==0){
  359. if($uid==='goodsorder_admin'){
  360. $configpub=getConfigPub();
  361. $info['user_nicename']="订单消息";
  362. $info['avatar']=get_upload_path('/orderMsg.png');
  363. $info['avatar_thumb']=get_upload_path('/orderMsg.png');
  364. $info['id']="goodsorder_admin";
  365. }
  366. $info['coin']="0";
  367. $info['sex']="1";
  368. $info['signature']='';
  369. $info['province']='';
  370. $info['city']='城市未填写';
  371. $info['birthday']='';
  372. $info['issuper']="0";
  373. $info['votestotal']="0";
  374. $info['consumption']="0";
  375. $info['location']='';
  376. $info['user_status']='1';
  377. }else{
  378. $info=getcaches("userinfo_".$uid);
  379. if($uid>0){
  380. $info=false;
  381. }
  382. if(!$info){
  383. $info=DI()->notorm->user
  384. ->select('id,user_nicename,avatar,avatar_thumb,sex,signature,consumption,votestotal,province,city,birthday,user_status,location')
  385. ->where('id=? and user_type="2"',$uid)
  386. ->fetchOne();
  387. if($info){
  388. }else if($type==1){
  389. return $info;
  390. }else{
  391. $info['id']=$uid;
  392. $info['user_nicename']='用户不存在';
  393. $info['avatar']='/default.jpg';
  394. $info['avatar_thumb']='/default_thumb.jpg';
  395. $info['sex']='0';
  396. $info['signature']='';
  397. $info['consumption']='0';
  398. $info['votestotal']='0';
  399. $info['province']='';
  400. $info['city']='';
  401. $info['birthday']='';
  402. $info['issuper']='0';
  403. $info['user_status']='1';
  404. $info['location']='';
  405. }
  406. if($uid<0){
  407. $info['user_nicename']='游客';
  408. }
  409. if($info){
  410. setcaches("userinfo_".$uid,$info);
  411. }
  412. }
  413. if($info){
  414. $info['level']=getLevel($info['consumption']);
  415. $info['level_anchor']=getLevelAnchor($info['votestotal']);
  416. $info['avatar']=get_upload_path($info['avatar']);
  417. $info['avatar_thumb']=get_upload_path($info['avatar_thumb']);
  418. if($info['birthday']){
  419. $info['birthday']=date('Y-m-d',$info['birthday']);
  420. }else{
  421. $info['birthday']='';
  422. }
  423. }
  424. }
  425. return $info;
  426. }
  427. /* 会员等级 */
  428. function getLevelList(){
  429. $key='level';
  430. $level=getcaches($key);
  431. if(!$level){
  432. $level=DI()->notorm->level
  433. ->select("*")
  434. ->order("level_up asc")
  435. ->fetchAll();
  436. if($level){
  437. setcaches($key,$level);
  438. }
  439. }
  440. foreach($level as $k=>$v){
  441. $v['thumb']=get_upload_path($v['thumb']);
  442. $v['thumb_mark']=get_upload_path($v['thumb_mark']);
  443. $v['bg']=get_upload_path($v['bg']);
  444. if($v['colour']){
  445. $v['colour']='#'.$v['colour'];
  446. }else{
  447. $v['colour']='#ffdd00';
  448. }
  449. $level[$k]=$v;
  450. }
  451. return $level;
  452. }
  453. function getLevel($experience){
  454. $levelid=1;
  455. $level_a=1;
  456. $level=getLevelList();
  457. foreach($level as $k=>$v){
  458. if( $v['level_up']>=$experience){
  459. $levelid=$v['levelid'];
  460. break;
  461. }else{
  462. $level_a = $v['levelid'];
  463. }
  464. }
  465. $levelid = $levelid < $level_a ? $level_a:$levelid;
  466. return (string)$levelid;
  467. }
  468. /* 主播等级 */
  469. function getLevelAnchorList(){
  470. $key='levelanchor';
  471. $level=getcaches($key);
  472. if(!$level){
  473. $level=DI()->notorm->level_anchor
  474. ->select("*")
  475. ->order("level_up asc")
  476. ->fetchAll();
  477. if($level){
  478. setcaches($key,$level);
  479. }
  480. }
  481. foreach($level as $k=>$v){
  482. $v['thumb']=get_upload_path($v['thumb']);
  483. $v['thumb_mark']=get_upload_path($v['thumb_mark']);
  484. $v['bg']=get_upload_path($v['bg']);
  485. $level[$k]=$v;
  486. }
  487. return $level;
  488. }
  489. function getLevelAnchor($experience){
  490. $levelid=1;
  491. $level_a=1;
  492. $level=getLevelAnchorList();
  493. foreach($level as $k=>$v){
  494. if( $v['level_up']>=$experience){
  495. $levelid=$v['levelid'];
  496. break;
  497. }else{
  498. $level_a = $v['levelid'];
  499. }
  500. }
  501. $levelid = $levelid < $level_a ? $level_a:$levelid;
  502. return (string)$levelid;
  503. }
  504. /* 统计 直播 */
  505. function getLives($uid) {
  506. /* 直播中 */
  507. $count1=DI()->notorm->live
  508. ->where('uid=? and islive="1"',$uid)
  509. ->count();
  510. /* 回放 */
  511. $count2=DI()->notorm->live_record
  512. ->where('uid=? ',$uid)
  513. ->count();
  514. return $count1+$count2;
  515. }
  516. /* 统计 关注 */
  517. function getFollows($uid) {
  518. $count=DI()->notorm->user_attention
  519. ->where('uid=? ',$uid)
  520. ->count();
  521. return $count;
  522. }
  523. /* 统计 粉丝 */
  524. function getFans($uid) {
  525. $count=DI()->notorm->user_attention
  526. ->where('touid=? ',$uid)
  527. ->count();
  528. return $count;
  529. }
  530. /**
  531. * @desc 根据两点间的经纬度计算距离
  532. * @param float $lat 纬度值
  533. * @param float $lng 经度值
  534. */
  535. function getDistance($lat1, $lng1, $lat2, $lng2){
  536. $earthRadius = 6371000; //近似地球半径 单位 米
  537. /*
  538. Convert these degrees to radians
  539. to work with the formula
  540. */
  541. $lat1 = ($lat1 * pi() ) / 180;
  542. $lng1 = ($lng1 * pi() ) / 180;
  543. $lat2 = ($lat2 * pi() ) / 180;
  544. $lng2 = ($lng2 * pi() ) / 180;
  545. $calcLongitude = $lng2 - $lng1;
  546. $calcLatitude = $lat2 - $lat1;
  547. $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2); $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
  548. $calculatedDistance = $earthRadius * $stepTwo;
  549. $distance=$calculatedDistance/1000;
  550. if($distance<10){
  551. $rs=round($distance,2);
  552. }else if($distance > 1000){
  553. $rs='1000';
  554. }else{
  555. $rs=round($distance);
  556. }
  557. return $rs.'km';
  558. }
  559. /* 判断账号是否禁用 */
  560. function isBanBF($uid){
  561. $status=DI()->notorm->user
  562. ->select("user_status")
  563. ->where('id=?',$uid)
  564. ->fetchOne();
  565. if(!$status || $status['user_status']==0){
  566. return '0';
  567. }
  568. return '1';
  569. }
  570. /* 是否认证 */
  571. function isAuth($uid){
  572. $status=DI()->notorm->user_auth
  573. ->select("status")
  574. ->where('uid=?',$uid)
  575. ->fetchOne();
  576. if($status && $status['status']==1){
  577. return '1';
  578. }
  579. return '0';
  580. }
  581. /* 过滤字符 */
  582. function filterField($field){
  583. $configpri=getConfigPri();
  584. $sensitive_field=$configpri['sensitive_field'];
  585. $sensitive=explode(",",$sensitive_field);
  586. $replace=array();
  587. $preg=array();
  588. foreach($sensitive as $k=>$v){
  589. if($v!=''){
  590. $re='';
  591. $num=mb_strlen($v);
  592. for($i=0;$i<$num;$i++){
  593. $re.='*';
  594. }
  595. $replace[$k]=$re;
  596. $preg[$k]='/'.$v.'/';
  597. }else{
  598. unset($sensitive[$k]);
  599. }
  600. }
  601. return preg_replace($preg,$replace,$field);
  602. }
  603. /* 时间差计算 */
  604. function datetime($time){
  605. $cha=time()-$time;
  606. $iz=floor($cha/60);
  607. $hz=floor($iz/60);
  608. $dz=floor($hz/24);
  609. /* 秒 */
  610. $s=$cha%60;
  611. /* 分 */
  612. $i=floor($iz%60);
  613. /* 时 */
  614. $h=floor($hz/24);
  615. /* 天 */
  616. if($cha<60){
  617. return $cha.'秒前';
  618. }else if($iz<60){
  619. return $iz.'分钟前';
  620. }else if($hz<24){
  621. return $hz.'小时'.$i.'分钟前';
  622. }else if($dz<30){
  623. return $dz.'天前';
  624. }else{
  625. return date("Y-m-d",$time);
  626. }
  627. }
  628. /* 时长格式化 */
  629. function getSeconds($time,$type=0){
  630. if(!$time){
  631. return (string)$time;
  632. }
  633. $value = array(
  634. "years" => 0,
  635. "days" => 0,
  636. "hours" => 0,
  637. "minutes" => 0,
  638. "seconds" => 0
  639. );
  640. if($time >= 31556926){
  641. $value["years"] = floor($time/31556926);
  642. $time = ($time%31556926);
  643. }
  644. if($time >= 86400){
  645. $value["days"] = floor($time/86400);
  646. $time = ($time%86400);
  647. }
  648. if($time >= 3600){
  649. $value["hours"] = floor($time/3600);
  650. $time = ($time%3600);
  651. }
  652. if($time >= 60){
  653. $value["minutes"] = floor($time/60);
  654. $time = ($time%60);
  655. }
  656. $value["seconds"] = floor($time);
  657. if($value['years']){
  658. if($type==1&&$value['years']<10){
  659. $value['years']='0'.$value['years'];
  660. }
  661. }
  662. if($value['days']){
  663. if($type==1&&$value['days']<10){
  664. $value['days']='0'.$value['days'];
  665. }
  666. }
  667. if($value['hours']){
  668. if($type==1&&$value['hours']<10){
  669. $value['hours']='0'.$value['hours'];
  670. }
  671. }
  672. if($value['minutes']){
  673. if($type==1&&$value['minutes']<10){
  674. $value['minutes']='0'.$value['minutes'];
  675. }
  676. }
  677. if($value['seconds']){
  678. if($type==1&&$value['seconds']<10){
  679. $value['seconds']='0'.$value['seconds'];
  680. }
  681. }
  682. if($value['years']){
  683. $t=$value["years"] ."年".$value["days"] ."天". $value["hours"] ."小时". $value["minutes"] ."分".$value["seconds"]."秒";
  684. }else if($value['days']){
  685. $t=$value["days"] ."天". $value["hours"] ."小时". $value["minutes"] ."分".$value["seconds"]."秒";
  686. }else if($value['hours']){
  687. $t=$value["hours"] ."小时". $value["minutes"] ."分".$value["seconds"]."秒";
  688. }else if($value['minutes']){
  689. $t=$value["minutes"] ."分".$value["seconds"]."秒";
  690. }else if($value['seconds']){
  691. $t=$value["seconds"]."秒";
  692. }
  693. return $t;
  694. }
  695. /* 数字格式化 */
  696. function NumberFormat($num){
  697. if($num<10000){
  698. }else if($num<1000000){
  699. $num=round($num/10000,2).'万';
  700. }else if($num<100000000){
  701. $num=round($num/10000,1).'万';
  702. }else if($num<10000000000){
  703. $num=round($num/100000000,2).'亿';
  704. }else{
  705. $num=round($num/100000000,1).'亿';
  706. }
  707. return $num;
  708. }
  709. /**
  710. * @desc 获取推拉流地址
  711. * @param string $host 协议,如:http、rtmp
  712. * @param string $stream 流名,如有则包含 .flv、.m3u8
  713. * @param int $type 类型,0表示播流,1表示推流
  714. */
  715. function PrivateKeyA($host,$stream,$type){
  716. $url=PrivateKey_tx($host,$stream,$type);
  717. return $url;
  718. }
  719. /**
  720. * @desc 腾讯云推拉流地址
  721. * @param string $host 协议,如:http、rtmp
  722. * @param string $stream 流名,如有则包含 .flv、.m3u8
  723. * @param int $type 类型,0表示播流,1表示推流
  724. */
  725. function PrivateKey_tx($host,$stream,$type){
  726. $configpri=getConfigPri();
  727. $bizid=$configpri['tx_bizid'];
  728. $push_url_key=$configpri['tx_push_key'];
  729. $play_url_key=$configpri['tx_play_key'];
  730. $push=$configpri['tx_push'];
  731. $pull=$configpri['tx_pull'];
  732. $stream_a=explode('.',$stream);
  733. $streamKey = $stream_a[0];
  734. //$live_code = $bizid . "_" .$streamKey;
  735. $live_code = $streamKey;
  736. $now=time();
  737. $now_time = $now + 3*60*60;
  738. $txTime = dechex($now_time);
  739. $txSecret = md5($push_url_key . $live_code . $txTime);
  740. $safe_url = "?txSecret=" .$txSecret."&txTime=" .$txTime;
  741. $play_safe_url='';
  742. //后台开启了播流鉴权
  743. if($configpri['tx_play_key_switch']){
  744. //播流鉴权时间
  745. $play_auth_time=$now+(int)$configpri['tx_play_time'];
  746. $txPlayTime = dechex($play_auth_time);
  747. $txPlaySecret = md5($play_url_key . $live_code . $txPlayTime);
  748. $play_safe_url = "?txSecret=" .$txPlaySecret."&txTime=" .$txPlayTime;
  749. }
  750. if($type==1){
  751. //$push_url = "rtmp://" . $bizid . ".livepush2.myqcloud.com/live/" . $live_code . "?bizid=" . $bizid . "&record=flv" .$safe_url; 可录像
  752. $url = "rtmp://{$push}/live/" . $live_code . $safe_url;
  753. }else{
  754. $url = "http://{$pull}/live/" . $live_code . ".flv".$play_safe_url;
  755. //$url = "http://{$pull}/live/" . $live_code . ".".$ext.$play_safe_url;(废弃)
  756. }
  757. return $url;
  758. }
  759. /* ip限定 */
  760. function ip_limit(){
  761. $configpri=getConfigPri();
  762. if($configpri['iplimit_switch']==0){
  763. return 0;
  764. }
  765. $date = date("Ymd");
  766. $ip= ip2long($_SERVER["REMOTE_ADDR"]) ;
  767. $isexist=DI()->notorm->getcode_limit_ip
  768. ->select('ip,date,times')
  769. ->where(' ip=? ',$ip)
  770. ->fetchOne();
  771. if(!$isexist){
  772. $data=array(
  773. "ip" => $ip,
  774. "date" => $date,
  775. "times" => 1,
  776. );
  777. $isexist=DI()->notorm->getcode_limit_ip->insert($data);
  778. return 0;
  779. }elseif($date == $isexist['date'] && $isexist['times'] >= $configpri['iplimit_times'] ){
  780. return 1;
  781. }else{
  782. if($date == $isexist['date']){
  783. $isexist=DI()->notorm->getcode_limit_ip
  784. ->where(' ip=? ',$ip)
  785. ->update(array('times'=> new NotORM_Literal("times + 1 ")));
  786. return 0;
  787. }else{
  788. $isexist=DI()->notorm->getcode_limit_ip
  789. ->where(' ip=? ',$ip)
  790. ->update(array('date'=> $date ,'times'=>1));
  791. return 0;
  792. }
  793. }
  794. }
  795. /* 检测用户是否存在 */
  796. function checkUser($where){
  797. if($where==''){
  798. return 0;
  799. }
  800. $isexist=DI()->notorm->user->where($where)->fetchOne();
  801. if($isexist){
  802. return 1;
  803. }
  804. return 0;
  805. }
  806. /* 直播分类 */
  807. function getLiveClass(){
  808. $key="getLiveClass";
  809. $list=getcaches($key);
  810. if(!$list){
  811. $list=DI()->notorm->live_class
  812. ->select("*")
  813. ->order("list_order asc,id desc")
  814. ->fetchAll();
  815. if($list){
  816. setcaches($key,$list);
  817. }
  818. }
  819. foreach($list as $k=>$v){
  820. $v['thumb']=get_upload_path($v['thumb']);
  821. $list[$k]=$v;
  822. }
  823. return $list;
  824. }
  825. /* 校验签名 */
  826. function checkSign($data,$sign){
  827. $key=DI()->config->get('app.sign_key');
  828. $str='';
  829. ksort($data);
  830. foreach($data as $k=>$v){
  831. $str.=$k.'='.$v.'&';
  832. }
  833. $str.=$key;
  834. $newsign=md5($str);
  835. //file_put_contents("33333.txt", $newsign);
  836. /*var_dump($newsign);
  837. die;*/
  838. if($sign==$newsign){
  839. return 1;
  840. }
  841. return 0;
  842. }
  843. /* 时长格式化 */
  844. function getBanSeconds($cha,$type=0){
  845. $iz=floor($cha/60);
  846. $hz=floor($iz/60);
  847. $dz=floor($hz/24);
  848. /* 秒 */
  849. $s=$cha%60;
  850. /* 分 */
  851. $i=floor($iz%60);
  852. /* 时 */
  853. $h=floor($hz/24);
  854. /* 天 */
  855. if($type==1){
  856. if($s<10){
  857. $s='0'.$s;
  858. }
  859. if($i<10){
  860. $i='0'.$i;
  861. }
  862. if($h<10){
  863. $h='0'.$h;
  864. }
  865. if($hz<10){
  866. $hz='0'.$hz;
  867. }
  868. return $hz.':'.$i.':'.$s;
  869. }
  870. if($cha<60){
  871. return $cha.'秒';
  872. }else if($iz<60){
  873. return $iz.'分钟'.$s.'秒';
  874. }else if($hz<24){
  875. return $hz.'小时'.$i.'分钟';
  876. }else if($dz<30){
  877. return $dz.'天'.$h.'小时';
  878. }
  879. }
  880. /* 过滤:敏感词 */
  881. function sensitiveField($field){
  882. if($field){
  883. $configpri=getConfigPri();
  884. $sensitive_words=$configpri['sensitive_words'];
  885. $sensitive=explode(",",$sensitive_words);
  886. $replace=array();
  887. $preg=array();
  888. foreach($sensitive as $k=>$v){
  889. if($v!=''){
  890. if(strstr($field, $v)!==false){
  891. return 1001;
  892. }
  893. }else{
  894. unset($sensitive[$k]);
  895. }
  896. }
  897. }
  898. return 1;
  899. }
  900. /* 处理直播信息 */
  901. function handleLive($v){
  902. $configpri=getConfigPri();
  903. $nums=DI()->redis->zCard('user_'.$v['stream']);
  904. $v['nums']=(string)$nums;
  905. $userinfo=getUserInfo($v['uid']);
  906. $v['avatar']=$userinfo['avatar'];
  907. $v['avatar_thumb']=$userinfo['avatar_thumb'];
  908. $v['user_nicename']=$userinfo['user_nicename'];
  909. $v['sex']=$userinfo['sex'];
  910. $v['level']=$userinfo['level'];
  911. $v['level_anchor']=$userinfo['level_anchor'];
  912. if(!$v['thumb']){
  913. $v['thumb']=$v['avatar'];
  914. }
  915. if($v['isvideo']==0 ){
  916. $v['pull']=PrivateKeyA('rtmp',$v['stream'],0);
  917. }
  918. if($v['type']==1){
  919. $v['type_val']='';
  920. }
  921. $v['thumb']=get_upload_path($v['thumb']);
  922. return $v;
  923. }
  924. /**
  925. * 判断是否为合法的身份证号码
  926. * @param $mobile
  927. * @return int
  928. */
  929. function isCreditNo($vStr){
  930. $vCity = array(
  931. '11','12','13','14','15','21','22',
  932. '23','31','32','33','34','35','36',
  933. '37','41','42','43','44','45','46',
  934. '50','51','52','53','54','61','62',
  935. '63','64','65','71','81','82','91'
  936. );
  937. if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)){
  938. return false;
  939. }
  940. if (!in_array(substr($vStr, 0, 2), $vCity)){
  941. return false;
  942. }
  943. $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
  944. $vLength = strlen($vStr);
  945. if($vLength == 18){
  946. $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
  947. }else{
  948. $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
  949. }
  950. if(date('Y-m-d', strtotime($vBirthday)) != $vBirthday){
  951. return false;
  952. }
  953. if ($vLength == 18) {
  954. $vSum = 0;
  955. for ($i = 17 ; $i >= 0 ; $i--) {
  956. $vSubStr = substr($vStr, 17 - $i, 1);
  957. $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
  958. }
  959. if($vSum % 11 != 1){
  960. return false;
  961. }
  962. }
  963. return true;
  964. }
  965. /**
  966. * post提交数据
  967. * @param string $url 请求Url
  968. * @param array $datas 提交的数据
  969. * @return url响应返回的html
  970. */
  971. function sendPost_KDN($url, $datas) {
  972. $temps = array();
  973. foreach ($datas as $key => $value) {
  974. $temps[] = sprintf('%s=%s', $key, $value);
  975. }
  976. $post_data = implode('&', $temps);
  977. $url_info = parse_url($url);
  978. if(empty($url_info['port']))
  979. {
  980. $url_info['port']=80;
  981. }
  982. $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
  983. $httpheader.= "Host:" . $url_info['host'] . "\r\n";
  984. $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
  985. $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
  986. $httpheader.= "Connection:close\r\n\r\n";
  987. $httpheader.= $post_data;
  988. $fd = fsockopen($url_info['host'], $url_info['port']);
  989. fwrite($fd, $httpheader);
  990. $gets = "";
  991. $headerFlag = true;
  992. while (!feof($fd)) {
  993. if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
  994. break;
  995. }
  996. }
  997. while (!feof($fd)) {
  998. $gets.= fread($fd, 128);
  999. }
  1000. fclose($fd);
  1001. return $gets;
  1002. }
  1003. function is_true($val, $return_null=false){
  1004. $boolval = ( is_string($val) ? filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) : (bool) $val );
  1005. return ( $boolval===null && !$return_null ? false : $boolval );
  1006. }
  1007. /* 时长格式化 */
  1008. function secondsFormat($time){
  1009. $now=time();
  1010. $cha=$now-$time;
  1011. if($cha<60){
  1012. return '刚刚';
  1013. }
  1014. if($cha>=4*24*60*60){ //超过4天
  1015. $now_year=date('Y',$now);
  1016. $time_year=date('Y',$time);
  1017. if($now_year==$time_year){
  1018. return date("m月d日",$time);
  1019. }else{
  1020. return date("Y年m月d日",$time);
  1021. }
  1022. }else{
  1023. $iz=floor($cha/60);
  1024. $hz=floor($iz/60);
  1025. $dz=floor($hz/24);
  1026. if($dz>3){
  1027. return '3天前';
  1028. }else if($dz>2){
  1029. return '2天前';
  1030. }else if($dz>1){
  1031. return '1天前';
  1032. }
  1033. if($hz>1){
  1034. return $hz.'小时前';
  1035. }
  1036. return $iz.'分钟前';
  1037. }
  1038. }
  1039. //获取播流地址
  1040. function getPull($stream){
  1041. $pull='';
  1042. $live_info=DI()->notorm->live->where("stream=?",$stream)->fetchOne();
  1043. if($live_info['isvideo']==1){ //视频
  1044. $pull=$live_info['pull'];
  1045. }else{
  1046. $configpri=getConfigPri();
  1047. if($configpri['cdn_switch']==5){
  1048. $wyinfo=PrivateKeyA('rtmp',$stream,1);
  1049. $pull=$wyinfo['ret']["rtmpPullUrl"];
  1050. }else{
  1051. $pull=PrivateKeyA('rtmp',$stream,0);
  1052. }
  1053. }
  1054. return $pull;
  1055. }
  1056. //检测姓名
  1057. function checkUsername($username){
  1058. $preg='/^(?=.*\d.*\b)/';
  1059. $isok = preg_match($preg,$username);
  1060. if($isok){
  1061. return 1;
  1062. }else{
  1063. return 0;
  1064. }
  1065. }
  1066. //验证数字是否整数/两位小数
  1067. function checkNumber($num){
  1068. if(floor($num) ==$num){
  1069. return 1;
  1070. }
  1071. if (preg_match('/^[0-9]+(.[0-9]{1,2})$/', $num)) {
  1072. return 1;
  1073. }
  1074. return 0;
  1075. }