497 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			497 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
| <?php
 | ||
| // +----------------------------------------------------------------------
 | ||
| // | ThinkPHP [ WE CAN DO IT JUST THINK ]
 | ||
| // +----------------------------------------------------------------------
 | ||
| // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
 | ||
| // +----------------------------------------------------------------------
 | ||
| // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 | ||
| // +----------------------------------------------------------------------
 | ||
| // | Author: 流年 <liu21st@gmail.com>
 | ||
| // +----------------------------------------------------------------------
 | ||
| 
 | ||
| use think\exception\ClassNotFoundException;
 | ||
| use app\model\Model as ModelModel ;
 | ||
| // 应用公共文件
 | ||
| 
 | ||
| if (!function_exists('widget')) {
 | ||
|     /**
 | ||
|      * 渲染输出Widget
 | ||
|      * @param string    $name Widget名称
 | ||
|      * @param array     $data 传入的参数
 | ||
|      * @return mixed
 | ||
|      * milo 2019-05-08 从TP5.1代码中拿来修改的
 | ||
|      */
 | ||
|     function widget($name, $data = [])
 | ||
|     {
 | ||
|         return action($name, $data, 'widget');
 | ||
|     }
 | ||
| }
 | ||
| if (!function_exists('action')) {
 | ||
|     /**
 | ||
|      * 调用模块的操作方法 参数格式 [模块/控制器/]操作
 | ||
|      * @param string        $url 调用地址
 | ||
|      * @param string|array  $vars 调用参数 支持字符串和数组
 | ||
|      * @param string        $layer 要调用的控制层名称
 | ||
|      * @param bool          $appendSuffix 是否添加类名后缀
 | ||
|      * @return mixed
 | ||
|      * milo 2019-05-08 从TP5.1代码中拿来修改的
 | ||
|      */
 | ||
|     function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
 | ||
|     {
 | ||
|         $info   = pathinfo($url);
 | ||
|         $action = $info['basename'];
 | ||
|         $module = '.' != $info['dirname'] ? $info['dirname'] : request()->controller();
 | ||
|         $class  = controller($module, $layer);
 | ||
|         if (is_scalar($vars)) {
 | ||
|             if (strpos($vars, '=')) {
 | ||
|                 parse_str($vars, $vars);
 | ||
|             } else {
 | ||
|                 $vars = [$vars];
 | ||
|             }
 | ||
|         }
 | ||
|         return app()->invokeMethod([$class, $action . config('route.action_suffix')], $vars);
 | ||
|     }
 | ||
| }
 | ||
