common.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. if (!function_exists('get_this_class_methods')) {
  12. /**获取当前类方法
  13. * @param $class
  14. * @return array
  15. */
  16. function get_this_class_methods($class, $unarray = [])
  17. {
  18. $arrayall = get_class_methods($class);//获取当前类的方法
  19. $parent_class = get_parent_class($class);//获取父类
  20. //如果有父类
  21. if ($parent_class) {
  22. $arrayparent = get_class_methods($parent_class);//获取父类的方法
  23. $arraynow = array_diff($arrayall, $arrayparent);//去除父级的
  24. } else {
  25. $arraynow = $arrayall;
  26. }
  27. return array_diff($arraynow, $unarray);//去除无用的
  28. }
  29. }
  30. if (!function_exists('setconfig')) {
  31. /**
  32. * 修改config的函数
  33. * @param $arr1 or $string 配置前缀
  34. * @param $arr2 or $string 数据变量
  35. * @return bool 返回状态
  36. */
  37. function setconfig($name, $pat, $rep)
  38. {
  39. /**
  40. * 原理就是 打开config配置文件 然后使用正则查找替换 然后在保存文件. 不能修改值为数组的配置
  41. * 传递的参数为2个数组 前面的为配置 后面的为数值. 正则的匹配为单引号 如果你的是分号 请自行修改为分号
  42. * $pat[0] = 参数前缀; 例: default_return_type
  43. * $rep[0] = 要替换的内容; 例: json
  44. */
  45. $pats = $reps = [];
  46. if (is_array($pat) && is_array($rep)) {
  47. for ($i = 0; $i < count($pat); $i++) {
  48. $pats[$i] = '/\'' . $pat[$i] . '\'(.*?),/';
  49. $reps[$i] = "'" . $pat[$i] . "'" . "=>" . "'" . $rep[$i] . "',";
  50. }
  51. $fileurl = app()->getConfigPath() . $name . ".php";//配置文件路径
  52. $string = file_get_contents($fileurl); //加载配置文件
  53. $string = preg_replace($pats, $reps, $string); // 正则查找然后替换
  54. @file_put_contents($fileurl, $string); // 写入配置文件
  55. return true;
  56. } else if (is_string($pat) && is_string($rep)) {
  57. $pats = '/\'' . $pat . '\'(.*?),/';
  58. if (substr_count($rep, '[')) {
  59. $reps = "'" . $pat . "'" . "=>" . $rep . ",";
  60. } else {
  61. $rep = str_replace('\'', "", $rep);
  62. $reps = "'" . $pat . "'" . "=>" . "'" . $rep . "',";
  63. }
  64. $fileurl = app()->getConfigPath() . $name . ".php";//配置文件路径
  65. $string = file_get_contents($fileurl); //加载配置文件
  66. $string = preg_replace($pats, $reps, $string); // 正则查找然后替换
  67. @file_put_contents($fileurl, $string); // 写入配置文件
  68. return true;
  69. } else {
  70. return false;
  71. }
  72. }
  73. }
  74. if (!function_exists('arrayToText')) {
  75. /**
  76. * 将数组转成PHP文本
  77. * @param $array
  78. * @return string
  79. */
  80. function arrayToText($array)
  81. {
  82. $config = print_r($array, true);
  83. $config = str_replace('[', "\"", $config);
  84. $config = str_replace(']', "\"", $config);
  85. $input = explode("\n", $config);
  86. foreach ($input as $k => $v) {
  87. if (empty($v) || strpos($v, 'Array') !== false || strpos($v, '(') !== false || strpos($v, ')') !== false) {
  88. continue;
  89. }
  90. $tmpValArr = explode('=>', $v);
  91. if (count($tmpValArr) == 2) {
  92. $input[$k] = $tmpValArr[0] . '=> \'' . trim($tmpValArr[1]) . '\',';
  93. }
  94. }
  95. $config = implode("\n", $input);
  96. $config = str_replace('Array', "", $config);
  97. $config = str_replace('(', "[", $config);
  98. $config = str_replace(')', "],", $config);
  99. $config = rtrim($config, "\n");
  100. $config = rtrim($config, ",");
  101. $config = "<?php \n return " . $config . ';';
  102. // $fileurl = app()->getConfigPath() ."templates.php";
  103. // @file_put_contents($fileurl, $config); // 写入配置文件
  104. return $config;
  105. }
  106. }
  107. if (!function_exists('attr_format')) {
  108. /**
  109. * 格式化属性
  110. * @param $arr
  111. * @return array
  112. */
  113. function attr_format($arr): array
  114. {
  115. $len = count($arr);//获取数组长度
  116. $title = array_column($arr, 'value');//获取数组中的value值
  117. $result = [];
  118. if ($len > 0) {
  119. if ($len > 1) {
  120. $result = $arr[0]['detail'];
  121. for ($i = 0; $i < $len - 1; $i++) {
  122. $temp = $result;
  123. $result = [];
  124. foreach ($temp as $item) {
  125. foreach ($arr[$i + 1]['detail'] as $datum) {
  126. $result[] = trim($item) . ',' . trim($datum);
  127. }
  128. }
  129. }
  130. } else {
  131. foreach ($arr[0]['detail'] as $item) {
  132. $result[] = trim($item);
  133. }
  134. }
  135. }
  136. return [$result, $title];
  137. }
  138. }
  139. if (!function_exists('verify_domain')) {
  140. /**
  141. * 验证域名是否合法
  142. * @param string $domain
  143. * @return bool
  144. */
  145. function verify_domain(string $domain): bool
  146. {
  147. $res = "/^(?=^.{3,255}$)(http(s)?:\/\/)(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*$/";//正则验证
  148. if (preg_match($res, $domain))//匹配正则
  149. return true;
  150. else
  151. return false;
  152. }
  153. }