CodeExtend.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace app\extra\tools;
  3. class CodeExtend
  4. {
  5. /**
  6. * 数组转XML内容
  7. * @param array $data
  8. * @return string
  9. */
  10. public static function arr2xml($data)
  11. {
  12. return "<xml>" . self::_arr2xml($data) . "</xml>";
  13. }
  14. /**
  15. * 解析XML内容到数组
  16. * @param string $xml
  17. * @return array
  18. */
  19. public static function xml2arr($xml)
  20. {
  21. // 使用simplexml_load_string并禁用外部实体加载
  22. $xmlObject = simplexml_load_string(
  23. $xml,
  24. 'SimpleXMLElement',
  25. LIBXML_NOCDATA | LIBXML_NOBLANKS | LIBXML_NOENT
  26. );
  27. if ($xmlObject === false) {
  28. throw new \RuntimeException("Failed to parse XML");
  29. }
  30. return json_decode(json_encode($xmlObject), true);
  31. }
  32. /**
  33. * XML内容生成
  34. * @param array $data 数据
  35. * @param string $content
  36. * @return string
  37. */
  38. private static function _arr2xml($data, $content = '')
  39. {
  40. foreach ($data as $key => $val) {
  41. is_numeric($key) && $key = 'item';
  42. $content .= "<{$key}>";
  43. if (is_array($val) || is_object($val)) {
  44. $content .= self::_arr2xml($val);
  45. } elseif (is_string($val)) {
  46. $content .= '<![CDATA[' . preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/", '', $val) . ']]>';
  47. } else {
  48. $content .= $val;
  49. }
  50. $content .= "</{$key}>";
  51. }
  52. return $content;
  53. }
  54. public static function createNoncestr($length = 32, $str = "")
  55. {
  56. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  57. for ($i = 0; $i < $length; $i++) {
  58. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  59. }
  60. return $str;
  61. }
  62. /**
  63. * 生成随机编码
  64. * @param integer $size 编码长度
  65. * @param integer $type 编码类型(1纯数字,2纯字母,3数字字母)
  66. * @param string $prefix 编码前缀
  67. * @return string
  68. */
  69. public static function random(int $size = 10, int $type = 1, string $prefix = ''): string
  70. {
  71. $numbs = '0123456789';
  72. $chars = 'abcdefghijklmnopqrstuvwxyz';
  73. if ($type === 1) $chars = $numbs;
  74. if ($type === 3) $chars = "{$numbs}{$chars}";
  75. $code = $prefix . $chars[rand(1, strlen($chars) - 1)];
  76. while (strlen($code) < $size) $code .= $chars[rand(0, strlen($chars) - 1)];
  77. return $code;
  78. }
  79. /**
  80. * 生成日期编码
  81. * @param integer $size 编码长度
  82. * @param string $prefix 编码前缀
  83. * @return string
  84. */
  85. public static function uniqidDate(int $size = 16, string $prefix = ''): string
  86. {
  87. if ($size < 14) $size = 14;
  88. $code = $prefix . date('Ymd') . (date('H') + date('i')) . date('s');
  89. while (strlen($code) < $size) $code .= rand(0, 9);
  90. return $code;
  91. }
  92. /**
  93. * 生成数字编码
  94. * @param integer $size 编码长度
  95. * @param string $prefix 编码前缀
  96. * @return string
  97. */
  98. public static function uniqidNumber(int $size = 12, string $prefix = ''): string
  99. {
  100. $time = strval(time());
  101. if ($size < 10) $size = 10;
  102. $code = $prefix . (intval($time[0]) + intval($time[1])) . substr($time, 2) . rand(0, 9);
  103. while (strlen($code) < $size) $code .= rand(0, 9);
  104. return $code;
  105. }
  106. /**
  107. * 文本转码
  108. * @param string $text 文本内容
  109. * @param string $target 目标编码
  110. * @return string
  111. */
  112. public static function text2utf8(string $text, string $target = 'UTF-8'): string
  113. {
  114. [$first2, $first4] = [substr($text, 0, 2), substr($text, 0, 4)];
  115. if ($first4 === chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF)) $ft = 'UTF-32BE';
  116. elseif ($first4 === chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00)) $ft = 'UTF-32LE';
  117. elseif ($first2 === chr(0xFE) . chr(0xFF)) $ft = 'UTF-16BE';
  118. elseif ($first2 === chr(0xFF) . chr(0xFE)) $ft = 'UTF-16LE';
  119. return mb_convert_encoding($text, $target, $ft ?? mb_detect_encoding($text));
  120. }
  121. /**
  122. * 数据解密处理
  123. * @param mixed $data 加密数据
  124. * @param string $skey 安全密钥
  125. * @return string
  126. */
  127. public static function encrypt($data, string $skey): string
  128. {
  129. $iv = static::random(16, 3);
  130. $value = openssl_encrypt(serialize($data), 'AES-256-CBC', $skey, 0, $iv);
  131. return static::enSafe64(json_encode(['iv' => $iv, 'value' => $value]));
  132. }
  133. /**
  134. * 数据加密处理
  135. * @param string $data 解密数据
  136. * @param string $skey 安全密钥
  137. * @return mixed
  138. */
  139. public static function decrypt(string $data, string $skey)
  140. {
  141. $attr = json_decode(static::deSafe64($data), true);
  142. return unserialize(openssl_decrypt($attr['value'], 'AES-256-CBC', $skey, 0, $attr['iv']));
  143. }
  144. /**
  145. * Base64Url 安全编码
  146. * @param string $text 待加密文本
  147. * @return string
  148. */
  149. public static function enSafe64(string $text): string
  150. {
  151. return rtrim(strtr(base64_encode($text), '+/', '-_'), '=');
  152. }
  153. /**
  154. * Base64Url 安全解码
  155. * @param string $text 待解密文本
  156. * @return string
  157. */
  158. public static function deSafe64(string $text): string
  159. {
  160. return base64_decode(str_pad(strtr($text, '-_', '+/'), strlen($text) % 4, '='));
  161. }
  162. /**
  163. * 压缩数据对象
  164. * @param mixed $data
  165. * @return string
  166. */
  167. public static function enzip($data): string
  168. {
  169. return static::enSafe64(gzcompress(serialize($data)));
  170. }
  171. /**
  172. * 解压数据对象
  173. * @param string $string
  174. * @return mixed
  175. */
  176. public static function dezip(string $string)
  177. {
  178. return unserialize(gzuncompress(static::deSafe64($string)));
  179. }
  180. }