Functions.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. function _uc_authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)
  3. {
  4. $ckey_length = 4;
  5. $key = md5($key ? $key : UC_KEY);
  6. $keya = md5(substr($key, 0, 16));
  7. $keyb = md5(substr($key, 16, 16));
  8. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
  9. $cryptkey = $keya . md5($keya . $keyc);
  10. $key_length = strlen($cryptkey);
  11. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
  12. $string_length = strlen($string);
  13. $result = '';
  14. $box = range(0, 255);
  15. $rndkey = array();
  16. for ($i = 0; $i <= 255; $i++)
  17. {
  18. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  19. }
  20. for ($j = $i = 0; $i < 256; $i++)
  21. {
  22. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  23. $tmp = $box[$i];
  24. $box[$i] = $box[$j];
  25. $box[$j] = $tmp;
  26. }
  27. for ($a = $j = $i = 0; $i < $string_length; $i++)
  28. {
  29. $a = ($a + 1) % 256;
  30. $j = ($j + $box[$a]) % 256;
  31. $tmp = $box[$a];
  32. $box[$a] = $box[$j];
  33. $box[$j] = $tmp;
  34. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  35. }
  36. if ($operation == 'DECODE')
  37. {
  38. if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16))
  39. {
  40. return substr($result, 26);
  41. }
  42. else
  43. {
  44. return '';
  45. }
  46. }
  47. else
  48. {
  49. return $keyc . str_replace('=', '', base64_encode($result));
  50. }
  51. }
  52. function _uc_stripslashes($string)
  53. {
  54. if (is_array($string))
  55. {
  56. foreach ($string as $key => $val)
  57. {
  58. $string[$key] = _stripslashes($val);
  59. }
  60. }
  61. else
  62. {
  63. $string = stripslashes($string);
  64. }
  65. return $string;
  66. }
  67. /**
  68. * 字符串命名风格转换
  69. * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
  70. * @param string $name 字符串
  71. * @param integer $type 转换类型
  72. * @return string
  73. */
  74. function parse_name($name, $type = 0)
  75. {
  76. if ($type)
  77. {
  78. return ucfirst(preg_replace_callback('/_([a-zA-Z])/', function($match)
  79. {
  80. return strtoupper($match[1]);
  81. }, $name));
  82. }
  83. else
  84. {
  85. return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
  86. }
  87. }