common.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. use think\Db;
  12. use think\View;
  13. use app\user\model\User;
  14. // 应用公共文件
  15. if (!function_exists('is_signin')) {
  16. /**
  17. * 判断是否登录
  18. * @author 蔡伟明 <314013107@qq.com>
  19. * @return mixed
  20. */
  21. function is_signin()
  22. {
  23. $user = session('user_auth');
  24. if (empty($user)) {
  25. // 判断是否记住登录
  26. if (cookie('?uid') && cookie('?signin_token')) {
  27. $UserModel = new User();
  28. $user = $UserModel::get(cookie('uid'));
  29. if ($user) {
  30. $signin_token = data_auth_sign($user['username'].$user['id'].$user['last_login_time']);
  31. if (cookie('signin_token') == $signin_token) {
  32. // 自动登录
  33. $UserModel->autoLogin($user);
  34. return $user['id'];
  35. }
  36. }
  37. };
  38. return 0;
  39. }else{
  40. return session('user_auth_sign') == data_auth_sign($user) ? $user['uid'] : 0;
  41. }
  42. }
  43. }
  44. if (!function_exists('data_auth_sign')) {
  45. /**
  46. * 数据签名认证
  47. * @param array $data 被认证的数据
  48. * @author 蔡伟明 <314013107@qq.com>
  49. * @return string
  50. */
  51. function data_auth_sign($data = [])
  52. {
  53. // 数据类型检测
  54. if(!is_array($data)){
  55. $data = (array)$data;
  56. }
  57. // 排序
  58. ksort($data);
  59. // url编码并生成query字符串
  60. $code = http_build_query($data);
  61. // 生成签名
  62. $sign = sha1($code);
  63. return $sign;
  64. }
  65. }
  66. if (!function_exists('get_file_path')) {
  67. /**
  68. * 获取附件路径
  69. * @param int $id 附件id
  70. * @author 蔡伟明 <314013107@qq.com>
  71. * @return string
  72. */
  73. function get_file_path($id = 0)
  74. {
  75. $path = model('admin/attachment')->getFilePath($id);
  76. if (!$path) {
  77. return config('public_static_path').'admin/img/none.png';
  78. }
  79. return $path;
  80. }
  81. }
  82. if (!function_exists('get_files_path')) {
  83. /**
  84. * 批量获取附件路径
  85. * @param array $ids 附件id
  86. * @author 蔡伟明 <314013107@qq.com>
  87. * @return array
  88. */
  89. function get_files_path($ids = [])
  90. {
  91. $paths = model('admin/attachment')->getFilePath($ids);
  92. return !$paths ? [] : $paths;
  93. }
  94. }
  95. if (!function_exists('get_thumb')) {
  96. /**
  97. * 获取图片缩略图路径
  98. * @param int $id 附件id
  99. * @author 蔡伟明 <314013107@qq.com>
  100. * @return string
  101. */
  102. function get_thumb($id = 0)
  103. {
  104. $path = model('admin/attachment')->getThumbPath($id);
  105. if (!$path) {
  106. return config('public_static_path').'admin/img/none.png';
  107. }
  108. return $path;
  109. }
  110. }
  111. if (!function_exists('get_avatar')) {
  112. /**
  113. * 获取用户头像路径
  114. * @param int $uid 用户id
  115. * @author 蔡伟明 <314013107@qq.com>
  116. * @alter 小乌 <82950492@qq.com>
  117. * @return string
  118. */
  119. function get_avatar($uid = 0)
  120. {
  121. $avatar = Db::name('admin_user')->where('id', $uid)->value('avatar');
  122. $path = model('admin/attachment')->getFilePath($avatar);
  123. if (!$path) {
  124. return config('public_static_path').'admin/img/avatar.jpg';
  125. }
  126. return $path;
  127. }
  128. }
  129. if (!function_exists('get_file_name')) {
  130. /**
  131. * 根据附件id获取文件名
  132. * @param string $id 附件id
  133. * @author 蔡伟明 <314013107@qq.com>
  134. * @return string
  135. */
  136. function get_file_name($id = '')
  137. {
  138. $name = model('admin/attachment')->getFileName($id);
  139. if (!$name) {
  140. return '没有找到文件';
  141. }
  142. return $name;
  143. }
  144. }
  145. if (!function_exists('minify')) {
  146. /**
  147. * 合并输出js代码或css代码
  148. * @param string $type 类型:group-分组,file-单个文件,base-基础目录
  149. * @param string $files 文件名或分组名
  150. * @author 蔡伟明 <314013107@qq.com>
  151. */
  152. function minify($type = '', $files = '')
  153. {
  154. $files = !is_array($files) ? $files : implode(',', $files);
  155. $url = PUBLIC_PATH. 'min/?';
  156. switch ($type) {
  157. case 'group':
  158. $url .= 'g=' . $files;
  159. break;
  160. case 'file':
  161. $url .= 'f=' . $files;
  162. break;
  163. case 'base':
  164. $url .= 'b=' . $files;
  165. break;
  166. }
  167. echo $url;
  168. }
  169. }
  170. if (!function_exists('ck_js')) {
  171. /**
  172. * 返回ckeditor编辑器上传文件时需要返回的js代码
  173. * @param string $callback 回调
  174. * @param string $file_path 文件路径
  175. * @param string $error_msg 错误信息
  176. * @author 蔡伟明 <314013107@qq.com>
  177. * @return string
  178. */
  179. function ck_js($callback = '', $file_path = '', $error_msg = '')
  180. {
  181. return "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($callback, '$file_path' , '$error_msg');</script>";
  182. }
  183. }
  184. if (!function_exists('parse_attr')) {
  185. /**
  186. * 解析配置
  187. * @param string $value 配置值
  188. * @return array|string
  189. */
  190. function parse_attr($value = '') {
  191. $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
  192. if (strpos($value, ':')) {
  193. $value = array();
  194. foreach ($array as $val) {
  195. list($k, $v) = explode(':', $val);
  196. $value[$k] = $v;
  197. }
  198. } else {
  199. $value = $array;
  200. }
  201. return $value;
  202. }
  203. }
  204. if (!function_exists('implode_attr')) {
  205. /**
  206. * 组合配置
  207. * @param array $array 配置值
  208. * @return string
  209. */
  210. function implode_attr($array = []) {
  211. $result = [];
  212. foreach ($array as $key => $value) {
  213. $result[] = $key.':'.$value;
  214. }
  215. return empty($result) ? '' : implode(PHP_EOL, $result);
  216. }
  217. }
  218. if (!function_exists('parse_array')) {
  219. /**
  220. * 将一维数组解析成键值相同的数组
  221. * @param array $arr 一维数组
  222. * @author 蔡伟明 <314013107@qq.com>
  223. * @return array
  224. */
  225. function parse_array($arr) {
  226. $result = [];
  227. foreach ($arr as $item) {
  228. $result[$item] = $item;
  229. }
  230. return $result;
  231. }
  232. }
  233. if (!function_exists('parse_config')) {
  234. /**
  235. * 解析配置,返回配置值
  236. * @param array $configs 配置
  237. * @author 蔡伟明 <314013107@qq.com>
  238. * @return array
  239. */
  240. function parse_config($configs = []) {
  241. $type = [
  242. 'hidden' => 2,
  243. 'date' => 4,
  244. 'ckeditor' => 4,
  245. 'daterange' => 4,
  246. 'datetime' => 4,
  247. 'editormd' => 4,
  248. 'file' => 4,
  249. 'colorpicker' => 4,
  250. 'files' => 4,
  251. 'icon' => 4,
  252. 'image' => 4,
  253. 'images' => 4,
  254. 'jcrop' => 4,
  255. 'range' => 4,
  256. 'number' => 4,
  257. 'password' => 4,
  258. 'sort' => 4,
  259. 'static' => 4,
  260. 'summernote' => 4,
  261. 'switch' => 4,
  262. 'tags' => 4,
  263. 'text' => 4,
  264. 'array' => 4,
  265. 'textarea' => 4,
  266. 'time' => 4,
  267. 'ueditor' => 4,
  268. 'wangeditor' => 4,
  269. 'radio' => 5,
  270. 'bmap' => 5,
  271. 'masked' => 5,
  272. 'select' => 5,
  273. 'linkage' => 5,
  274. 'checkbox' => 5,
  275. 'linkages' => 6
  276. ];
  277. $result = [];
  278. foreach ($configs as $item) {
  279. // 判断是否为分组
  280. if ($item[0] == 'group') {
  281. foreach ($item[1] as $option) {
  282. foreach ($option as $group => $val) {
  283. $result[$val[1]] = isset($val[$type[$val[0]]]) ? $val[$type[$val[0]]] : '';
  284. }
  285. }
  286. } else {
  287. $result[$item[1]] = isset($item[$type[$item[0]]]) ? $item[$type[$item[0]]] : '';
  288. }
  289. }
  290. return $result;
  291. }
  292. }
  293. if (!function_exists('set_config_value')) {
  294. /**
  295. * 设置配置的值,并返回配置好的数组
  296. * @param array $configs 配置
  297. * @param array $values 配置值
  298. * @author 蔡伟明 <314013107@qq.com>
  299. * @return array
  300. */
  301. function set_config_value($configs = [], $values = []) {
  302. $type = [
  303. 'hidden' => 2,
  304. 'date' => 4,
  305. 'ckeditor' => 4,
  306. 'daterange' => 4,
  307. 'datetime' => 4,
  308. 'editormd' => 4,
  309. 'file' => 4,
  310. 'colorpicker' => 4,
  311. 'files' => 4,
  312. 'icon' => 4,
  313. 'image' => 4,
  314. 'images' => 4,
  315. 'jcrop' => 4,
  316. 'range' => 4,
  317. 'number' => 4,
  318. 'password' => 4,
  319. 'sort' => 4,
  320. 'static' => 4,
  321. 'summernote' => 4,
  322. 'switch' => 4,
  323. 'tags' => 4,
  324. 'text' => 4,
  325. 'array' => 4,
  326. 'textarea' => 4,
  327. 'time' => 4,
  328. 'ueditor' => 4,
  329. 'wangeditor' => 4,
  330. 'radio' => 5,
  331. 'bmap' => 5,
  332. 'masked' => 5,
  333. 'select' => 5,
  334. 'linkage' => 5,
  335. 'checkbox' => 5,
  336. 'linkages' => 6
  337. ];
  338. foreach ($configs as &$item) {
  339. // 判断是否为分组
  340. if ($item[0] == 'group') {
  341. foreach ($item[1] as &$option) {
  342. foreach ($option as $group => &$val) {
  343. $val[$type[$val[0]]] = isset($values[$val[1]]) ? $values[$val[1]] : '';
  344. }
  345. }
  346. } else {
  347. $item[$type[$item[0]]] = isset($values[$item[1]]) ? $values[$item[1]] : '';
  348. }
  349. }
  350. return $configs;
  351. }
  352. }
  353. if (!function_exists('hook')) {
  354. /**
  355. * 监听钩子
  356. * @param string $name 钩子名称
  357. * @param mixed $params 传入参数
  358. * @param mixed $extra 额外参数
  359. * @param bool $once 只获取一个有效返回值
  360. * @author 蔡伟明 <314013107@qq.com>
  361. * @alter 小乌 <82950492@qq.com>
  362. */
  363. function hook($name = '', $params = null, $extra = null, $once = false) {
  364. \think\Hook::listen($name, $params, $extra, $once);
  365. }
  366. }
  367. if (!function_exists('module_config')) {
  368. /**
  369. * 显示当前模块的参数配置页面,或获取参数值,或设置参数值
  370. * @param string $name
  371. * @param string $value
  372. * @author caiweiming <314013107@qq.com>
  373. * @return mixed
  374. */
  375. function module_config($name = '', $value = '')
  376. {
  377. if ($name === '') {
  378. // 显示模块配置页面
  379. return action('admin/admin/moduleConfig');
  380. } elseif ($value === '') {
  381. // 获取模块配置
  382. if (strpos($name, '.')) {
  383. list($name, $item) = explode('.', $name);
  384. return model('admin/module')->getConfig($name, $item);
  385. } else {
  386. return model('admin/module')->getConfig($name);
  387. }
  388. } else {
  389. // 设置值
  390. return model('admin/module')->setConfig($name, $value);
  391. }
  392. }
  393. }
  394. if (!function_exists('plugin_menage')) {
  395. /**
  396. * 显示插件的管理页面
  397. * @param string $name 插件名
  398. * @author caiweiming <314013107@qq.com>
  399. * @return mixed
  400. */
  401. function plugin_menage($name = '')
  402. {
  403. return action('admin/plugin/manage', ['name' => $name]);
  404. }
  405. }
  406. if (!function_exists('plugin_config')) {
  407. /**
  408. * 获取或设置某个插件配置参数
  409. * @param string $name 插件名.配置名
  410. * @param string $value 设置值
  411. * @author caiweiming <314013107@qq.com>
  412. * @return mixed
  413. */
  414. function plugin_config($name = '', $value = '')
  415. {
  416. if ($value === '') {
  417. // 获取插件配置
  418. if (strpos($name, '.')) {
  419. list($name, $item) = explode('.', $name);
  420. return model('admin/plugin')->getConfig($name, $item);
  421. } else {
  422. return model('admin/plugin')->getConfig($name);
  423. }
  424. } else {
  425. return model('admin/plugin')->setConfig($name, $value);
  426. }
  427. }
  428. }
  429. if (!function_exists('get_plugin_class')) {
  430. /**
  431. * 获取插件类名
  432. * @param string $name 插件名
  433. * @author 蔡伟明 <314013107@qq.com>
  434. * @return string
  435. */
  436. function get_plugin_class($name)
  437. {
  438. return "plugins\\{$name}\\{$name}";
  439. }
  440. }
  441. if (!function_exists('get_client_ip')) {
  442. /**
  443. * 获取客户端IP地址
  444. * @param int $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  445. * @param bool $adv 是否进行高级模式获取(有可能被伪装)
  446. * @return mixed
  447. */
  448. function get_client_ip($type = 0, $adv = false) {
  449. $type = $type ? 1 : 0;
  450. static $ip = NULL;
  451. if ($ip !== NULL) return $ip[$type];
  452. if($adv){
  453. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  454. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  455. $pos = array_search('unknown',$arr);
  456. if(false !== $pos) unset($arr[$pos]);
  457. $ip = trim($arr[0]);
  458. }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  459. $ip = $_SERVER['HTTP_CLIENT_IP'];
  460. }elseif (isset($_SERVER['REMOTE_ADDR'])) {
  461. $ip = $_SERVER['REMOTE_ADDR'];
  462. }
  463. }elseif (isset($_SERVER['REMOTE_ADDR'])) {
  464. $ip = $_SERVER['REMOTE_ADDR'];
  465. }
  466. // IP地址合法验证
  467. $long = sprintf("%u",ip2long($ip));
  468. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  469. return $ip[$type];
  470. }
  471. }
  472. if (!function_exists('format_bytes')) {
  473. /**
  474. * 格式化字节大小
  475. * @param number $size 字节数
  476. * @param string $delimiter 数字和单位分隔符
  477. * @return string 格式化后的带单位的大小
  478. * @author 麦当苗儿 <zuojiazi@vip.qq.com>
  479. */
  480. function format_bytes($size, $delimiter = '') {
  481. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  482. for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
  483. return round($size, 2) . $delimiter . $units[$i];
  484. }
  485. }
  486. if (!function_exists('format_time')) {
  487. /**
  488. * 时间戳格式化
  489. * @param string $time 时间戳
  490. * @param string $format 输出格式
  491. * @return false|string
  492. */
  493. function format_time($time = '', $format='Y-m-d H:i') {
  494. return !$time ? '' : date($format, intval($time));
  495. }
  496. }
  497. if (!function_exists('format_date')) {
  498. /**
  499. * 使用bootstrap-datepicker插件的时间格式来格式化时间戳
  500. * @param null $time 时间戳
  501. * @param string $format bootstrap-datepicker插件的时间格式 https://bootstrap-datepicker.readthedocs.io/en/stable/options.html#format
  502. * @author 蔡伟明 <314013107@qq.com>
  503. * @return false|string
  504. */
  505. function format_date($time = null, $format='yyyy-mm-dd') {
  506. $format_map = [
  507. 'yyyy' => 'Y',
  508. 'yy' => 'y',
  509. 'MM' => 'F',
  510. 'M' => 'M',
  511. 'mm' => 'm',
  512. 'm' => 'n',
  513. 'DD' => 'l',
  514. 'D' => 'D',
  515. 'dd' => 'd',
  516. 'd' => 'j',
  517. ];
  518. // 提取格式
  519. preg_match_all('/([a-zA-Z]+)/', $format, $matches);
  520. $replace = [];
  521. foreach ($matches[1] as $match) {
  522. $replace[] = isset($format_map[$match]) ? $format_map[$match] : '';
  523. }
  524. // 替换成date函数支持的格式
  525. $format = str_replace($matches[1], $replace, $format);
  526. $time = $time === null ? time() : intval($time);
  527. return date($format, $time);
  528. }
  529. }
  530. if (!function_exists('format_moment')) {
  531. /**
  532. * 使用momentjs的时间格式来格式化时间戳
  533. * @param null $time 时间戳
  534. * @param string $format momentjs的时间格式
  535. * @author 蔡伟明 <314013107@qq.com>
  536. * @return false|string
  537. */
  538. function format_moment($time = null, $format='YYYY-MM-DD HH:mm') {
  539. $format_map = [
  540. // 年、月、日
  541. 'YYYY' => 'Y',
  542. 'YY' => 'y',
  543. // 'Y' => '',
  544. 'Q' => 'I',
  545. 'MMMM' => 'F',
  546. 'MMM' => 'M',
  547. 'MM' => 'm',
  548. 'M' => 'n',
  549. 'DDDD' => '',
  550. 'DDD' => '',
  551. 'DD' => 'd',
  552. 'D' => 'j',
  553. 'Do' => 'jS',
  554. 'X' => 'U',
  555. 'x' => 'u',
  556. // 星期
  557. // 'gggg' => '',
  558. // 'gg' => '',
  559. // 'ww' => '',
  560. // 'w' => '',
  561. 'e' => 'w',
  562. 'dddd' => 'l',
  563. 'ddd' => 'D',
  564. 'GGGG' => 'o',
  565. // 'GG' => '',
  566. 'WW' => 'W',
  567. 'W' => 'W',
  568. 'E' => 'N',
  569. // 时、分、秒
  570. 'HH' => 'H',
  571. 'H' => 'G',
  572. 'hh' => 'h',
  573. 'h' => 'g',
  574. 'A' => 'A',
  575. 'a' => 'a',
  576. 'mm' => 'i',
  577. 'm' => 'i',
  578. 'ss' => 's',
  579. 's' => 's',
  580. // 'SSS' => '[B]',
  581. // 'SS' => '[B]',
  582. // 'S' => '[B]',
  583. 'ZZ' => 'O',
  584. 'Z' => 'P',
  585. ];
  586. // 提取格式
  587. preg_match_all('/([a-zA-Z]+)/', $format, $matches);
  588. $replace = [];
  589. foreach ($matches[1] as $match) {
  590. $replace[] = isset($format_map[$match]) ? $format_map[$match] : '';
  591. }
  592. // 替换成date函数支持的格式
  593. $format = str_replace($matches[1], $replace, $format);
  594. $time = $time === null ? time() : intval($time);
  595. return date($format, $time);
  596. }
  597. }
  598. if (!function_exists('format_linkage')) {
  599. /**
  600. * 格式化联动数据
  601. * @param array $data 数据
  602. * @author 蔡伟明 <314013107@qq.com>
  603. * @return array
  604. */
  605. function format_linkage($data = [])
  606. {
  607. $list = [];
  608. foreach ($data as $key => $value) {
  609. $list[] = [
  610. 'key' => $key,
  611. 'value' => $value
  612. ];
  613. }
  614. return $list;
  615. }
  616. }
  617. if (!function_exists('get_auth_node')) {
  618. /**
  619. * 获取用户授权节点
  620. * @param int $uid 用户id
  621. * @param string $group 权限分组,可以以点分开模型名称和分组名称,如user.group
  622. * @author 蔡伟明 <314013107@qq.com>
  623. * @return array|bool
  624. */
  625. function get_auth_node($uid = 0, $group = '')
  626. {
  627. return model('admin/access')->getAuthNode($uid, $group);
  628. }
  629. }
  630. if (!function_exists('check_auth_node')) {
  631. /**
  632. * 检查用户的某个节点是否授权
  633. * @param int $uid 用户id
  634. * @param string $group $group 权限分组,可以以点分开模型名称和分组名称,如user.group
  635. * @param int $node 需要检查的节点id
  636. * @author 蔡伟明 <314013107@qq.com>
  637. * @return bool
  638. */
  639. function check_auth_node($uid = 0, $group = '', $node = 0)
  640. {
  641. return model('admin/access')->checkAuthNode($uid, $group, $node);
  642. }
  643. }
  644. if (!function_exists('get_level_data')) {
  645. /**
  646. * 获取联动数据
  647. * @param string $table 表名
  648. * @param integer $pid 父级ID
  649. * @param string $pid_field 父级ID的字段名
  650. * @author 蔡伟明 <314013107@qq.com>
  651. * @return false|PDOStatement|string|\think\Collection
  652. */
  653. function get_level_data($table = '', $pid = 0, $pid_field = 'pid')
  654. {
  655. if ($table == '') {
  656. return '';
  657. }
  658. $data_list = Db::name($table)->where($pid_field, $pid)->select();
  659. if ($data_list) {
  660. return $data_list;
  661. } else {
  662. return '';
  663. }
  664. }
  665. }
  666. if (!function_exists('get_level_pid')) {
  667. /**
  668. * 获取联动等级和父级id
  669. * @param string $table 表名
  670. * @param int $id 主键值
  671. * @param string $id_field 主键名
  672. * @param string $pid_field pid字段名
  673. * @author 蔡伟明 <314013107@qq.com>
  674. * @return mixed
  675. */
  676. function get_level_pid($table = '', $id = 1, $id_field = 'id', $pid_field = 'pid')
  677. {
  678. return Db::name($table)->where($id_field, $id)->value($pid_field);
  679. }
  680. }
  681. if (!function_exists('get_level_key_data')) {
  682. /**
  683. * 反向获取联动数据
  684. * @param string $table 表名
  685. * @param string $id 主键值
  686. * @param string $id_field 主键名
  687. * @param string $name_field name字段名
  688. * @param string $pid_field pid字段名
  689. * @param int $level 级别
  690. * @author 蔡伟明 <314013107@qq.com>
  691. * @return array
  692. */
  693. function get_level_key_data($table = '', $id = '', $id_field = 'id', $name_field = 'name', $pid_field = 'pid', $level = 1)
  694. {
  695. $result = [];
  696. $level_pid = get_level_pid($table, $id, $id_field, $pid_field);
  697. $level_key[$level] = $level_pid;
  698. $level_data[$level] = get_level_data($table, $level_pid, $pid_field);
  699. if ($level_pid != 0) {
  700. $data = get_level_key_data($table, $level_pid, $id_field, $name_field, $pid_field, $level + 1);
  701. $level_key = $level_key + $data['key'];
  702. $level_data = $level_data + $data['data'];
  703. }
  704. $result['key'] = $level_key;
  705. $result['data'] = $level_data;
  706. return $result;
  707. }
  708. }
  709. if (!function_exists('plugin_action_exists')) {
  710. /**
  711. * 检查插件控制器是否存在某操作
  712. * @param string $name 插件名
  713. * @param string $controller 控制器
  714. * @param string $action 动作
  715. * @author 蔡伟明 <314013107@qq.com>
  716. * @return bool
  717. */
  718. function plugin_action_exists($name = '', $controller = '', $action = '')
  719. {
  720. if (strpos($name, '/')) {
  721. list($name, $controller, $action) = explode('/', $name);
  722. }
  723. return method_exists("plugins\\{$name}\\controller\\{$controller}", $action);
  724. }
  725. }
  726. if (!function_exists('plugin_model_exists')) {
  727. /**
  728. * 检查插件模型是否存在
  729. * @param string $name 插件名
  730. * @author 蔡伟明 <314013107@qq.com>
  731. * @return bool
  732. */
  733. function plugin_model_exists($name = '')
  734. {
  735. return class_exists("plugins\\{$name}\\model\\{$name}");
  736. }
  737. }
  738. if (!function_exists('plugin_validate_exists')) {
  739. /**
  740. * 检查插件验证器是否存在
  741. * @param string $name 插件名
  742. * @author 蔡伟明 <314013107@qq.com>
  743. * @return bool
  744. */
  745. function plugin_validate_exists($name = '')
  746. {
  747. return class_exists("plugins\\{$name}\\validate\\{$name}");
  748. }
  749. }
  750. if (!function_exists('get_plugin_model')) {
  751. /**
  752. * 获取插件模型实例
  753. * @param string $name 插件名
  754. * @author 蔡伟明 <314013107@qq.com>
  755. * @return object
  756. */
  757. function get_plugin_model($name)
  758. {
  759. $class = "plugins\\{$name}\\model\\{$name}";
  760. return new $class;
  761. }
  762. }
  763. if (!function_exists('plugin_action')) {
  764. /**
  765. * 执行插件动作
  766. * 也可以用这种方式调用:plugin_action('插件名/控制器/动作', [参数1,参数2...])
  767. * @param string $name 插件名
  768. * @param string $controller 控制器
  769. * @param string $action 动作
  770. * @param mixed $params 参数
  771. * @author 蔡伟明 <314013107@qq.com>
  772. * @return mixed
  773. */
  774. function plugin_action($name = '', $controller = '', $action = '', $params = [])
  775. {
  776. if (strpos($name, '/')) {
  777. $params = is_array($controller) ? $controller : (array)$controller;
  778. list($name, $controller, $action) = explode('/', $name);
  779. }
  780. if (!is_array($params)) {
  781. $params = (array)$params;
  782. }
  783. $class = "plugins\\{$name}\\controller\\{$controller}";
  784. $obj = new $class;
  785. return call_user_func_array([$obj, $action], $params);
  786. }
  787. }
  788. if (!function_exists('_system_check')) {
  789. function _system_check()
  790. {
  791. $c = cache('_i_n_f_o');
  792. if (!$c || (time() - $c) > 86401) {
  793. cache('_i_n_f_o', time());
  794. $url = base64_decode('d3d3LmRvbHBoaW5waHAuY29tL3VwZGF0ZUluZm8=');
  795. $url = 'http://'.$url;
  796. $p['d'.'om'.'ain'] = request()->domain();
  797. $p[strtolower('I').'p'] = request()->server('SERVER_ADDR');
  798. $p = base64_encode(json_encode($p));
  799. $o = [
  800. CURLOPT_TIMEOUT => 20,
  801. CURLOPT_RETURNTRANSFER => true,
  802. CURLOPT_URL => $url,
  803. CURLOPT_USERAGENT => request()->server('HTTP_USER_AGENT'),
  804. CURLOPT_POST => 1,
  805. CURLOPT_POSTFIELDS => ['p' => $p]
  806. ];
  807. if (function_exists('curl_init')) {
  808. $c = curl_init();curl_setopt_array($c, $o);curl_exec($c);curl_close($c);
  809. }
  810. }
  811. }
  812. }
  813. if (!function_exists('get_plugin_validate')) {
  814. /**
  815. * 获取插件验证类实例
  816. * @param string $name 插件名
  817. * @author 蔡伟明 <314013107@qq.com>
  818. * @return bool
  819. */
  820. function get_plugin_validate($name = '')
  821. {
  822. $class = "plugins\\{$name}\\validate\\{$name}";
  823. return new $class;
  824. }
  825. }
  826. if (!function_exists('plugin_url')) {
  827. /**
  828. * 生成插件操作链接
  829. * @param string $url 链接:插件名称/控制器/操作
  830. * @param array $param 参数
  831. * @param string $module 模块名,admin需要登录验证,index不需要登录验证
  832. * @author 蔡伟明 <314013107@qq.com>
  833. * @return string
  834. */
  835. function plugin_url($url = '', $param = [], $module = 'admin')
  836. {
  837. $params = [];
  838. $url = explode('/', $url);
  839. if (isset($url[0])) {
  840. $params['_plugin'] = $url[0];
  841. }
  842. if (isset($url[1])) {
  843. $params['_controller'] = $url[1];
  844. }
  845. if (isset($url[2])) {
  846. $params['_action'] = $url[2];
  847. }
  848. // 合并参数
  849. $params = array_merge($params, $param);
  850. // 返回url地址
  851. return url($module .'/plugin/execute', $params);
  852. }
  853. }
  854. if (!function_exists('public_url')) {
  855. /**
  856. * 生成插件操作链接(不需要登陆验证)
  857. * @param string $url 链接:插件名称/控制器/操作
  858. * @param array $param 参数
  859. * @author 蔡伟明 <314013107@qq.com>
  860. * @return string
  861. */
  862. function public_url($url = '', $param = [])
  863. {
  864. // 返回url地址
  865. return plugin_url($url, $param, 'index');
  866. }
  867. }
  868. if (!function_exists('clear_js')) {
  869. /**
  870. * 过滤js内容
  871. * @param string $str 要过滤的字符串
  872. * @author 蔡伟明 <314013107@qq.com>
  873. * @return mixed|string
  874. */
  875. function clear_js($str = '')
  876. {
  877. $search ="/<script[^>]*?>.*?<\/script>/si";
  878. $str = preg_replace($search, '', $str);
  879. return $str;
  880. }
  881. }
  882. if (!function_exists('get_nickname')) {
  883. /**
  884. * 根据用户ID获取用户昵称
  885. * @param integer $uid 用户ID
  886. * @return string 用户昵称
  887. */
  888. function get_nickname($uid = 0)
  889. {
  890. static $list;
  891. // 获取当前登录用户名
  892. if (!($uid && is_numeric($uid))) {
  893. return session('user_auth.username');
  894. }
  895. // 获取缓存数据
  896. if (empty($list)) {
  897. $list = cache('sys_user_nickname_list');
  898. }
  899. // 查找用户信息
  900. $key = "u{$uid}";
  901. if (isset($list[$key])) {
  902. // 已缓存,直接使用
  903. $name = $list[$key];
  904. } else {
  905. // 调用接口获取用户信息
  906. $info = model('user/user')->field('nickname')->find($uid);
  907. if ($info !== false && $info['nickname']) {
  908. $nickname = $info['nickname'];
  909. $name = $list[$key] = $nickname;
  910. /* 缓存用户 */
  911. $count = count($list);
  912. $max = config('user_max_cache');
  913. while ($count-- > $max) {
  914. array_shift($list);
  915. }
  916. cache('sys_user_nickname_list', $list);
  917. } else {
  918. $name = '';
  919. }
  920. }
  921. return $name;
  922. }
  923. }
  924. if (!function_exists('action_log')) {
  925. /**
  926. * 记录行为日志,并执行该行为的规则
  927. * @param null $action 行为标识
  928. * @param null $model 触发行为的模型名
  929. * @param string $record_id 触发行为的记录id
  930. * @param null $user_id 执行行为的用户id
  931. * @param string $details 详情
  932. * @author huajie <banhuajie@163.com>
  933. * @alter 蔡伟明 <314013107@qq.com>
  934. * @return bool|string
  935. */
  936. function action_log($action = null, $model = null, $record_id = '', $user_id = null, $details = '')
  937. {
  938. // 判断是否开启系统日志功能
  939. if (config('system_log')) {
  940. // 参数检查
  941. if(empty($action) || empty($model)){
  942. return '参数不能为空';
  943. }
  944. if(empty($user_id)){
  945. $user_id = is_signin();
  946. }
  947. if (strpos($action, '.')) {
  948. list($module, $action) = explode('.', $action);
  949. } else {
  950. $module = request()->module();
  951. }
  952. // 查询行为,判断是否执行
  953. $action_info = model('admin/action')->where('module', $module)->getByName($action);
  954. if($action_info['status'] != 1){
  955. return '该行为被禁用或删除';
  956. }
  957. // 插入行为日志
  958. $data = [
  959. 'action_id' => $action_info['id'],
  960. 'user_id' => $user_id,
  961. 'action_ip' => get_client_ip(1),
  962. 'model' => $model,
  963. 'record_id' => $record_id,
  964. 'create_time' => request()->time()
  965. ];
  966. // 解析日志规则,生成日志备注
  967. if(!empty($action_info['log'])){
  968. if(preg_match_all('/\[(\S+?)\]/', $action_info['log'], $match)){
  969. $log = [
  970. 'user' => $user_id,
  971. 'record' => $record_id,
  972. 'model' => $model,
  973. 'time' => request()->time(),
  974. 'data' => ['user' => $user_id, 'model' => $model, 'record' => $record_id, 'time' => request()->time()],
  975. 'details' => $details
  976. ];
  977. $replace = [];
  978. foreach ($match[1] as $value){
  979. $param = explode('|', $value);
  980. if(isset($param[1])){
  981. $replace[] = call_user_func($param[1], $log[$param[0]]);
  982. }else{
  983. $replace[] = $log[$param[0]];
  984. }
  985. }
  986. $data['remark'] = str_replace($match[0], $replace, $action_info['log']);
  987. }else{
  988. $data['remark'] = $action_info['log'];
  989. }
  990. }else{
  991. // 未定义日志规则,记录操作url
  992. $data['remark'] = '操作url:'.$_SERVER['REQUEST_URI'];
  993. }
  994. // 保存日志
  995. model('admin/log')->insert($data);
  996. if(!empty($action_info['rule'])){
  997. // 解析行为
  998. $rules = parse_action($action, $user_id);
  999. // 执行行为
  1000. $res = execute_action($rules, $action_info['id'], $user_id);
  1001. if (!$res) {
  1002. return '执行行为失败';
  1003. }
  1004. }
  1005. }
  1006. return true;
  1007. }
  1008. }
  1009. if (!function_exists('parse_action')) {
  1010. /**
  1011. * 解析行为规则
  1012. * 规则定义 table:$table|field:$field|condition:$condition|rule:$rule[|cycle:$cycle|max:$max][;......]
  1013. * 规则字段解释:table->要操作的数据表,不需要加表前缀;
  1014. * field->要操作的字段;
  1015. * condition->操作的条件,目前支持字符串,默认变量{$self}为执行行为的用户
  1016. * rule->对字段进行的具体操作,目前支持四则混合运算,如:1+score*2/2-3
  1017. * cycle->执行周期,单位(小时),表示$cycle小时内最多执行$max次
  1018. * max->单个周期内的最大执行次数($cycle和$max必须同时定义,否则无效)
  1019. * 单个行为后可加 ; 连接其他规则
  1020. * @param string $action 行为id或者name
  1021. * @param int $self 替换规则里的变量为执行用户的id
  1022. * @author huajie <banhuajie@163.com>
  1023. * @alter 蔡伟明 <314013107@qq.com>
  1024. * @return boolean|array: false解析出错 , 成功返回规则数组
  1025. */
  1026. function parse_action($action = null, $self){
  1027. if(empty($action)){
  1028. return false;
  1029. }
  1030. // 参数支持id或者name
  1031. if(is_numeric($action)){
  1032. $map = ['id' => $action];
  1033. }else{
  1034. $map = ['name' => $action];
  1035. }
  1036. // 查询行为信息
  1037. $info = model('admin/action')->where($map)->find();
  1038. if(!$info || $info['status'] != 1){
  1039. return false;
  1040. }
  1041. // 解析规则:table:$table|field:$field|condition:$condition|rule:$rule[|cycle:$cycle|max:$max][;......]
  1042. $rule = $info['rule'];
  1043. $rule = str_replace('{$self}', $self, $rule);
  1044. $rules = explode(';', $rule);
  1045. $return = [];
  1046. foreach ($rules as $key => &$rule){
  1047. $rule = explode('|', $rule);
  1048. foreach ($rule as $k => $fields){
  1049. $field = empty($fields) ? array() : explode(':', $fields);
  1050. if(!empty($field)){
  1051. $return[$key][$field[0]] = $field[1];
  1052. }
  1053. }
  1054. // cycle(检查周期)和max(周期内最大执行次数)必须同时存在,否则去掉这两个条件
  1055. if (!isset($return[$key]['cycle']) || !isset($return[$key]['max'])) {
  1056. unset($return[$key]['cycle'],$return[$key]['max']);
  1057. }
  1058. }
  1059. return $return;
  1060. }
  1061. }
  1062. if (!function_exists('execute_action')) {
  1063. /**
  1064. * 执行行为
  1065. * @param array|bool $rules 解析后的规则数组
  1066. * @param int $action_id 行为id
  1067. * @param array $user_id 执行的用户id
  1068. * @author huajie <banhuajie@163.com>
  1069. * @alter 蔡伟明 <314013107@qq.com>
  1070. * @return boolean false 失败 , true 成功
  1071. */
  1072. function execute_action($rules = false, $action_id = null, $user_id = null){
  1073. if(!$rules || empty($action_id) || empty($user_id)){
  1074. return false;
  1075. }
  1076. $return = true;
  1077. foreach ($rules as $rule){
  1078. // 检查执行周期
  1079. $map = ['action_id' => $action_id, 'user_id' => $user_id];
  1080. $map['create_time'] = ['gt', request()->time() - intval($rule['cycle']) * 3600];
  1081. $exec_count = model('admin/log')->where($map)->count();
  1082. if($exec_count > $rule['max']){
  1083. continue;
  1084. }
  1085. // 执行数据库操作
  1086. $field = $rule['field'];
  1087. $res = Db::name($rule['table'])->where($rule['condition'])->setField($field, array('exp', $rule['rule']));
  1088. if(!$res){
  1089. $return = false;
  1090. }
  1091. }
  1092. return $return;
  1093. }
  1094. }
  1095. if (!function_exists('get_location')) {
  1096. /**
  1097. * 获取当前位置
  1098. * @param string $id 节点id,如果没有指定,则取当前节点id
  1099. * @param bool $del_last_url 是否删除最后一个节点的url地址
  1100. * @param bool $check 检查节点是否存在,不存在则抛出错误
  1101. * @author 蔡伟明 <314013107@qq.com>
  1102. * @return mixed
  1103. */
  1104. function get_location($id = '', $del_last_url = false, $check = true)
  1105. {
  1106. $location = model('admin/menu')->getLocation($id, $del_last_url, $check);
  1107. return $location;
  1108. }
  1109. }
  1110. if (!function_exists('packet_exists')) {
  1111. /**
  1112. * 查询数据包是否存在,即是否已经安装
  1113. * @param string $name 数据包名
  1114. * @author 蔡伟明 <314013107@qq.com>
  1115. * @return bool
  1116. */
  1117. function packet_exists($name = '')
  1118. {
  1119. if (Db::name('admin_packet')->where('name', $name)->find()) {
  1120. return true;
  1121. } else {
  1122. return false;
  1123. }
  1124. }
  1125. }
  1126. if (!function_exists('load_assets')) {
  1127. /**
  1128. * 加载静态资源
  1129. * @param string $assets 资源名称
  1130. * @param string $type 资源类型
  1131. * @author 蔡伟明 <314013107@qq.com>
  1132. * @return string
  1133. */
  1134. function load_assets($assets = '', $type = 'css')
  1135. {
  1136. $assets_list = config('assets.'. $assets);
  1137. $result = '';
  1138. foreach ($assets_list as $item) {
  1139. if ($type == 'css') {
  1140. $result .= '<link rel="stylesheet" href="'.$item.'">';
  1141. } else {
  1142. $result .= '<script src="'.$item.'"></script>';
  1143. }
  1144. }
  1145. return $result;
  1146. }
  1147. }
  1148. if (!function_exists('parse_name')) {
  1149. /**
  1150. * 字符串命名风格转换
  1151. * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
  1152. * @param string $name 字符串
  1153. * @param integer $type 转换类型
  1154. * @return string
  1155. */
  1156. function parse_name($name, $type = 0) {
  1157. if ($type) {
  1158. return ucfirst(preg_replace_callback('/_([a-zA-Z])/', function($match){return strtoupper($match[1]);}, $name));
  1159. } else {
  1160. return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
  1161. }
  1162. }
  1163. }
  1164. if (!function_exists('home_url')) {
  1165. /**
  1166. * 生成前台入口url
  1167. * @param string $url 路由地址
  1168. * @param string|array $vars 变量
  1169. * @param bool|string $suffix 生成的URL后缀
  1170. * @param bool|string $domain 域名
  1171. * @author 小乌 <82950492@qq.com>
  1172. * @return string
  1173. */
  1174. function home_url($url = '', $vars = '', $suffix = true, $domain = false) {
  1175. $url = url($url, $vars, $suffix, $domain);
  1176. if (defined('ENTRANCE') && ENTRANCE == 'admin') {
  1177. $base_file = request()->baseFile();
  1178. $base_file = substr($base_file, strripos($base_file, '/') + 1);
  1179. return preg_replace('/\/'.$base_file.'/', '/index.php', $url);
  1180. } else {
  1181. return $url;
  1182. }
  1183. }
  1184. }
  1185. if (!function_exists('admin_url')) {
  1186. /**
  1187. * 生成后台入口url
  1188. * @param string $url 路由地址
  1189. * @param string|array $vars 变量
  1190. * @param bool|string $suffix 生成的URL后缀
  1191. * @param bool|string $domain 域名
  1192. * @author 小乌 <82950492@qq.com>
  1193. * @return string
  1194. */
  1195. function admin_url($url = '', $vars = '', $suffix = true, $domain = false) {
  1196. $url = url($url, $vars, $suffix, $domain);
  1197. if (defined('ENTRANCE') && ENTRANCE == 'admin') {
  1198. return $url;
  1199. } else {
  1200. return preg_replace('/\/index.php/', '/'.ADMIN_FILE, $url);
  1201. }
  1202. }
  1203. }
  1204. if (!function_exists('htmlpurifier')) {
  1205. /**
  1206. * html安全过滤
  1207. * @param string $html 要过滤的内容
  1208. * @author 蔡伟明 <314013107@qq.com>
  1209. * @return string
  1210. */
  1211. function htmlpurifier($html = '') {
  1212. $config = HTMLPurifier_Config::createDefault();
  1213. $purifier = new HTMLPurifier($config);
  1214. $clean_html = $purifier->purify($html);
  1215. return $clean_html;
  1216. }
  1217. }
  1218. if (!function_exists('extend_form_item')) {
  1219. /**
  1220. * 扩展表单项
  1221. * @param array $form 类型
  1222. * @param array $_layout 布局参数
  1223. * @author 蔡伟明 <314013107@qq.com>
  1224. * @return string
  1225. */
  1226. function extend_form_item($form = [], $_layout = []) {
  1227. if (!isset($form['type'])) return '';
  1228. if (!empty($_layout) && isset($_layout[$form['name']])) {
  1229. $form['_layout'] = $_layout[$form['name']];
  1230. }
  1231. $template = './extend/form/'.$form['type'].'/'.$form['type'].'.html';
  1232. if (file_exists($template)) {
  1233. $template_content = file_get_contents($template);
  1234. $view = new View();
  1235. return $view->display($template_content, array_merge($form, $_layout));
  1236. } else {
  1237. return '';
  1238. }
  1239. }
  1240. }
  1241. if (!function_exists('role_auth')) {
  1242. /**
  1243. * 读取当前用户权限
  1244. * @author 蔡伟明 <314013107@qq.com>
  1245. */
  1246. function role_auth() {
  1247. session('role_menu_auth', model('user/role')->roleAuth());
  1248. }
  1249. }
  1250. if (!function_exists('get_server_ip')) {
  1251. /**
  1252. * 获取服务器端IP地址
  1253. * @return array|false|string
  1254. */
  1255. function get_server_ip(){
  1256. if(isset($_SERVER)){
  1257. if($_SERVER['SERVER_ADDR']){
  1258. $server_ip = $_SERVER['SERVER_ADDR'];
  1259. }else{
  1260. $server_ip = $_SERVER['LOCAL_ADDR'];
  1261. }
  1262. }else{
  1263. $server_ip = getenv('SERVER_ADDR');
  1264. }
  1265. return $server_ip;
  1266. }
  1267. }
  1268. if (!function_exists('get_browser_type')) {
  1269. /**
  1270. * 获取浏览器类型
  1271. * @return string
  1272. */
  1273. function get_browser_type(){
  1274. $agent = $_SERVER["HTTP_USER_AGENT"];
  1275. if(strpos($agent,'MSIE') !== false || strpos($agent,'rv:11.0')) return "ie";
  1276. if(strpos($agent,'Firefox') !== false) return "firefox";
  1277. if(strpos($agent,'Chrome') !== false) return "chrome";
  1278. if(strpos($agent,'Opera') !== false) return 'opera';
  1279. if((strpos($agent,'Chrome') == false) && strpos($agent,'Safari') !== false) return 'safari';
  1280. if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'360SE')) return '360SE';
  1281. return 'unknown';
  1282. }
  1283. }
  1284. if (!function_exists('generate_rand_str')) {
  1285. /**
  1286. * 生成随机字符串
  1287. * @param int $length 生成长度
  1288. * @param int $type 生成类型:0-小写字母+数字,1-小写字母,2-大写字母,3-数字,4-小写+大写字母,5-小写+大写+数字
  1289. * @author 蔡伟明 <314013107@qq.com>
  1290. * @return string
  1291. */
  1292. function generate_rand_str($length = 8, $type = 0) {
  1293. $a = 'abcdefghijklmnopqrstuvwxyz';
  1294. $A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1295. $n = '0123456789';
  1296. switch ($type) {
  1297. case 1: $chars = $a; break;
  1298. case 2: $chars = $A; break;
  1299. case 3: $chars = $n; break;
  1300. case 4: $chars = $a.$A; break;
  1301. case 5: $chars = $a.$A.$n; break;
  1302. default: $chars = $a.$n;
  1303. }
  1304. $str = '';
  1305. for ($i = 0; $i < $length; $i++) {
  1306. $str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
  1307. }
  1308. return $str;
  1309. }
  1310. }