common.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. if ($parent_class = get_parent_class($class)) {
  20. $arrayparent = get_class_methods($parent_class);
  21. $arraynow = array_diff($arrayall, $arrayparent);//去除父级的
  22. } else {
  23. $arraynow = $arrayall;
  24. }
  25. return array_diff($arraynow, $unarray);//去除无用的
  26. }
  27. }
  28. if (!function_exists('setconfig')) {
  29. /**
  30. * 修改config的函数
  31. * @param $arr1 or $string 配置前缀
  32. * @param $arr2 or $string 数据变量
  33. * @return bool 返回状态
  34. */
  35. function setconfig($name, $pat, $rep)
  36. {
  37. /**
  38. * 原理就是 打开config配置文件 然后使用正则查找替换 然后在保存文件. 不能修改值为数组的配置
  39. * 传递的参数为2个数组 前面的为配置 后面的为数值. 正则的匹配为单引号 如果你的是分号 请自行修改为分号
  40. * $pat[0] = 参数前缀; 例: default_return_type
  41. * $rep[0] = 要替换的内容; 例: json
  42. */
  43. $pats = $reps = [];
  44. if (is_array($pat) && is_array($rep)) {
  45. for ($i = 0; $i < count($pat); $i++) {
  46. $pats[$i] = '/\'' . $pat[$i] . '\'(.*?),/';
  47. $reps[$i] = "'" . $pat[$i] . "'" . "=>" . "'" . $rep[$i] . "',";
  48. }
  49. $fileurl = app()->getConfigPath() . $name . ".php";
  50. $string = file_get_contents($fileurl); //加载配置文件
  51. $string = preg_replace($pats, $reps, $string); // 正则查找然后替换
  52. @file_put_contents($fileurl, $string); // 写入配置文件
  53. return true;
  54. } else if (is_string($pat) && is_string($rep)) {
  55. $pats = '/\'' . $pat . '\'(.*?),/';
  56. if (substr_count($rep, '[')) {
  57. $reps = "'" . $pat . "'" . "=>" . $rep . ",";
  58. } else {
  59. $rep = str_replace('\'', "", $rep);
  60. $reps = "'" . $pat . "'" . "=>" . "'" . $rep . "',";
  61. }
  62. $fileurl = app()->getConfigPath() . $name . ".php";
  63. $string = file_get_contents($fileurl); //加载配置文件
  64. $string = preg_replace($pats, $reps, $string); // 正则查找然后替换
  65. @file_put_contents($fileurl, $string); // 写入配置文件
  66. return true;
  67. } else {
  68. return false;
  69. }
  70. }
  71. }
  72. if (!function_exists('arrayToText')) {
  73. /**
  74. * 修改config的函数
  75. * @param $array
  76. * @return string
  77. */
  78. function arrayToText($array)
  79. {
  80. $config = print_r($array, true);
  81. $config = str_replace('[', "\"", $config);
  82. $config = str_replace(']', "\"", $config);
  83. $input = explode("\n", $config);
  84. foreach ($input as $k => $v) {
  85. if (empty($v) || strpos($v, 'Array') !== false || strpos($v, '(') !== false || strpos($v, ')') !== false) {
  86. continue;
  87. }
  88. $tmpValArr = explode('=>', $v);
  89. if (count($tmpValArr) == 2) {
  90. $input[$k] = $tmpValArr[0] . '=> \'' . trim($tmpValArr[1]) . '\',';
  91. }
  92. }
  93. $config = implode("\n", $input);
  94. $config = str_replace('Array', "", $config);
  95. $config = str_replace('(', "[", $config);
  96. $config = str_replace(')', "],", $config);
  97. $config = rtrim($config, "\n");
  98. $config = rtrim($config, ",");
  99. $config = "<?php \n return " . $config . ';';
  100. // $fileurl = app()->getConfigPath() ."templates.php";
  101. // @file_put_contents($fileurl, $config); // 写入配置文件
  102. return $config;
  103. }
  104. }
  105. if (!function_exists('attr_format')) {
  106. /**
  107. * 格式化属性
  108. * @param $arr
  109. * @return array
  110. */
  111. function attr_format($arr): array
  112. {
  113. $len = count($arr);
  114. $title = array_column($arr, 'value');
  115. $result = [];
  116. if ($len > 0) {
  117. if ($len > 1) {
  118. $result = $arr[0]['detail'];
  119. for ($i = 0; $i < $len - 1; $i++) {
  120. $temp = $result;
  121. $result = [];
  122. foreach ($temp as $item) {
  123. foreach ($arr[$i + 1]['detail'] as $datum) {
  124. $result[] = trim($item) . ',' . trim($datum);
  125. }
  126. }
  127. }
  128. } else {
  129. foreach ($arr[0]['detail'] as $item) {
  130. $result[] = trim($item);
  131. }
  132. }
  133. }
  134. return [$result, $title];
  135. }
  136. }
  137. if (!function_exists('verify_domain')) {
  138. /**
  139. * 验证域名是否合法
  140. * @param string $domain
  141. * @return bool
  142. */
  143. function verify_domain(string $domain): bool
  144. {
  145. $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+)*$/";
  146. if (preg_match($res, $domain))
  147. return true;
  148. else
  149. return false;
  150. }
  151. }