common_helper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. function links($path){
  3. $url = site_url($path);
  4. $url = str_replace(SELF,'index.php',$url);
  5. if(CS_Web_Wjt == 1){
  6. $url = str_replace(Web_Path.'index.php/',Web_Path,$url);
  7. }
  8. return $url;
  9. }
  10. //m3u8地址
  11. function m3u8_link($path,$ac='m3u8',$xu=1){
  12. $Mu_Path = substr(Mu_Path,0,2) == './' ? substr(Mu_Path,2) : Mu_Path;
  13. $webpath = CS_Web_Wjt == 1 ? Web_Path : Web_Path.'index.php/';
  14. //多图模式
  15. if($ac != 'm3u8'){
  16. if($xu > 1 && strpos($path, '[xu]') === false){
  17. $name = end(explode('/', $path));
  18. $path = str_replace($name, $xu.'_'.$name, $path);
  19. }else{
  20. $path = str_replace('[xu]', $xu, $path);
  21. }
  22. if($ac == 'gif') $path = str_replace('.jpg', '.gif', $path);
  23. }else{
  24. if(Mu_Type == 1 && $xu != '1'){
  25. $ext = end(explode('/', $path));
  26. $path = str_replace($ext, $xu.'/'.$ext, $path);
  27. }
  28. }
  29. if(Web_Play == ''){
  30. if(Web_M3u8On == 1){
  31. if($ac == 'm3u8'){
  32. $link = 'http://'.Web_Url.$webpath.'m3u8/'.sys_auth($path).'.'.$ac;
  33. }else{
  34. $link = 'http://'.Web_Url.$webpath.'m3u8/pic/'.sys_auth($path).'.'.$ac;
  35. }
  36. }else{
  37. $link = 'http://'.Web_Url.Web_Path.$Mu_Path.$path;
  38. }
  39. }else{
  40. if(Web_M3u8On == 1){
  41. if($ac == 'm3u8'){
  42. $link = 'http://'.Web_Play.$webpath.'m3u8/'.sys_auth($path).'.'.$ac;
  43. }else{
  44. $link = 'http://'.Web_Play.$webpath.'m3u8/pic/'.sys_auth($path).'.'.$ac;
  45. }
  46. }else{
  47. $link = 'http://'.Web_Play.'/'.$path;
  48. }
  49. }
  50. return $link;
  51. }
  52. function m3u8_dir($id,$time,$ac='m3u8',$old=0){
  53. if($old == 0){
  54. $Mu_Pic_Name = defined('Mu_Pic_Name') ? Mu_Pic_Name : '[年]/[月]/[日]/[md5]/vod.jpg';
  55. $Mu_M3u8_Name = defined('Mu_M3u8_Name') ? Mu_M3u8_Name : '[年]/[月]/[日]/[md5]/m3u8.m3u8';
  56. }else{
  57. $Mu_Pic_Name = '[年]/[月]/[日]/[md5]/vod.jpg';
  58. $Mu_M3u8_Name = '[年]/[月]/[日]/[md5]/m3u8.m3u8';
  59. }
  60. $md5 = substr(md5(Mu_Key.$id.'ctcms'),0,8);
  61. $yarr = array('[年]','[月]','[日]','[id]','[md5]');
  62. $xarr = array(date('Y',$time),date('m',$time),date('d',$time),$id,$md5);
  63. if($ac == 'gif'){
  64. $dir_file = str_replace(array('.jpg','.png','jpeg'),'.gif',str_replace($yarr, $xarr, $Mu_Pic_Name));
  65. }elseif ($ac == 'jpg') {
  66. $dir_file = str_replace($yarr, $xarr, $Mu_Pic_Name);
  67. }else{
  68. $dir_file = str_replace($yarr, $xarr, $Mu_M3u8_Name);
  69. }
  70. return $dir_file;
  71. }
  72. //写文件
  73. function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE){
  74. $dir = dirname($path);
  75. if(!is_dir($dir)){
  76. mkdirss($dir);
  77. }
  78. if ( ! $fp = @fopen($path, $mode)){
  79. return FALSE;
  80. }
  81. flock($fp, LOCK_EX);
  82. fwrite($fp, $data);
  83. flock($fp, LOCK_UN);
  84. fclose($fp);
  85. return TRUE;
  86. }
  87. //递归创建文件夹
  88. function mkdirss($dir) {
  89. if (!$dir) {
  90. return FALSE;
  91. }
  92. if (!is_dir($dir)) {
  93. mkdirss(dirname($dir));
  94. if (!file_exists($dir)) {
  95. mkdir($dir, 0777);
  96. }
  97. }
  98. return true;
  99. }
  100. //JSON输出
  101. function getjson($info,$error=1,$sign=0,$callback=''){
  102. $msg = $info;
  103. $data['error'] = $error;
  104. $data['info'] = $info;
  105. //兼容前台
  106. $data['msg'] = $msg;
  107. if($sign==1 && is_array($msg)){
  108. $data = array_merge($msg);
  109. }
  110. $json = json_encode($data);
  111. if(!empty($callback)){
  112. echo $callback."(".$json.")";
  113. }else{
  114. echo $json;
  115. }
  116. exit;
  117. }
  118. //视频分页
  119. function admin_page($url,$page,$pages){
  120. $phtml = '<div class="layui-box layui-laypage layui-laypage-default" id="layui-laypage-0">';
  121. if($page > 1){
  122. $phtml .= '<a href="'.$url.($page-1).'" class="layui-laypage-prev" data-page="'.($page-1).'">上一页</a>';
  123. }
  124. if($pages<6 || $page<4){
  125. if($pages < 2){
  126. return '';
  127. }
  128. if($pages<6){
  129. $len = $pages;
  130. }else{
  131. $len = 5;
  132. }
  133. for($i=1;$i<$len+1;$i++){
  134. $phtml .= page_curr($url,$page,$i);
  135. }
  136. if($pages>5){
  137. $phtml .= '<span>…</span><a href="'.$url.$pages.'" class="layui-laypage-last" title="尾页" data-page="'.$pages.'">末页</a>';
  138. }
  139. }else{//pages>$nums
  140. if($pages<$page+2){
  141. $phtml .= '<a href="'.$url.'1" class="laypage_first" data-page="1" title="首页">首页</a><span>…</span>';
  142. for($i=$pages-4;$i<$pages+1;$i++){
  143. $phtml .= page_curr($url,$page,$i);
  144. }
  145. }else{
  146. $phtml .='<a href="'.$url.'1" class="laypage_first" data-page="1" title="首页">首页</a><span>…</span>';
  147. for($i=$page-2;$i<$page+3;$i++){
  148. $phtml .= page_curr($url,$page,$i);
  149. }
  150. $phtml .= '<span>…</span><a href="'.$url.$pages.'" class="layui-laypage-last" title="尾页" data-page="'.$pages.'">末页</a>';
  151. }
  152. }
  153. if($page < $pages){
  154. $phtml .= '<a href="'.$url.($page+1).'" class="layui-laypage-next" data-page="'.($page+1).'">下一页</a>';
  155. }
  156. $phtml .= '<span class="layui-laypage-total phide">到第 <input id="goto_page" type="number" min="1" onkeyup="this.value=this.value.replace(/\D/, \'\')" value="'.$page.'" class="layui-laypage-skip"> 页 <button type="button" onclick="goto_page(\''.$url.'\')" class="layui-laypage-btn">确定</button></span></div>';
  157. return $phtml;
  158. }
  159. function page_curr($url,$page,$i){
  160. $phtml = '';
  161. if($page==$i){
  162. $phtml .= '<span class="layui-laypage-curr"><em class="layui-laypage-em"></em><em>'.$page.'</em></span>';
  163. }else{
  164. $phtml .= '<a href="'.$url.$i.'" data-page="'.$i.'">'.$i.'</a>';
  165. }
  166. return $phtml;
  167. }
  168. function page_data($nums,$page,$pages){
  169. if($pages<2){
  170. return '';
  171. }else{
  172. return '共'.$nums.'条记录'.$pages.'页,当前显示第'.$page.'页';
  173. }
  174. }
  175. function formatsize($size, $dec=2){
  176. $a = array("B", "KB", "MB", "GB", "TB", "PB");
  177. $pos = 0;
  178. while ($size >= 1024) {
  179. $size /= 1024;
  180. $pos++;
  181. }
  182. return round($size,$dec)." ".$a[$pos];
  183. }
  184. function formattime($time,$sign=0){
  185. $h = $m = $s = 0;
  186. $str = '';
  187. $s = floor($time%60);
  188. $m = floor($time/60)%60;
  189. $h = floor($time/60/60);
  190. if($sign==0){
  191. if($h>0) return $str = $h."时".$m."分".$s.'秒';
  192. if($m>0) return $str = $m."分".$s.'秒';
  193. return $str = $s.'秒';
  194. }
  195. if($sign==1){
  196. if($m<10) $m = '0'.$m;
  197. if($s<10) $s = '0'.$s;
  198. if($h>0) return $str = $h.":".$m.":".$s;
  199. if($m>0) return $str = $m.":".$s;
  200. return $str = '00:'.$s;
  201. }
  202. }
  203. function is_referer($m3u8=0){
  204. if(Web_CrossDomain == '' ||
  205. Web_CrossOn == 0 ||
  206. preg_match("/(PlaySDK|ExoPlayerLib)/i", strtoupper($_SERVER['HTTP_USER_AGENT']))
  207. ){
  208. return true;
  209. }
  210. if(empty($_SERVER['HTTP_REFERER'])){
  211. if(Web_Cross == 0 || (Web_M3u8On == 1 && $m3u8 == 1)){
  212. return false;
  213. }else{
  214. return true;
  215. }
  216. }
  217. $uriarr = parse_url($_SERVER['HTTP_REFERER']);
  218. $host = $uriarr['host'];
  219. $ymarr = explode("|",Web_CrossDomain);
  220. if($host == Web_Url || $host == Web_Play || in_array($host,$ymarr)){
  221. return true;
  222. }
  223. return false;
  224. }
  225. //SQL过滤
  226. function safe_replace($string){
  227. if(is_array($string)) {
  228. foreach($string as $k => $v) {
  229. $string[$k] = safe_replace($v);
  230. }
  231. }else{
  232. if(!is_numeric($string)){
  233. $string = str_replace('&','&amp;',$string);
  234. $string = str_replace('%20','',$string);
  235. $string = str_replace('%27','',$string);
  236. $string = str_replace('%2527','',$string);
  237. $string = str_replace("'",'&#039;',$string);
  238. $string = str_replace('"','&quot;',$string);
  239. $string = str_replace(';','',$string);
  240. $string = str_replace('*','',$string);
  241. $string = str_replace('<','&lt;',$string);
  242. $string = str_replace('>','&gt;',$string);
  243. $string = str_replace("\\",'/',$string);
  244. $string = str_replace('%','\%',$string);
  245. $string = str_replace('{','%7b',$string);
  246. $string = str_replace('}','%7d',$string);
  247. }
  248. }
  249. return $string;
  250. }
  251. //删除目录和文件
  252. function deldir($dir,$sid=1) {
  253. if(!is_dir($dir)){
  254. return true;
  255. }
  256. //先删除目录下的文件
  257. $dh=opendir($dir);
  258. while ($file=readdir($dh)) {
  259. if($file!="." && $file!="..") {
  260. $fullpath=$dir."/".$file;
  261. if(!is_dir($fullpath)) {
  262. @unlink($fullpath);
  263. } else {
  264. deldir($fullpath,$sid);
  265. }
  266. }
  267. }
  268. closedir($dh);
  269. //删除当前文件夹:
  270. if($sid==1){
  271. if(@rmdir($dir)) {
  272. return true;
  273. } else {
  274. return false;
  275. }
  276. }else{
  277. return true;
  278. }
  279. }
  280. //创建目录
  281. function creatdir($path){
  282. if(!is_dir($path)){
  283. if(creatdir(dirname($path))){
  284. mkdir($path,0777);
  285. return true;
  286. }
  287. }else{
  288. return true;
  289. }
  290. }
  291. //CURL
  292. function get_curl($url,$post=''){
  293. $ch = curl_init($url);
  294. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  295. curl_setopt($ch, CURLOPT_HEADER, false);
  296. if(!empty($post)){
  297. curl_setopt($ch, CURLOPT_USERAGENT, 'ctcms_yzm_api');
  298. curl_setopt($ch, CURLOPT_POST, 1);
  299. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  300. }else{
  301. curl_setopt($ch, CURLOPT_USERAGENT, "ctcms_yunzhuanma");
  302. }
  303. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
  304. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  305. $content = curl_exec($ch);
  306. curl_close($ch);
  307. return $content;
  308. }
  309. function get_api($ac,$data=array()){
  310. if(Api_On==0 || Api_Url=='' || Api_Key==''){
  311. return 'Api Unopened';
  312. }
  313. $data['ac'] = $ac;
  314. $data['key'] = Api_Key;
  315. $res = get_curl(Api_Url,$data);
  316. $logfile = FCPATH.'logs/';
  317. mkdirss($logfile);
  318. $txt = date('Y-m-d H:i:s')."-----".json_encode($data)."-----".$ac."-----".$res."\r\n";
  319. file_put_contents($logfile.date('Y-m-d').'.txt',$txt,FILE_APPEND);
  320. return $res;
  321. }
  322. function sys_auth($string, $type = 0, $expiry = 0, $key = 'yunzhuanma') {
  323. if($type == 1) $string = str_replace(array('-','_'),array('+','/'),$string);
  324. $ckey_length = 4;
  325. $key = md5($key);
  326. $keya = md5(substr($key, 0, 16));
  327. $keyb = md5(substr($key, 16, 16));
  328. $keyc = $ckey_length ? ($type == 1 ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  329. $cryptkey = $keya.md5($keya.$keyc);
  330. $key_length = strlen($cryptkey);
  331. $string = $type == 1 ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  332. $string_length = strlen($string);
  333. $result = '';
  334. $box = range(0, 255);
  335. $rndkey = array();
  336. for($i = 0; $i <= 255; $i++) {
  337. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  338. }
  339. for($j = $i = 0; $i < 256; $i++) {
  340. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  341. $tmp = $box[$i];
  342. $box[$i] = $box[$j];
  343. $box[$j] = $tmp;
  344. }
  345. for($a = $j = $i = 0; $i < $string_length; $i++) {
  346. $a = ($a + 1) % 256;
  347. $j = ($j + $box[$a]) % 256;
  348. $tmp = $box[$a];
  349. $box[$a] = $box[$j];
  350. $box[$j] = $tmp;
  351. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  352. }
  353. if($type == 1) {
  354. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  355. return substr($result, 26);
  356. } else {
  357. return '';
  358. }
  359. } else {
  360. return str_replace(array('+','/'), array('-','_'), $keyc.str_replace('=', '', base64_encode($result)));
  361. }
  362. }
  363. //获取待转目录第一个文件
  364. function dir_file($dir){
  365. if(!is_dir($dir)) return '';
  366. $darr = array();
  367. $dh = opendir($dir);
  368. while ($file=readdir($dh)) {
  369. if(empty($darr) && $file!="." && $file!="..") {
  370. if(!is_dir($dir.'/'.$file)){
  371. $darr['dir'] = $dir;
  372. $darr['file'] = $file;
  373. break;
  374. } else {
  375. $darr = dir_file($dir.'/'.$file);
  376. if(!empty($darr)) break;
  377. }
  378. }
  379. }
  380. closedir($dh);
  381. if(!empty($darr)){
  382. return $darr;
  383. }
  384. }