Wecenter.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Utils;
  3. use App\Models\User;
  4. use App\Models\WecenterUser;
  5. use App\Services\Config;
  6. use App\Utils;
  7. class Wecenter
  8. {
  9. /**
  10. * 添加或者更新密码信息
  11. */
  12. public static function Add($user, $pwd)
  13. {
  14. if (Config::get('enable_wecenter')=='true') {
  15. $email=$user->email;
  16. $exists=WecenterUser::where("email", $email)->first();
  17. if ($exists==null) {
  18. $exists=new WecenterUser();
  19. $exists->password=md5(md5($pwd).Config::get('salt'));
  20. $exists->user_name=$user->user_name;
  21. $exists->email=$email;
  22. $exists->salt=Config::get('salt');
  23. $exists->group_id=5;
  24. $exists->save();
  25. } else {
  26. $exists->password=md5(md5($pwd).Config::get('salt'));
  27. $exists->salt=Config::get('salt');
  28. $exists->save();
  29. }
  30. }
  31. }
  32. public static function Delete($email)
  33. {
  34. if (Config::get('enable_wecenter')=='true') {
  35. WecenterUser::where("email", $email)->delete();
  36. }
  37. }
  38. public static function ChangeUserName($email1, $email2, $pwd, $username)
  39. {
  40. if (Config::get('enable_wecenter')=='true') {
  41. $exists=WecenterUser::where("email", $email1)->first();
  42. if ($exists!=null) {
  43. $exists->password=md5(md5($pwd).Config::get('salt'));
  44. $exists->user_name=$username;
  45. $exists->email=$email2;
  46. $exists->salt=Config::get('salt');
  47. $exists->save();
  48. } else {
  49. $exists=new WecenterUser();
  50. $exists->password=md5(md5($pwd).Config::get('salt'));
  51. $exists->user_name=$username;
  52. $exists->email=$email2;
  53. $exists->salt=Config::get('salt');
  54. $exists->group_id=5;
  55. $exists->save();
  56. }
  57. }
  58. }
  59. public static function Login($user, $pwd, $time)
  60. {
  61. if (Config::get('enable_wecenter')=='true') {
  62. $email=$user->email;
  63. $exists=WecenterUser::where("email", $email)->first();
  64. $expire_in = $time+time();
  65. Utils\Cookie::setwithdomain([Config::get('wecenter_cookie_prefix')."_user_login"=>Wecenter::encode_hash(json_encode(array(
  66. 'uid' => $exists->uid,
  67. 'user_name' => $user->email,
  68. 'password' => md5(md5($pwd).Config::get('salt'))
  69. )), md5(Config::get('wecenter_cookie_key') . $_SERVER['HTTP_USER_AGENT']))], $expire_in, Config::get('wecenter_system_main_domain'));
  70. }
  71. }
  72. public static function Loginout()
  73. {
  74. if (Config::get('enable_wecenter')=='true') {
  75. Utils\Cookie::setwithdomain([Config::get('wecenter_cookie_prefix')."_user_login"=>"loginout"], time()-86400, Config::get('wecenter_system_main_domain'));
  76. }
  77. }
  78. public static function encode_hash($hash_data, $hash_key = null)
  79. {
  80. $mcrypt = mcrypt_module_open(Wecenter::get_algorithms(), '', MCRYPT_MODE_ECB, '');
  81. mcrypt_generic_init($mcrypt, Wecenter::get_key($mcrypt, $hash_key), mcrypt_create_iv(mcrypt_enc_get_iv_size($mcrypt), MCRYPT_RAND));
  82. $result = mcrypt_generic($mcrypt, gzcompress($hash_data));
  83. mcrypt_generic_deinit($mcrypt);
  84. mcrypt_module_close($mcrypt);
  85. return Wecenter::get_algorithms() . '|' . Wecenter::str_to_hex($result);
  86. }
  87. private static function get_key($mcrypt, $key = null)
  88. {
  89. if (!$key) {
  90. $key = Config::get('wecenter_cookie_key');
  91. }
  92. return substr($key, 0, mcrypt_enc_get_key_size($mcrypt));
  93. }
  94. private static function get_algorithms()
  95. {
  96. $algorithms = mcrypt_list_algorithms();
  97. foreach ($algorithms as $algorithm) {
  98. if (strstr($algorithm, '-256')) {
  99. return $algorithm;
  100. }
  101. }
  102. foreach ($algorithms as $algorithm) {
  103. if (strstr($algorithm, '-128')) {
  104. return $algorithm;
  105. }
  106. }
  107. return end($algorithms);
  108. }
  109. private static function str_to_hex($string)
  110. {
  111. $hex = null;
  112. for ($i = 0; $i < strlen($string); $i++) {
  113. $ord = ord($string[$i]);
  114. $hexCode = dechex($ord);
  115. $hex .= substr('0' . $hexCode, -2);
  116. }
  117. return strtoupper($hex);
  118. }
  119. private static function hex_to_str($hex)
  120. {
  121. $string = null;
  122. for ($i = 0; $i < strlen($hex)-1; $i += 2) {
  123. $string .= chr(hexdec($hex[$i] . $hex[$i + 1]));
  124. }
  125. return $string;
  126. }
  127. }