No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.php 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 全局函数文件
  13. // +----------------------------------------------------------------------
  14. use think\Db;
  15. use think\facade\Cache;
  16. use think\facade\Config;
  17. use think\facade\Request;
  18. !defined('DS') && define('DS', DIRECTORY_SEPARATOR);
  19. // 运行目录
  20. define('ROOT_URL', Request::rootUrl() . '/');
  21. //模板目录
  22. define('TEMPLATE_PATH', ROOT_PATH . 'templates' . DS);
  23. //强制隐藏index.php
  24. Url::root('/');
  25. // Form别名
  26. if (!class_exists('Form')) {
  27. class_alias('form\\Form', 'Form');
  28. }
  29. // 加载用户函数文件
  30. include_once APP_PATH . 'function.php';
  31. /**
  32. * 系统缓存缓存管理
  33. * cache('model') 获取model缓存
  34. * cache('model',null) 删除model缓存
  35. * @param mixed $name 缓存名称
  36. * @param mixed $value 缓存值
  37. * @param mixed $options 缓存参数
  38. * @return mixed
  39. */
  40. function cache($name, $value = '', $options = null)
  41. {
  42. static $cache = '';
  43. if (empty($cache)) {
  44. $cache = \libs\Cache_factory::instance();
  45. }
  46. // 获取缓存
  47. if ('' === $value) {
  48. if (false !== strpos($name, '.')) {
  49. $vars = explode('.', $name);
  50. $data = $cache->get($vars[0]);
  51. return is_array($data) ? $data[$vars[1]] : $data;
  52. } else {
  53. return $cache->get($name);
  54. }
  55. } elseif (is_null($value)) {
  56. //删除缓存
  57. return $cache->remove($name);
  58. } else {
  59. //缓存数据
  60. if (is_array($options)) {
  61. $expire = isset($options['expire']) ? $options['expire'] : null;
  62. } else {
  63. $expire = is_numeric($options) ? $options : null;
  64. }
  65. return $cache->set($name, $value, $expire);
  66. }
  67. }
  68. /**
  69. * 获取上传资源的CDN的地址
  70. * @param string $url 资源相对地址
  71. * @param boolean $domain 是否显示域名 或者直接传入域名
  72. * @return string
  73. */
  74. function cdnurl($url, $domain = false)
  75. {
  76. $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
  77. $cdnurl = Config::get('cdnurl');
  78. if (is_bool($domain) || stripos($cdnurl, '/') === 0) {
  79. $url = preg_match($regex, $url) || ($cdnurl && stripos($url, $cdnurl) === 0) ? $url : $cdnurl . $url;
  80. }
  81. if ($domain && !preg_match($regex, $url)) {
  82. $domain = is_bool($domain) ? Request::domain() : $domain;
  83. $url = $domain . $url;
  84. }
  85. return $url;
  86. }
  87. /**
  88. * select返回的数组进行整数映射转换
  89. *
  90. * @param array $map 映射关系二维数组 array(
  91. * '字段名1'=>array(映射关系数组),
  92. * '字段名2'=>array(映射关系数组),
  93. * ......
  94. * )
  95. * @author 朱亚杰 <zhuyajie@topthink.net>
  96. * @return array
  97. *
  98. * array(
  99. * array('id'=>1,'title'=>'标题','status'=>'1','status_text'=>'正常')
  100. * ....
  101. * )
  102. *
  103. */
  104. function int_to_string(&$data, $map = ['status' => [1 => '正常', -1 => '删除', 0 => '禁用', 2 => '未审核', 3 => '草稿']])
  105. {
  106. if ($data === false || $data === null) {
  107. return $data;
  108. }
  109. $data = (array) $data;
  110. foreach ($data as $key => $row) {
  111. foreach ($map as $col => $pair) {
  112. if (isset($row[$col]) && isset($pair[$row[$col]])) {
  113. $data[$key][$col . '_text'] = $pair[$row[$col]];
  114. }
  115. }
  116. }
  117. return $data;
  118. }
  119. /**
  120. * 字符串转换为数组,主要用于把分隔符调整到第二个参数
  121. * @param string $str 要分割的字符串
  122. * @param string $glue 分割符
  123. * @return array
  124. */
  125. function str2arr($str, $glue = ',')
  126. {
  127. return explode($glue, $str);
  128. }
  129. /**
  130. * 数组转换为字符串,主要用于把分隔符调整到第二个参数
  131. * @param array $arr 要连接的数组
  132. * @param string $glue 分割符
  133. * @return string
  134. */
  135. function arr2str($arr, $glue = ',')
  136. {
  137. if (is_string($arr)) {
  138. return $arr;
  139. }
  140. return implode($glue, $arr);
  141. }
  142. /**
  143. * 字符截取
  144. * @param $string 需要截取的字符串
  145. * @param $length 长度
  146. * @param $dot
  147. */
  148. function str_cut($sourcestr, $length, $dot = '...')
  149. {
  150. $returnstr = '';
  151. $i = 0;
  152. $n = 0;
  153. $str_length = strlen($sourcestr); //字符串的字节数
  154. while (($n < $length) && ($i <= $str_length)) {
  155. $temp_str = substr($sourcestr, $i, 1);
  156. $ascnum = Ord($temp_str); //得到字符串中第$i位字符的ascii码
  157. if ($ascnum >= 224) { //如果ASCII位高与224,
  158. $returnstr = $returnstr . substr($sourcestr, $i, 3); //根据UTF-8编码规范,将3个连续的字符计为单个字符
  159. $i = $i + 3; //实际Byte计为3
  160. $n++; //字串长度计1
  161. } elseif ($ascnum >= 192) { //如果ASCII位高与192,
  162. $returnstr = $returnstr . substr($sourcestr, $i, 2); //根据UTF-8编码规范,将2个连续的字符计为单个字符
  163. $i = $i + 2; //实际Byte计为2
  164. $n++; //字串长度计1
  165. } elseif ($ascnum >= 65 && $ascnum <= 90) {
  166. //如果是大写字母,
  167. $returnstr = $returnstr . substr($sourcestr, $i, 1);
  168. $i = $i + 1; //实际的Byte数仍计1个
  169. $n++; //但考虑整体美观,大写字母计成一个高位字符
  170. } else {
  171. //其他情况下,包括小写字母和半角标点符号,
  172. $returnstr = $returnstr . substr($sourcestr, $i, 1);
  173. $i = $i + 1; //实际的Byte数计1个
  174. $n = $n + 0.5; //小写字母和半角标点等与半个高位字符宽...
  175. }
  176. }
  177. if ($str_length > strlen($returnstr)) {
  178. $returnstr = $returnstr . $dot; //超过长度时在尾处加上省略号
  179. }
  180. return $returnstr;
  181. }
  182. /**
  183. * 对查询结果集进行排序
  184. * @access public
  185. * @param array $list 查询结果
  186. * @param string $field 排序的字段名
  187. * @param array $sortby 排序类型
  188. * asc正向排序 desc逆向排序 nat自然排序
  189. * @return array
  190. */
  191. function list_sort_by($list, $field, $sortby = 'asc')
  192. {
  193. if (is_array($list)) {
  194. $refer = $resultSet = [];
  195. foreach ($list as $i => $data) {
  196. $refer[$i] = &$data[$field];
  197. }
  198. switch ($sortby) {
  199. case 'asc': // 正向排序
  200. asort($refer);
  201. break;
  202. case 'desc': // 逆向排序
  203. arsort($refer);
  204. break;
  205. case 'nat': // 自然排序
  206. natcasesort($refer);
  207. break;
  208. }
  209. foreach ($refer as $key => $val) {
  210. $resultSet[] = &$list[$key];
  211. }
  212. return $resultSet;
  213. }
  214. return false;
  215. }
  216. /**
  217. * 把返回的数据集转换成Tree
  218. * @param array $list 要转换的数据集
  219. * @param string $pid parent标记字段
  220. * @param string $level level标记字段
  221. * @return array
  222. * @author 麦当苗儿 <zuojiazi@vip.qq.com>
  223. */
  224. function list_to_tree($list, $pk = 'id', $pid = 'parentid', $child = '_child', $root = 0)
  225. {
  226. // 创建Tree
  227. $tree = [];
  228. if (is_array($list)) {
  229. // 创建基于主键的数组引用
  230. $refer = [];
  231. foreach ($list as $key => $data) {
  232. $refer[$data[$pk]] = &$list[$key];
  233. }
  234. foreach ($list as $key => $data) {
  235. // 判断是否存在parent
  236. $parentId = $data[$pid];
  237. if ($root == $parentId) {
  238. $tree[] = &$list[$key];
  239. } else {
  240. if (isset($refer[$parentId])) {
  241. $parent = &$refer[$parentId];
  242. $parent[$child][] = &$list[$key];
  243. }
  244. }
  245. }
  246. }
  247. return $tree;
  248. }
  249. /**
  250. * 解析配置
  251. * @param string $value 配置值
  252. * @return array|string
  253. */
  254. function parse_attr($value = '')
  255. {
  256. $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
  257. if (strpos($value, ':')) {
  258. $value = [];
  259. foreach ($array as $val) {
  260. list($k, $v) = explode(':', $val);
  261. $value[$k] = $v;
  262. }
  263. } else {
  264. $value = $array;
  265. }
  266. return $value;
  267. }
  268. /**
  269. * 时间戳格式化
  270. * @param int $timestamp
  271. * @return string 完整的时间显示
  272. */
  273. function time_format($timestamp = null, $type = 0)
  274. {
  275. if ($timestamp == 0) {
  276. return '';
  277. }
  278. $types = ['Y-m-d H:i:s', 'Y-m-d H:i', 'Y-m-d'];
  279. $timestamp = $timestamp === null ? $_SERVER['REQUEST_TIME'] : intval($timestamp);
  280. return date($types[$type], $timestamp);
  281. }
  282. /**
  283. * 获取语义化时间
  284. * @param int $time 时间
  285. * @param int $local 本地时间
  286. * @return string
  287. */
  288. function human_date($time, $local = null)
  289. {
  290. return \util\Date::human($time, $local);
  291. }
  292. /**
  293. * 格式化字节大小
  294. * @param number $size 字节数
  295. * @param string $delimiter 数字和单位分隔符
  296. * @return string 格式化后的带单位的大小
  297. */
  298. function format_bytes($size, $delimiter = '')
  299. {
  300. $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  301. for ($i = 0; $size >= 1024 && $i < 5; $i++) {
  302. $size /= 1024;
  303. }
  304. return round($size, 2) . $delimiter . $units[$i];
  305. }
  306. /**
  307. * 根据PHP各种类型变量生成唯一标识号
  308. * @param mixed $mix 变量
  309. * @return string
  310. */
  311. function to_guid_string($mix)
  312. {
  313. if (is_object($mix)) {
  314. return spl_object_hash($mix);
  315. } elseif (is_resource($mix)) {
  316. $mix = get_resource_type($mix) . strval($mix);
  317. } else {
  318. $mix = serialize($mix);
  319. }
  320. return md5($mix);
  321. }
  322. /**
  323. * 对用户的密码进行加密
  324. * @param $password
  325. * @param $encrypt //传入加密串,在修改密码时做认证
  326. * @return array/password
  327. */
  328. function encrypt_password($password, $encrypt = '')
  329. {
  330. $pwd = [];
  331. $pwd['encrypt'] = $encrypt ? $encrypt : genRandomString();
  332. $pwd['password'] = md5(trim($password) . $pwd['encrypt']);
  333. return $encrypt ? $pwd['password'] : $pwd;
  334. }
  335. /**
  336. * 产生一个指定长度的随机字符串,并返回给用户
  337. * @param type $len 产生字符串的长度
  338. * @return string 随机字符串
  339. */
  340. function genRandomString($len = 6)
  341. {
  342. $chars = [
  343. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  344. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  345. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  346. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  347. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
  348. "3", "4", "5", "6", "7", "8", "9",
  349. ];
  350. $charsLen = count($chars) - 1;
  351. // 将数组打乱
  352. shuffle($chars);
  353. $output = "";
  354. for ($i = 0; $i < $len; $i++) {
  355. $output .= $chars[mt_rand(0, $charsLen)];
  356. }
  357. return $output;
  358. }
  359. /**
  360. * 获取模型数据
  361. * @param type $modelid 模型ID
  362. * @param type $name 返回的字段,默认返回全部,数组
  363. * @return boolean
  364. */
  365. function getModel($modelid, $name = '')
  366. {
  367. if (empty($modelid) || !is_numeric($modelid)) {
  368. return false;
  369. }
  370. $key = 'getModel_' . $modelid;
  371. /* 读取缓存数据 */
  372. $cache = Cache::get($key);
  373. if ($cache === 'false') {
  374. return false;
  375. }
  376. if (empty($cache)) {
  377. //读取数据
  378. $cache = Db::name('Model')->where(['id' => $modelid])->find();
  379. if (empty($cache)) {
  380. Cache::set($key, 'false', 60);
  381. return false;
  382. } else {
  383. Cache::set($key, $cache, 3600);
  384. }
  385. }
  386. return is_null($name) ? $cache : $cache[$name];
  387. }
  388. /**
  389. * 生成缩略图
  390. * @param type $imgurl 图片地址
  391. * @param type $width 缩略图宽度
  392. * @param type $height 缩略图高度
  393. * @param type $thumbType 缩略图生成方式
  394. * @param type $smallpic 图片不存在时显示默认图片
  395. * @return type
  396. */
  397. function thumb($imgurl, $width = 100, $height = 100, $thumbType = 1, $smallpic = 'noimage.jpg')
  398. {
  399. static $_thumb_cache = [];
  400. $smallpic = Config::get('public_url') . 'static/common/img/' . $smallpic;
  401. if (empty($imgurl)) {
  402. return $smallpic;
  403. }
  404. //区分
  405. $key = md5($imgurl . $width . $height . $thumbType . $smallpic);
  406. if (isset($_thumb_cache[$key])) {
  407. return $_thumb_cache[$key];
  408. }
  409. if (!$width) {
  410. return $smallpic;
  411. }
  412. $uploadUrl = cdnurl(Config::get('public_url')) . 'uploads/';
  413. $imgurl_replace = str_replace($uploadUrl, '', $imgurl);
  414. $newimgname = 'thumb_' . $width . '_' . $height . '_' . basename($imgurl_replace);
  415. $newimgurl = dirname($imgurl_replace) . '/' . $newimgname;
  416. //检查生成的缩略图是否已经生成过
  417. if (is_file(ROOT_PATH . 'public' . DS . 'uploads' . DS . $newimgurl)) {
  418. return $uploadUrl . $newimgurl;
  419. }
  420. //检查文件是否存在,如果是开启远程附件的,估计就通过不了,以后在考虑完善!
  421. if (!is_file(ROOT_PATH . 'public' . DS . 'uploads' . DS . $imgurl_replace)) {
  422. return $imgurl;
  423. }
  424. //取得图片相关信息
  425. list($width_t, $height_t, $type, $attr) = getimagesize(ROOT_PATH . 'public' . DS . 'uploads' . DS . $imgurl_replace);
  426. //如果高是0,自动计算高
  427. if ($height <= 0) {
  428. $height = round(($width / $width_t) * $height_t);
  429. }
  430. //判断生成的缩略图大小是否正常
  431. if ($width >= $width_t || $height >= $height_t) {
  432. return $imgurl;
  433. }
  434. model('Attachment')->create_thumb(ROOT_PATH . 'public' . DS . 'uploads' . DS . $imgurl_replace, ROOT_PATH . 'public' . DS . 'uploads' . DS . dirname($imgurl_replace) . '/', $newimgname, "{$width},{$height}", $thumbType);
  435. $_thumb_cache[$key] = $uploadUrl . $newimgurl;
  436. return $_thumb_cache[$key];
  437. }
  438. /**
  439. * 下载远程文件,默认保存在temp下
  440. * @param string $url 网址
  441. * @param string $filename 保存文件名
  442. * @param integer $timeout 过期时间
  443. * @param bool $repalce 是否覆盖已存在文件
  444. * @return string 本地文件名
  445. */
  446. function http_down($url, $filename = "", $timeout = 60)
  447. {
  448. if (empty($filename)) {
  449. $filename = ROOT_PATH . 'public' . DS . 'temp' . DS . pathinfo($url, PATHINFO_BASENAME);
  450. }
  451. $path = dirname($filename);
  452. if (!is_dir($path) && !mkdir($path, 0755, true)) {
  453. return false;
  454. }
  455. $url = str_replace(" ", "%20", $url);
  456. if (function_exists('curl_init')) {
  457. $ch = curl_init();
  458. curl_setopt($ch, CURLOPT_URL, $url);
  459. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  460. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  461. // curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
  462. // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  463. if ('https' == substr($url, 0, 5)) {
  464. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  465. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  466. }
  467. $temp = curl_exec($ch);
  468. if (file_put_contents($filename, $temp) && !curl_error($ch)) {
  469. return $filename;
  470. } else {
  471. return false;
  472. }
  473. } else {
  474. $opts = [
  475. "http" => [
  476. "method" => "GET",
  477. "header" => "",
  478. "timeout" => $timeout,
  479. ],
  480. ];
  481. $context = stream_context_create($opts);
  482. if (@copy($url, $filename, $context)) {
  483. //$http_response_header
  484. return $filename;
  485. } else {
  486. return false;
  487. }
  488. }
  489. }
  490. /**
  491. * 安全过滤函数
  492. * @param $string
  493. * @return string
  494. */
  495. function safe_replace($string)
  496. {
  497. $string = str_replace('%20', '', $string);
  498. $string = str_replace('%27', '', $string);
  499. $string = str_replace('%2527', '', $string);
  500. $string = str_replace('*', '', $string);
  501. $string = str_replace('"', '&quot;', $string);
  502. $string = str_replace("'", '', $string);
  503. $string = str_replace('"', '', $string);
  504. $string = str_replace(';', '', $string);
  505. $string = str_replace('<', '&lt;', $string);
  506. $string = str_replace('>', '&gt;', $string);
  507. $string = str_replace("{", '', $string);
  508. $string = str_replace('}', '', $string);
  509. $string = str_replace('\\', '', $string);
  510. return $string;
  511. }
  512. /**
  513. * 字符串加密、解密函数
  514. * @param string $txt 字符串
  515. * @param string $operation ENCODE为加密,DECODE为解密,可选参数,默认为ENCODE,
  516. * @param string $key 密钥:数字、字母、下划线
  517. * @param string $expiry 过期时间
  518. * @return string
  519. */
  520. function sys_auth($string, $operation = 'ENCODE', $key = '', $expiry = 0)
  521. {
  522. $ckey_length = 4;
  523. $key = md5($key != '' ? $key : Config::get('data_auth_key'));
  524. $keya = md5(substr($key, 0, 16));
  525. $keyb = md5(substr($key, 16, 16));
  526. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
  527. $cryptkey = $keya . md5($keya . $keyc);
  528. $key_length = strlen($cryptkey);
  529. $string = $operation == 'DECODE' ? base64_decode(strtr(substr($string, $ckey_length), '-_', '+/')) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
  530. $string_length = strlen($string);
  531. $result = '';
  532. $box = range(0, 255);
  533. $rndkey = [];
  534. for ($i = 0; $i <= 255; $i++) {
  535. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  536. }
  537. for ($j = $i = 0; $i < 256; $i++) {
  538. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  539. $tmp = $box[$i];
  540. $box[$i] = $box[$j];
  541. $box[$j] = $tmp;
  542. }
  543. for ($a = $j = $i = 0; $i < $string_length; $i++) {
  544. $a = ($a + 1) % 256;
  545. $j = ($j + $box[$a]) % 256;
  546. $tmp = $box[$a];
  547. $box[$a] = $box[$j];
  548. $box[$j] = $tmp;
  549. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  550. }
  551. if ($operation == 'DECODE') {
  552. if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
  553. return substr($result, 26);
  554. } else {
  555. return '';
  556. }
  557. } else {
  558. return $keyc . rtrim(strtr(base64_encode($result), '+/', '-_'), '=');
  559. }
  560. }
  561. function hsv2rgb($h, $s, $v)
  562. {
  563. $r = $g = $b = 0;
  564. $i = floor($h * 6);
  565. $f = $h * 6 - $i;
  566. $p = $v * (1 - $s);
  567. $q = $v * (1 - $f * $s);
  568. $t = $v * (1 - (1 - $f) * $s);
  569. switch ($i % 6) {
  570. case 0:
  571. $r = $v;
  572. $g = $t;
  573. $b = $p;
  574. break;
  575. case 1:
  576. $r = $q;
  577. $g = $v;
  578. $b = $p;
  579. break;
  580. case 2:
  581. $r = $p;
  582. $g = $v;
  583. $b = $t;
  584. break;
  585. case 3:
  586. $r = $p;
  587. $g = $q;
  588. $b = $v;
  589. break;
  590. case 4:
  591. $r = $t;
  592. $g = $p;
  593. $b = $v;
  594. break;
  595. case 5:
  596. $r = $v;
  597. $g = $p;
  598. $b = $q;
  599. break;
  600. }
  601. return [
  602. floor($r * 255),
  603. floor($g * 255),
  604. floor($b * 255),
  605. ];
  606. }
  607. /**
  608. * 首字母头像
  609. * @param $text
  610. * @return string
  611. */
  612. function letter_avatar($text)
  613. {
  614. $total = unpack('L', hash('adler32', $text, true))[1];
  615. $hue = $total % 360;
  616. list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);
  617. $bg = "rgb({$r},{$g},{$b})";
  618. $color = "#ffffff";
  619. $first = mb_strtoupper(mb_substr($text, 0, 1));
  620. $src = base64_encode('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="100" width="100"><rect fill="' . $bg . '" x="0" y="0" width="100" height="100"></rect><text x="50" y="50" font-size="50" text-copy="fast" fill="' . $color . '" text-anchor="middle" text-rights="admin" alignment-baseline="central">' . $first . '</text></svg>');
  621. $value = 'data:image/svg+xml;base64,' . $src;
  622. return $value;
  623. }
  624. /**
  625. * 生成文件后缀图片
  626. * @param string $suffix 后缀
  627. * @param null $background
  628. * @return string
  629. */
  630. function build_suffix_image($suffix, $background = null)
  631. {
  632. $suffix = mb_substr(strtoupper($suffix), 0, 4);
  633. $total = unpack('L', hash('adler32', $suffix, true))[1];
  634. $hue = $total % 360;
  635. list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);
  636. $background = $background ? $background : "rgb({$r},{$g},{$b})";
  637. $icon = <<<EOT
  638. <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
  639. <path style="fill:#E2E5E7;" d="M128,0c-17.6,0-32,14.4-32,32v448c0,17.6,14.4,32,32,32h320c17.6,0,32-14.4,32-32V128L352,0H128z"/>
  640. <path style="fill:#B0B7BD;" d="M384,128h96L352,0v96C352,113.6,366.4,128,384,128z"/>
  641. <polygon style="fill:#CAD1D8;" points="480,224 384,128 480,128 "/>
  642. <path style="fill:{$background};" d="M416,416c0,8.8-7.2,16-16,16H48c-8.8,0-16-7.2-16-16V256c0-8.8,7.2-16,16-16h352c8.8,0,16,7.2,16,16 V416z"/>
  643. <path style="fill:#CAD1D8;" d="M400,432H96v16h304c8.8,0,16-7.2,16-16v-16C416,424.8,408.8,432,400,432z"/>
  644. <g><text><tspan x="220" y="380" font-size="124" font-family="Verdana, Helvetica, Arial, sans-serif" fill="white" text-anchor="middle">{$suffix}</tspan></text></g>
  645. </svg>
  646. EOT;
  647. return $icon;
  648. }
  649. /**
  650. * 跨域检测
  651. */
  652. function check_cors_request()
  653. {
  654. //跨域访问的时候才会存在此字段
  655. if (isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN']) {
  656. $info = parse_url($_SERVER['HTTP_ORIGIN']);
  657. $domainArr = explode(',', Config::get('cors_request_domain'));
  658. $domainArr[] = Request::host(true);
  659. if (in_array("*", $domainArr) || in_array($_SERVER['HTTP_ORIGIN'], $domainArr) || (isset($info['host']) && in_array($info['host'], $domainArr))) {
  660. header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']);
  661. } else {
  662. header('HTTP/1.1 403 Forbidden');
  663. exit;
  664. }
  665. header('Access-Control-Allow-Credentials: true');
  666. header('Access-Control-Max-Age: 86400');
  667. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  668. if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
  669. header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
  670. }
  671. if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
  672. header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
  673. }
  674. exit;
  675. }
  676. }
  677. }
  678. /**
  679. * 清理XSS
  680. */
  681. function xss_clean($content, $is_image = false)
  682. {
  683. return \app\common\library\Security::instance()->xss_clean($content, $is_image);
  684. }
  685. /**
  686. * 清理URL
  687. */
  688. function url_clean($url)
  689. {
  690. if (!check_url_allowed($url)) {
  691. return '';
  692. }
  693. return xss_clean($url);
  694. }
  695. /**
  696. * 检测URL是否允许范围内
  697. * @param string $url URL
  698. * @return bool
  699. */
  700. function check_url_allowed($url = '')
  701. {
  702. //允许的主机列表
  703. $allowedHostArr = [
  704. strtolower(request()->host())
  705. ];
  706. if (empty($url)) {
  707. return true;
  708. }
  709. //如果是站内相对链接则允许
  710. if (preg_match("/^[\/a-z][a-z0-9][a-z0-9\.\/]+((\?|#).*)?\$/i", $url) && substr($url, 0, 2) !== '//') {
  711. return true;
  712. }
  713. //如果是站外链接则需要判断HOST是否允许
  714. if (preg_match("/((http[s]?:\/\/)+(?>[a-z\-0-9]{2,}\.){1,}[a-z]{2,8})(?:\s|\/)/i", $url)) {
  715. $chkHost = parse_url(strtolower($url), PHP_URL_HOST);
  716. if ($chkHost && in_array($chkHost, $allowedHostArr)) {
  717. return true;
  718. }
  719. }
  720. return false;
  721. }
  722. /**
  723. * 使用短标签打印或返回数组结构
  724. * @param mixed $data
  725. * @param boolean $return 是否返回数据
  726. * @return string
  727. */
  728. function var_export_short($data, $return = true)
  729. {
  730. return var_export($data, $return);
  731. $replaced = [];
  732. $count = 0;
  733. //判断是否是对象
  734. if (is_resource($data) || is_object($data)) {
  735. return var_export($data, $return);
  736. }
  737. //判断是否有特殊的键名
  738. $specialKey = false;
  739. array_walk_recursive($data, function (&$value, &$key) use (&$specialKey) {
  740. if (is_string($key) && (stripos($key, "\n") !== false || stripos($key, "array (") !== false)) {
  741. $specialKey = true;
  742. }
  743. });
  744. if ($specialKey) {
  745. return var_export($data, $return);
  746. }
  747. array_walk_recursive($data, function (&$value, &$key) use (&$replaced, &$count, &$stringcheck) {
  748. if (is_object($value) || is_resource($value)) {
  749. $replaced[$count] = var_export($value, true);
  750. $value = "##<{$count}>##";
  751. } else {
  752. if (is_string($value) && (stripos($value, "\n") !== false || stripos($value, "array (") !== false)) {
  753. $index = array_search($value, $replaced);
  754. if ($index === false) {
  755. $replaced[$count] = var_export($value, true);
  756. $value = "##<{$count}>##";
  757. } else {
  758. $value = "##<{$index}>##";
  759. }
  760. }
  761. }
  762. $count++;
  763. });
  764. $dump = var_export($data, true);
  765. $dump = preg_replace('#(?:\A|\n)([ ]*)array \(#i', '[', $dump); // Starts
  766. $dump = preg_replace('#\n([ ]*)\),#', "\n$1],", $dump); // Ends
  767. $dump = preg_replace('#=> \[\n\s+\],\n#', "=> [],\n", $dump); // Empties
  768. $dump = preg_replace('#\)$#', "]", $dump); //End
  769. if ($replaced) {
  770. $dump = preg_replace_callback("/'##<(\d+)>##'/", function ($matches) use ($replaced) {
  771. return isset($replaced[$matches[1]]) ? $replaced[$matches[1]] : "''";
  772. }, $dump);
  773. }
  774. if ($return === true) {
  775. return $dump;
  776. } else {
  777. echo $dump;
  778. }
  779. }