PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/fun/Source.fun.php

https://github.com/ywl890227/longphp
PHP | 92 lines | 64 code | 9 blank | 19 comment | 14 complexity | b56c9780eeb2c63e428172e6c57bafa5 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. function autoload($classname){
  3. global $file;
  4. $action = 'Action_'.$classname;
  5. $classname = htmlspecialchars($classname, ENT_QUOTES, 'UTF-8');
  6. require_once DIR_APP.$file.$classname.'.app.php';
  7. if(!class_exists($action)){
  8. if(DEBUG){
  9. exit('控制器: '.$action.' 不存在');
  10. }else{
  11. header('HTTP/1.1 404 Not Found');
  12. header("status: 404 Not Found");
  13. }
  14. }
  15. }
  16. /**
  17. * @param string $string 原文或者密文
  18. * @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE
  19. * @param string $key 密钥
  20. * @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效
  21. * @return string 处理后的 原文或者 经过 base64_encode 处理后的密文
  22. *
  23. * @example
  24. *
  25. * $a = authcode('abc', 'ENCODE', 'key');
  26. * $b = authcode($a, 'DECODE', 'key'); // $b(abc)
  27. *
  28. * $a = authcode('abc', 'ENCODE', 'key', 3600);
  29. * $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为空
  30. */
  31. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0){
  32. $ckey_length = 4;
  33. // 随机密钥长度 取值 0-32;
  34. // 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
  35. // 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
  36. // 当此值为 0 时,则不产生随机密钥
  37. $key = md5($key ? $key : EABAX::getAppInf('KEY'));
  38. $keya = md5(substr($key, 0, 16));
  39. $keyb = md5(substr($key, 16, 16));
  40. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  41. $cryptkey = $keya.md5($keya.$keyc);
  42. $key_length = strlen($cryptkey);
  43. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  44. $string_length = strlen($string);
  45. $result = '';
  46. $box = range(0, 255);
  47. $rndkey = array();
  48. for($i = 0; $i <= 255; $i++)
  49. {
  50. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  51. }
  52. for($j = $i = 0; $i < 256; $i++)
  53. {
  54. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  55. $tmp = $box[$i];
  56. $box[$i] = $box[$j];
  57. $box[$j] = $tmp;
  58. }
  59. for($a = $j = $i = 0; $i < $string_length; $i++)
  60. {
  61. $a = ($a + 1) % 256;
  62. $j = ($j + $box[$a]) % 256;
  63. $tmp = $box[$a];
  64. $box[$a] = $box[$j];
  65. $box[$j] = $tmp;
  66. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  67. }
  68. if($operation == 'DECODE')
  69. {
  70. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16))
  71. {
  72. return substr($result, 26);
  73. }
  74. else
  75. {
  76. return '';
  77. }
  78. }
  79. else
  80. {
  81. return $keyc.str_replace('=', '', base64_encode($result));
  82. }
  83. }