Helpers.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the overtrue/wechat.
  9. *
  10. * (c) overtrue <i@overtrue.me>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace ByteDance\Kernel\Support;
  16. /*
  17. * helpers.
  18. *
  19. * @author overtrue <i@overtrue.me>
  20. */
  21. /**
  22. * Generate a signature.
  23. *
  24. * @param array $attributes
  25. * @param string $key
  26. * @param string $encryptMethod
  27. *
  28. * @return string
  29. */
  30. function generate_sign(array $attributes, $key, $encryptMethod = 'md5')
  31. {
  32. ksort($attributes);
  33. $attributes['key'] = $key;
  34. return strtoupper(call_user_func_array($encryptMethod, [urldecode(http_build_query($attributes))]));
  35. }
  36. /**
  37. * Get client ip.
  38. *
  39. * @return string
  40. */
  41. function get_client_ip()
  42. {
  43. if (!empty($_SERVER['REMOTE_ADDR'])) {
  44. $ip = $_SERVER['REMOTE_ADDR'];
  45. } else {
  46. // for php-cli(phpunit etc.)
  47. $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
  48. }
  49. return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
  50. }
  51. /**
  52. * Get current server ip.
  53. *
  54. * @return string
  55. */
  56. function get_server_ip()
  57. {
  58. if (!empty($_SERVER['SERVER_ADDR'])) {
  59. $ip = $_SERVER['SERVER_ADDR'];
  60. } elseif (!empty($_SERVER['SERVER_NAME'])) {
  61. $ip = gethostbyname($_SERVER['SERVER_NAME']);
  62. } else {
  63. // for php-cli(phpunit etc.)
  64. $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
  65. }
  66. return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
  67. }
  68. /**
  69. * Return current url.(弃用)
  70. *
  71. * @return string
  72. */
  73. function current_url()
  74. {
  75. $protocol = 'http://';
  76. if ((!empty($_SERVER['HTTPS']) && 'off' !== $_SERVER['HTTPS']) || ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? 'http') === 'https') {
  77. $protocol = 'https://';
  78. }
  79. return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  80. }
  81. /**
  82. * Return random string.
  83. *
  84. * @param string $length
  85. *
  86. * @return string
  87. */
  88. function str_random($length)
  89. {
  90. return Str::random($length);
  91. }
  92. /**
  93. * @param string $content
  94. * @param string $publicKey
  95. *
  96. * @return string
  97. */
  98. function rsa_public_encrypt($content, $publicKey)
  99. {
  100. $encrypted = '';
  101. openssl_public_encrypt($content, $encrypted, openssl_pkey_get_public($publicKey), OPENSSL_PKCS1_OAEP_PADDING);
  102. return base64_encode($encrypted);
  103. }