userService.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /**
  3. * userService类
  4. * @author meitu
  5. */
  6. class userService extends Service
  7. {
  8. /**
  9. *
  10. * @var userDao
  11. */
  12. private $userDao;
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->userDao = InitPHP::getDao("user");
  17. }
  18. /**
  19. * 添加一个用户
  20. */
  21. public function createUser($data)
  22. {
  23. $port = $this->userDao->getLastPort();
  24. $data['port'] = $port + rand(1, 2);
  25. return $this->userDao->createUser($data);
  26. }
  27. /**
  28. * 更新用户的信息
  29. *
  30. * @param integer $id
  31. * @param array $data
  32. * @return boolean
  33. */
  34. public function updateUser($id, $data)
  35. {
  36. return $this->userDao->updateUser($id, $data);
  37. }
  38. public function resetTran()
  39. {
  40. set_time_limit(0);
  41. $users = $this->getUsers();
  42. sort($users);
  43. foreach ($users as $k => $v) {
  44. if ((time() - $v['last_login_time'] > 86400 * 30) && (time() - $v['last_check_in_time'] > 86400 * 30) && $v['switch'] == 1 && $v['plan'] == 'A') {
  45. if ($v['u'] + $v['d'] < 1024 * 1024) {
  46. self::delUser($v['uid']);
  47. $subject = $v['user_name'] . ':您在shadowsocks5上的账号由于长期未使用已经被冻结';
  48. $message = "<html>
  49. <head>
  50. <title>欢迎使用ShadowSocks服务</title>
  51. </head>
  52. <body>
  53. <p>尊敬的ss5用户:" . $v['user_name'] . "</p>
  54. <table>
  55. <tr>
  56. 您在 <a href='".SITE_URL."'>SS5</a> 上的账号由于长期未使用已经被冻结,具体可能的情况如下:
  57. <br>1、您已没有近一个月没有登录过我们的网站了;
  58. <br>2、您在本月的使用流量小于1M;
  59. <br>您可以登录网站后按照网站提示进行解除冻结操作,在冻结解除之前您将无法继续使用ss5提供的shadowsocks服务,感谢您的支持与理解!
  60. <br>本邮件由系统自动发送,请不要直接回复本邮件,如有问题请发送邮件到support@tailsocks.com
  61. </tr>
  62. </table>
  63. </body>
  64. </html>";
  65. sendmail($v['email'],$subject,$message,'webmaster@tailsocks.com');
  66. sleep(1);
  67. }
  68. }
  69. }
  70. return $this->userDao->resetTran();
  71. }
  72. /**
  73. * 获取所有用户的总流量
  74. */
  75. public function getAllTran()
  76. {
  77. return $this->userDao->getAllTran();
  78. }
  79. /**
  80. * 更新用户信息
  81. *
  82. * @param integer $id
  83. * @param array $data
  84. * @return bool
  85. */
  86. public function updateUserinfo($id, $data)
  87. {
  88. $userinfo = $this->userDao->getUserById($id);
  89. $nowpass = md5($data['nowpassword']);
  90. if ($userinfo['pass'] != $nowpass) {
  91. return false;
  92. }
  93. if ($data['password'] != "") {
  94. $userdata['pass'] = md5($data['password']);
  95. }
  96. if ($data['email'] != "") {
  97. $userdata['email'] = $data['email'];
  98. }
  99. if (empty($userdata)) {
  100. return true;
  101. }
  102. return $this->userDao->updateUser($id, $userdata);
  103. }
  104. public function updatePortPass($id, $data)
  105. {
  106. return $this->userDao->updateUser($id, $data);
  107. }
  108. /**
  109. * 根据用户id获取用户信息
  110. *
  111. * @param int $uid
  112. * 用户id
  113. * @return Ambigous
  114. */
  115. public function getUserById($uid)
  116. {
  117. return $this->userDao->getUserById($uid);
  118. }
  119. /**
  120. * 登陆
  121. *
  122. * @param array $data
  123. * @return boolean|Ambigous <Ambigous, boolean, multitype:>
  124. */
  125. public function login($data)
  126. {
  127. $userinfo = $this->userDao->getUserByName($data['username']);
  128. if ($userinfo['pass'] != md5($data['password'])) {
  129. $userinfo = $this->userDao->getUserByEmail($data['username']);
  130. if ($userinfo['pass'] != md5($data['password'])) {
  131. return false;
  132. }
  133. }
  134. $u_data['last_login_time'] = time();
  135. $this->userDao->updateUser($userinfo['uid'], $u_data);
  136. return $userinfo;
  137. }
  138. public function getUserByNameOrEmail($name)
  139. {
  140. $userinfo = $this->userDao->getUserByName($name);
  141. if (!$userinfo) {
  142. $userinfo = $this->userDao->getUserByEmail($name);
  143. if (!$userinfo) {
  144. return false;
  145. }
  146. }
  147. return $userinfo;
  148. }
  149. /**
  150. * 对用户解除冻结
  151. *
  152. * @param int $uid
  153. * 用户的uid
  154. * @param string $code
  155. * 校验码
  156. * @param bool $pass
  157. * @return bool
  158. */
  159. public function doReset($uid, $code, $pass = FALSE)
  160. {
  161. if ($pass === true) {
  162. $user_info = $this->userDao->getUserById($uid);
  163. if ($user_info['code'] == $code && time() < $user_info['vaild_time']) {
  164. return true;
  165. } else {
  166. return false;
  167. }
  168. } elseif (strlen($pass) > 6) {
  169. $user_info = $this->userDao->getUserById($uid);
  170. if ($user_info['code'] == $code && time() < $user_info['vaild_time']) {
  171. $data['code'] = "";
  172. $data['vaild_time'] = 0;
  173. $data['pass'] = md5($pass);
  174. return $this->userDao->updateUser($uid, $data);
  175. } else {
  176. return false;
  177. }
  178. } else {
  179. $data['code'] = $code;
  180. $data['vaild_time'] = time() + 3600;
  181. return $this->userDao->updateUser($uid, $data);
  182. }
  183. }
  184. /**
  185. * 获取所有用户
  186. */
  187. public function getUsers($num = NULL, $offset = 0)
  188. {
  189. return $this->userDao->getUsers($num, $offset);
  190. }
  191. /**
  192. * 获取所有用户
  193. */
  194. public function getUserByPort($port)
  195. {
  196. return $this->userDao->getUserByField(array(
  197. 'port' => $port
  198. ));
  199. }
  200. /**
  201. * 删除一个用户
  202. *
  203. * @param integer $uid
  204. * @return boolean
  205. */
  206. public function delUser($uid)
  207. {
  208. return $this->userDao->delUser($uid);
  209. }
  210. /**
  211. * 获取所有用户数
  212. *
  213. * @return number
  214. */
  215. public function getCount()
  216. {
  217. return $this->userDao->getCount();
  218. }
  219. /**
  220. * 检测用户是否可以注册
  221. *
  222. * @param array $userinfo
  223. * @return boolean
  224. */
  225. public function checkUser($userinfo)
  226. {
  227. return $this->userDao->checkUser($userinfo);
  228. }
  229. /**
  230. * 检测邀请码
  231. *
  232. * @param string $code
  233. * @return boolean
  234. */
  235. public function checkInvite($code)
  236. {
  237. $invite = InitPHP::getDao("invite");
  238. $data = $invite->getOneByInvite($code);
  239. if ($data['status'] == 1) {
  240. return true;
  241. } else {
  242. return false;
  243. }
  244. }
  245. /**
  246. * 删除邀请码
  247. *
  248. * @param string $code
  249. */
  250. public function deleteInvite($code)
  251. {
  252. $invite = InitPHP::getDao("invite");
  253. return $invite->delete($code);
  254. }
  255. /**
  256. * 增加邀请码
  257. *
  258. * @param string $pre
  259. * 邀请码前缀设置
  260. * @param int $num
  261. * 产生个数
  262. * @param string $platform
  263. * 产生平台来源
  264. * @return boolean
  265. */
  266. public function add($pre, $num, $platform)
  267. {
  268. $invite = InitPHP::getDao("invite");
  269. $str = "";
  270. for ($i = 0; $i < $num; $i++) {
  271. $data = array();
  272. $data['code'] = $pre . $i . substr(md5($i . time() . uniqid()), 0, 8);
  273. $data['platform'] = $platform;
  274. $invite->add($data);
  275. $str .= $data['code'] . "<br>";
  276. }
  277. return $str;
  278. }
  279. /**
  280. * 获取一个随机邀请码
  281. */
  282. public function getInvite()
  283. {
  284. $invite = InitPHP::getDao("invite");
  285. $codes = $invite->getAll();
  286. return $codes[rand(0, count($codes) - 1)];
  287. }
  288. /**
  289. * 对用户解除冻结
  290. *
  291. * @param int $uid
  292. * 用户的uid
  293. * @param string $code
  294. * 校验码
  295. * @param bool|string $free
  296. * 是否执行解冻
  297. * @return bool
  298. */
  299. public function doFreeze($uid, $code, $free = FALSE)
  300. {
  301. if ($free) {
  302. $user_info = $this->userDao->getUserById($uid);
  303. if ($user_info['code'] == $code && time() < $user_info['vaild_time']) {
  304. $data['switch'] = 1;
  305. // $data['enable'] = 1;
  306. $data['code'] = "";
  307. $data['vaild_time'] = 0;
  308. return $this->userDao->updateUser($uid, $data);
  309. } else {
  310. return false;
  311. }
  312. } else {
  313. $data['code'] = $code;
  314. $data['vaild_time'] = time() + 86400 * 2;
  315. return $this->userDao->updateUser($uid, $data);
  316. }
  317. }
  318. }