| if (!function_exists('controller')) {
 | ||
|     /**
 | ||
|      * 实例化(分层)控制器 格式:[模块名/]控制器名
 | ||
|      * @access public
 | ||
|      * @param  string $name              资源地址
 | ||
|      * @param  string $layer             控制层名称
 | ||
|      * @param  bool   $appendSuffix      是否添加类名后缀
 | ||
|      * @param  string $empty             空控制器名称
 | ||
|      * @return object
 | ||
|      * @throws ClassNotFoundException
 | ||
|      * 
 | ||
|      * milo 2019-05-08 从TP5.1代码中拿来修改的
 | ||
|      */
 | ||
|     function controller($name, $layer = 'controller', $empty = '')
 | ||
|     {
 | ||
|         $class = parseClass($name, $layer);
 | ||
|         if (class_exists($class)) {
 | ||
|             return app()->make($class);
 | ||
|         } elseif ($empty && class_exists($emptyClass = app()->parseClass($layer, $empty))) {
 | ||
|             return app()->make($emptyClass);
 | ||
|         }
 | ||
| 
 | ||
|         throw new ClassNotFoundException('class not exists:' . $class, $class);
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| if (!function_exists('parseClass')) {
 | ||
|     /**
 | ||
|      * 解析模块和类名
 | ||
|      * @access protected
 | ||
|      * @param  string $name         资源地址
 | ||
|      * @param  string $layer        验证层名称
 | ||
|      * @param  bool   $appendSuffix 是否添加类名后缀
 | ||
|      * @return array
 | ||
|      * 
 | ||
|      * milo 2019-05-08 从TP5.1代码中拿来修改的
 | ||
|      */
 | ||
|     function parseClass($name, $layer)
 | ||
|     {
 | ||
|         if (false !== strpos($name, '\\')) {
 | ||
|             $class  = $name;
 | ||
|         } else {
 | ||
|             if (strpos($name, '/')) {
 | ||
|                 $names = explode('/', $name, 2);
 | ||
|                 $name = $names[1];
 | ||
|             }
 | ||
|             $class = app()->parseClass($layer,$name);
 | ||
|         }
 | ||
|         return $class;
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| 
 | ||
| if (!function_exists('randomStr')) {
 | ||
|     /**
 | ||
|      * 获取随机字符串
 | ||
|      * @param int $type 0:数字(默认);1:全部;2:小写字母;3:大写字母;4:字母;
 | ||
|      * @param int $len 字符串长度
 | ||
|      * @return string
 | ||
|      */
 | ||
|     function randomStr($type = 0,$len = 5)
 | ||
|     {
 | ||
|         $strPol = "0123456789";
 | ||
|         if($type == 1) {
 | ||
|             $strPol = "ABCDEFGHIJKLMOPQRSTUVWYZ0123456789abcdefghijklmopqrstuvwyz";
 | ||
|         } elseif ($type == 2) {
 | ||
|             $strPol = "abcdefghijklmopqrstuvwyz";
 | ||
|         } elseif ($type == 3) {
 | ||
|             $strPol = "ABCDEFGHIJKLMOPQRSTUVWYZ";
 | ||
|         } elseif ($type == 4) {
 | ||
|             $strPol = "ABCDEFGHIJKLMOPQRSTUVWYZabcdefghijklmopqrstuvwyz";
 | ||
|         }
 | ||
|         $max = strlen($strPol) - 1;
 | ||
|         $str = '';
 | ||
|         for ($i=0;$i<$len;$i++) {
 | ||
|             $str .= $strPol[rand(0,$max)];
 | ||
|         }
 | ||
|         return $str;
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| if(!function_exists('isMobile')){
 | ||
|     //判断访问终端是否为移动端
 | ||
|     function isMobile()
 | ||
|     {  
 | ||
|         // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
 | ||
|         if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
 | ||
|             return true;
 | ||
|         }
 | ||
|         // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
 | ||
|         if (isset($_SERVER['HTTP_VIA'])) { 
 | ||
|             // 找不到为flase,否则为true
 | ||
|             return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
 | ||
|         } 
 | ||
|         // 脑残法,判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信
 | ||
|         if (isset($_SERVER['HTTP_USER_AGENT'])) {
 | ||
|             $clientkeywords = ['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','MicroMessenger']; 
 | ||
|             // 从HTTP_USER_AGENT中查找手机浏览器的关键字
 | ||
|             if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
 | ||
|                 return true;
 | ||
|             }
 | ||
|         } 
 | ||
|         // 协议法,因为有可能不准确,放到最后判断
 | ||
|         if (isset ($_SERVER['HTTP_ACCEPT'])) { 
 | ||
|             // 如果只支持wml并且不支持html那一定是移动设备
 | ||
|             // 如果支持wml和html但是wml在html之前则是移动设备
 | ||
|             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;
 | ||
|     }
 | ||
| }
 | ||
| //根据栏目获取路径
 | ||
| if(!function_exists('getUri')){
 | ||
|     function getUri($cate)
 | ||
|     {
 | ||
|         $url = '';
 | ||
|         if(!empty($cate)){
 | ||
|             if($cate['is_index']){
 | ||
|                 return '/';
 | ||
|             }
 | ||
|             if(!empty($cate['url'])){
 | ||
|                 return $cate['url'];
 | ||
|             }
 | ||
|             if(!empty($cate['route'])){
 | ||
|                 return $cate['route'].".html";
 | ||
|             }
 | ||
|             switch ($cate["model_id"]){
 | ||
|                 case ModelModel::MODEL_ARTICLE:
 | ||
|                     return url('/articles/'. $cate['id']);
 | ||
|                     break;
 | ||
|                 case ModelModel::MODEL_PAGE:
 | ||
|                     return url('/page/index', ['categoryId' => $cate['id']]);
 | ||
|                     break;
 | ||
|                 case ModelModel::MODEL_PRODUCT:
 | ||
|                     return url('/product/index', ['categoryId' => $cate['id']]);
 | ||
|                     break;
 | ||
|                 default:
 | ||
|                     return url('/article/index', ['categoryId' => $cate['id']]);
 | ||
|                     break;
 | ||
|             }
 | ||
|         }
 | ||
|         return $url;
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| //根据栏目获取文章路径
 | ||
| if (!function_exists('archiveGetUri')) {
 | ||
|     function archiveGetUri($archive)
 | ||
|     {
 | ||
|         if (!empty($archive) &&isset($archive["id"])&& isset($archive->archivesCategory)&&!empty($archive->archivesCategory)) {
 | ||
| 
 | ||
|             if (empty($archive->archivesCategory->route)) {
 | ||
|                 switch ($archive->archivesCategory["model_id"]){
 | ||
|                     case ModelModel::MODEL_PRODUCT:
 | ||
|                         return '/product/detail/'.$archive['id'].".html";
 | ||
|                         break;
 | ||
|                     default:
 | ||
|                         return '/article/detail/'.$archive['id'].".html";
 | ||
|                         break;
 | ||
|                 }
 | ||
|             } else {
 | ||
|                 return $archive->archivesCategory->route."/".$archive['id'] .".html";
 | ||
|             }
 | ||
|         }
 | ||
|         return '';
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| 
 | ||
| //根据文件大小转换为文字
 | ||
| if(!function_exists('sizeToStr')){
 | ||
|     function sizeToStr($size)
 | ||
|     {
 | ||
|         if(!is_numeric($size) || $size <= 0){
 | ||
|             return '';
 | ||
|         }
 | ||
|         $size = $size / 1024;
 | ||
|         if($size < 1024){
 | ||
|             return sprintf("%.2fK", $size);
 | ||
|         }
 | ||
|         $size = $size / 1024;
 | ||
|         if($size < 1024){
 | ||
|             return sprintf("%.2fM", $size);
 | ||
|         }
 | ||
|         $size = $size / 1024;
 | ||
|         return sprintf("%.2fG", $size);
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| //根据路径获取文件名
 | ||
| if(!function_exists('getKeyByPath')){
 | ||
|     function getKeyByPath($path)
 | ||
|     {
 | ||
|         return substr($path, strrpos($path, '/')+1);
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| //富文本中提取图片路径
 | ||
| if(!function_exists('getImageUrlFromText')){
 | ||
|     function getImageUrlFromText($content)
 | ||
|     {
 | ||
|         preg_match_all('/<img.*?src="(.*?)".*?>/is', $content, $imgs);
 | ||
|         $data = [];
 | ||
|         if(!empty($imgs) && !empty($imgs[1])){
 | ||
|             foreach($imgs[1] as $img){
 | ||
|                 if(substr($img, 0, 4) != 'http'){
 | ||
|                     $key = getKeyByPath($img);
 | ||
|                     $data[$key] = $img;
 | ||
|                 }
 | ||
|             }
 | ||
|         }
 | ||
|         return $data;
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| //富文本中提取视频路径
 | ||
| if(!function_exists('getVideoUrlFromText')){
 | ||
|     function getVideoUrlFromText($content)
 | ||
|     {
 | ||
|         preg_match_all('/<video.*?src="(.*?)".*?>/is', $content, $videos);
 | ||
|         $data = [];
 | ||
|         if(!empty($videos) && !empty($videos[1])){
 | ||
|             foreach($videos[1] as $video){
 | ||
|                 if(substr($video, 0, 4) != 'http'){
 | ||
|                     $key = getKeyByPath($video);
 | ||
|                     $data[$key] = $video;
 | ||
|                 }
 | ||
|             }
 | ||
|         }
 | ||
|         return $data;
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| //获取目录下的所有文件
 | ||
| if(!function_exists('getAllFilesByPath')){
 | ||
|     function getAllFilesByPath($path, $rootPath)
 | ||
|     {
 | ||
|         if(is_dir($path) && file_exists($path)){
 | ||
|             $items = scandir($path);
 | ||
|             $files = [];
 | ||
|             foreach($items as $item){
 | ||
|                 if(substr($item, 0, 1) != '.' && strpos($item, '_') == false){
 | ||
|                     $itemPath = $path . '/' . $item;
 | ||
|                     if(is_file($itemPath)){
 | ||
|                         $size = filesize($itemPath);
 | ||
|                         $files[$item] = [
 | ||
|                             'path' => str_replace($rootPath, '/', $itemPath),
 | ||
|                             'realPath' => $itemPath,
 | ||
|                             'size' => $size,
 | ||
|                             'sizeStr' => sizeToStr($size),
 | ||
|                             'suffix' => strtolower(substr($item, strrpos($item, '.')+1))    //后缀
 | ||
|                         ];
 | ||
|                     }elseif(is_dir($itemPath)){
 | ||
|                         $childFiles = getAllFilesByPath($itemPath, $rootPath);
 | ||
|                         if(!empty($childFiles)){
 | ||
|                             $files = array_merge($files, $childFiles);
 | ||
|                         }else{
 | ||
|                             rmdir($itemPath);
 | ||
|                         }
 | ||
|                     }
 | ||
|                 }
 | ||
|             }
 | ||
|             return $files;
 | ||
|         }
 | ||
|         return [];
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| //过滤get输入
 | ||
| if(!function_exists('getFilter')){
 | ||
|     function getFilter($value){
 | ||
|         $getFilter = "'|(and|or)\b.+?(>|<|=|in|like)|\/\*.+?\*\/|<\s*script\b|\bEXEC\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\s+(TABLE|DATABASE)";
 | ||
|         $forArray = false;
 | ||
|         if(is_array($value)){
 | ||
|             $forFilter = implode($value);
 | ||
|             $forArray = true;
 | ||
|         }else{
 | ||
|             $forFilter = $value;
 | ||
|         }
 | ||
|         if (preg_match("/".$getFilter."/is", $forFilter) == 1){
 | ||
|             $value = $forArray ? [] : '';
 | ||
|         }
 | ||
|         return filterExp($value);
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| //过滤post录入
 | ||
| if(!function_exists('postFilter')){
 | ||
|     function postFilter($value){
 | ||
|         $postFilter = "\b(and|or)\b.{1,6}?(=|>|<|\bin\b|\blike\b)|\/\*.+?\*\/|<\s*script\b|\bEXEC\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\s+(TABLE|DATABASE)";
 | ||
|         $forArray = false;
 | ||
|         if(is_array($value)){
 | ||
|             $forFilter = implode($value);
 | ||
|             $forArray = true;
 | ||
|         }else{
 | ||
|             $forFilter = $value;
 | ||
|         }
 | ||
|         if (preg_match("/".$postFilter."/is", $forFilter) == 1){
 | ||
|             $value = $forArray ? [] : '';
 | ||
|         }
 | ||
|         return filterExp($value);
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| //过滤cookie数据
 | ||
| if(!function_exists('cookieFilter')){
 | ||
|     function cookieFilter($value){
 | ||
|         $cookieFilter = "\b(and|or)\b.{1,6}?(=|>|<|\bin\b|\blike\b)|\/\*.+?\*\/|<\s*script\b|\bEXEC\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\s+(TABLE|DATABASE)";
 | ||
|         $forArray = false;
 | ||
|         if(is_array($value)){
 | ||
|             $forFilter = implode($value);
 | ||
|             $forArray = true;
 | ||
|         }else{
 | ||
|             $forFilter = $value;
 | ||
|         }
 | ||
|         if (preg_match("/".$cookieFilter."/is", $forFilter) == 1){
 | ||
|             $value = $forArray ? [] : '';
 | ||
|         }
 | ||
|         return filterExp($value);
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| if(!function_exists('filterExp')){
 | ||
|     function filterExp($value){
 | ||
|         $filter = '/^(\s)+(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT LIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN)(\s)+$/i';
 | ||
|         $forArray = false;
 | ||
|         if(is_array($value)){
 | ||
|             $forFilter = implode($value);
 | ||
|             $forArray = true;
 | ||
|         }else{
 | ||
|             $forFilter = $value;
 | ||
|         }
 | ||
|         if (preg_match($filter, $forFilter) == 1){
 | ||
|             $value = $forArray ? [] : '';
 | ||
|         }
 | ||
|         return $value;
 | ||
|     }
 | ||
| }
 | ||
| if(!function_exists('arrayHtmlFilter')){
 | ||
|     function arrayHtmlFilter($arr) {
 | ||
|         foreach ($arr as $k => $one) {
 | ||
|             if(is_array($one)) {
 | ||
|                 $arr[$k] = arrayHtmlFilter($one);
 | ||
|             } else {
 | ||
|                 $one = trim($one);
 | ||
|                 $arr[$k] = strip_tags($one);
 | ||
|             }
 | ||
|         }
 | ||
|         return $arr;
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| if(!function_exists('getTextareaText')){
 | ||
|     function getTextareaText($content) {
 | ||
|         $content = trim($content);
 | ||
|         if(!empty($content)) {
 | ||
|             $content   = str_replace("\r\n", "\n", $content);
 | ||
|             $content   = str_replace("\r", "\n", $content);
 | ||
|         }
 | ||
|         return $content;
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| if(!function_exists('getTextNlToList')){
 | ||
|     function getTextNlToList($content) {
 | ||
|         $list = [];
 | ||
|         $content = getTextareaText($content);
 | ||
|         if(!empty($content)) {
 | ||
|             $list = explode("\n", $content);
 | ||
|         }
 | ||
|         return $list;
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| if (!function_exists('checkPathExistWithMake')) {
 | ||
|     /**
 | ||
|      * 检测文件夹是否存在,不存在时自动创建(需要有写入权限)
 | ||
|      * 支持递归创建
 | ||
|      *
 | ||
|      * @param string $absolutePath
 | ||
|      * @return bool
 | ||
|      */
 | ||
|     function checkPathExistWithMake(string $absolutePath): bool
 | ||
|     {
 | ||
|         try {
 | ||
|             $absolutePath   = rtrim($absolutePath, '/');
 | ||
|             if (empty($absolutePath)) {
 | ||
|                 return false;
 | ||
|             }
 | ||
| 
 | ||
|             if (!is_dir($absolutePath)) {
 | ||
|                 return mkdir($absolutePath, 0777, true);
 | ||
|             }
 | ||
| 
 | ||
|             return true;
 | ||
|         } catch (\Exception $e) {
 | ||
|             return false;
 | ||
|         }
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| if (!function_exists('dateToFormat')) {
 | ||
|     /**
 | ||
|      * 时间日期格式化
 | ||
|      *
 | ||
|      * @param null|string $dateStr
 | ||
|      * @param string $format
 | ||
|      * @return string
 | ||
|      */
 | ||
|     function dateToFormat(?string $dateStr, string $format='Y-m-d'): string
 | ||
|     {
 | ||
|         if (empty($dateStr)) {
 | ||
|             return '';
 | ||
|         }
 | ||
| 
 | ||
|         $time = strtotime($dateStr);
 | ||
|         if (!$time) {
 | ||
|             return '';
 | ||
|         }
 | ||
| 
 | ||
|         return date($format, $time);
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| if (!function_exists('timeToFormat')) {
 | ||
|     /**
 | ||
|      * 时间日期格式化
 | ||
|      *
 | ||
|      * @param int $time
 | ||
|      * @param string $format
 | ||
|      * @return string
 | ||
|      */
 | ||
|     function timeToFormat(int $time, string $format='Y-m-d'): string
 | ||
|     {
 | ||
|         if ($time <= 0) {
 | ||
|             return '';
 | ||
|         }
 | ||
| 
 | ||
|         return date($format, $time);
 | ||
|     }
 | ||
| } |