functions.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. use Hashids\Hashids;
  3. /**
  4. * Here is your custom functions.
  5. */
  6. /*
  7. 快速排序
  8. */
  9. function quickSort($array)
  10. {
  11. if(!isset($array[1]))
  12. return $array;
  13. $mid = $array[0]; //获取一个用于分割的关键字,一般是首个元素
  14. $leftArray = array();
  15. $rightArray = array();
  16. foreach($array as $v)
  17. {
  18. if($v > $mid)
  19. $rightArray[] = $v; //把比$mid大的数放到一个数组里
  20. if($v < $mid)
  21. $leftArray[] = $v; //把比$mid小的数放到另一个数组里
  22. }
  23. $leftArray = quickSort($leftArray); //把比较小的数组再一次进行分割
  24. $leftArray[] = $mid; //把分割的元素加到小的数组后面,不能忘了它哦
  25. $rightArray = quickSort($rightArray); //把比较大的数组再一次进行分割
  26. return array_merge($leftArray,$rightArray); //组合两个结果
  27. }
  28. // 获取Hashids 对象
  29. function getHashids(){
  30. global $hashids;
  31. if($hashids instanceof Hashids ){
  32. return $hashids;
  33. }
  34. $hashids = new Hashids('D6M97LIvpp4qWuz3nKzqi6yYN4GAA61b',32);
  35. return $hashids;
  36. }
  37. /**
  38. * 获取对象或数组的属性值
  39. * @param $param
  40. * @param $key
  41. * @param string $default
  42. * @return mixed|string
  43. */
  44. function getProp($param, $key, $default = '')
  45. {
  46. $result = $default;
  47. if (is_object($param) && isset($param->$key)) {
  48. $result = $param->$key;
  49. }
  50. if (is_array($param) && isset($param[$key])) {
  51. $result = $param[$key];
  52. }
  53. return $result;
  54. }
  55. /**
  56. * 获取真实IP
  57. */
  58. function get_client_ip($type = 0, $adv = true)
  59. {
  60. $type = $type ? 1 : 0;
  61. if ($adv) {
  62. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  63. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  64. $pos = array_search('unknown', $arr);
  65. if (false !== $pos) {
  66. unset($arr[$pos]);
  67. }
  68. $ip = trim($arr[0]);
  69. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  70. $ip = $_SERVER['HTTP_CLIENT_IP'];
  71. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  72. $ip = $_SERVER['REMOTE_ADDR'];
  73. }
  74. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  75. $ip = $_SERVER['REMOTE_ADDR'];
  76. }
  77. // IP地址合法验证
  78. $long = sprintf("%u", ip2long($ip));
  79. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  80. return $ip[$type];
  81. }
  82. /**
  83. * 获取真实IP
  84. */
  85. function get_client_ua()
  86. {
  87. $ua = $_SERVER['HTTP_USER_AGENT'];
  88. return $ua;
  89. }