userController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. <?php
  2. class userController extends Controller
  3. {
  4. private $uid;
  5. public $initphp_list = array(
  6. 'index',
  7. 'node',
  8. 'my',
  9. 'modify',
  10. 'login',
  11. 'logout',
  12. 'qr',
  13. 'reg',
  14. 'docheckin',
  15. 'code',
  16. 'unfreeze',
  17. 'reset'
  18. );
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->checkLogin();
  23. }
  24. public function run()
  25. {
  26. $user = self::getUserService();
  27. $userinfo = $user->getUserById($this->uid);
  28. $this->view->set_tpl("index/user"); // 中间的模板
  29. $config = InitPHP::getConfig();
  30. $title = $config['sitename'];
  31. $userinfo['gravatar'] = $this->get_gravatar($userinfo['email']);
  32. $this->view->assign("userinfo", $userinfo);
  33. $this->view->assign("active", "userindex");
  34. $this->view->assign("title", $title);
  35. $this->view->display();
  36. }
  37. public function index()
  38. {
  39. $user = self::getUserService();
  40. $userinfo = $user->getUserById($this->uid);
  41. $this->view->set_tpl("index/user"); // 中间的模板
  42. $config = InitPHP::getConfig();
  43. $title = $config['sitename'];
  44. $userinfo['gravatar'] = $this->get_gravatar($userinfo['email']);
  45. $this->view->assign("userinfo", $userinfo);
  46. $this->view->assign("active", "userindex");
  47. $this->view->assign("title", $title);
  48. $this->view->display();
  49. }
  50. /**
  51. * 节点列表
  52. */
  53. public function node()
  54. {
  55. $user = self::getUserService();
  56. $userinfo = $user->getUserById($this->uid);
  57. $config = InitPHP::getConfig();
  58. $title = $config['sitename'];
  59. $node = self::getNode();
  60. $nodeinfo = $node->getAllNode();
  61. foreach ($nodeinfo as $k => $v) {
  62. $data[$v['node_type']][] = $v;
  63. }
  64. $userinfo['gravatar'] = $this->get_gravatar($userinfo['email']);
  65. $this->view->assign("userinfo", $userinfo);
  66. $this->view->assign("title", $title);
  67. $this->view->assign("active", "node");
  68. $this->view->assign("data", $data);
  69. $this->view->set_tpl("index/node");
  70. $this->view->display();
  71. }
  72. /**
  73. * 我的信息
  74. */
  75. public function my()
  76. {
  77. $user = self::getUserService();
  78. $userinfo = $user->getUserById($this->uid);
  79. $this->view->set_tpl("index/myinfo"); // 中间的模板
  80. $config = InitPHP::getConfig();
  81. $title = $config['sitename'];
  82. $userinfo['gravatar'] = $this->get_gravatar($userinfo['email']);
  83. $this->view->assign("userinfo", $userinfo);
  84. $this->view->assign("active", "info");
  85. $this->view->assign("title", $title);
  86. $this->view->display();
  87. }
  88. /**
  89. * 签到获取流量
  90. */
  91. public function docheckin()
  92. {
  93. $config = InitPHP::getConfig();
  94. $user = self::getUserService();
  95. $userinfo = $user->getUserById($this->uid);
  96. if ($userinfo['last_check_in_time'] > strtotime(date("Y-m-d"))) {
  97. JsMessage("您当前不能进行签到!", '/user/index.html');
  98. die();
  99. }
  100. if (($userinfo['transfer_enable'] - $userinfo['u'] - $userinfo['d']) < 2048 * 1024 * 1024) {
  101. $transfer_to_add = 2048;
  102. } else {
  103. $transfer_to_add = rand($config['check_min'], $config['check_max']);
  104. }
  105. $data['transfer_enable'] = $userinfo['transfer_enable'] + $transfer_to_add * 1024 * 1024;
  106. $data['last_check_in_time'] = time();
  107. $result = $user->updatePortPass($this->uid, $data);
  108. if ($result) {
  109. JsMessage("签到成功,你本次签到获得流量" . $transfer_to_add . "M!", '/user/index.html');
  110. } else
  111. JsMessage("您当前不能进行签到!", '/user/index.html');
  112. }
  113. /**
  114. * 获取邀请码
  115. */
  116. public function code()
  117. {
  118. $user = self::getUserService();
  119. $userinfo = $user->getUserById($this->uid);
  120. if ($userinfo['plan'] != "VIP") {
  121. JsMessage("您无权查看邀请码!");
  122. }
  123. $userinfo['gravatar'] = $this->get_gravatar($userinfo['email']);
  124. $code = $user->getInvite();
  125. $code = array(
  126. $code
  127. );
  128. $config = InitPHP::getConfig();
  129. $title = $config['sitename'];
  130. $this->view->set_tpl("index/code");
  131. $this->view->assign("userinfo", $userinfo);
  132. $this->view->assign("active", "code");
  133. $this->view->assign("title", $title);
  134. $this->view->assign("code", $code);
  135. $this->view->display();
  136. }
  137. /**
  138. * 二维码显示
  139. */
  140. public function qr()
  141. {
  142. $user = self::getUserService();
  143. $userinfo = $user->getUserById($this->uid);
  144. $nodeid = $this->controller->get_get("id");
  145. $node = InitPHP::getService("node");
  146. $nodeinfo = $node->getNodeById($nodeid);
  147. $ssurl = $nodeinfo['node_method'] . ":" . $userinfo['passwd'] . "@" . $nodeinfo['node_server'] . ":" . $userinfo['port'];
  148. $ssqr = "ss://" . base64_encode($ssurl);
  149. $qrcode = InitPHP::getLibrarys("qrcode");
  150. $qrcode->getQr($ssqr);
  151. die();
  152. }
  153. /**
  154. * 修改用户资料
  155. */
  156. public function modify()
  157. {
  158. $do = $this->controller->get_gp("do");
  159. if ($do == "info") {
  160. $data = $this->controller->get_gp(array(
  161. 'nowpassword',
  162. 'password',
  163. 'email'
  164. ));
  165. $user = self::getUserService();
  166. $result = $user->updateUserinfo($this->uid, $data);
  167. if (! $result) {
  168. JsMessage("当前密码错误!");
  169. } else {
  170. JsMessage("修改成功!", "/user/index.html");
  171. }
  172. } elseif ($do == "port_pass") {
  173. $data = $this->controller->get_gp(array(
  174. 'passwd'
  175. ));
  176. $user = self::getUserService();
  177. $result = $user->updatePortPass($this->uid, $data);
  178. JsMessage("修改成功", "/user/index.html");
  179. } else {
  180. $user = self::getUserService();
  181. $userinfo = $user->getUserById($this->uid);
  182. $this->view->set_tpl("index/modify"); // 中间的模板
  183. $config = InitPHP::getConfig();
  184. $title = $config['sitename'];
  185. $userinfo['gravatar'] = $this->get_gravatar($userinfo['email']);
  186. $this->view->assign("userinfo", $userinfo);
  187. $this->view->assign("active", "modify");
  188. $this->view->assign("title", $title);
  189. $this->view->display();
  190. }
  191. }
  192. public function reset(){
  193. $do=$this->controller->get_gp("do");
  194. $user = self::getUserService();
  195. if ($do=="pass"){
  196. $username=$this->controller->get_gp("username");
  197. $userinfo=$user->getUserByNameOrEmail($username);
  198. $code = md5(uniqid("shadowsocks" . time()));
  199. $result = $user->doReset($userinfo['uid'], $code);
  200. if ($userinfo) {
  201. $subject = '重置你的shadowsocks5密码,如果不是本人操作,请忽略本邮件!';
  202. $message = "<html>
  203. <head>
  204. <title>你正在重置你的shadowsocks5密码,如果不是本人操作,请忽略本邮件!</title>
  205. </head>
  206. <body>
  207. <p>尊敬的ss5用户:".$userinfo['user_name']."</p>
  208. <table style='width: 99.8%; '><tbody><tr><td id='QQMAILSTATIONERY’ style='background:url(https://rescdn.qqmail.com/zh_CN/htmledition/images/xinzhi/bg/a_08.jpg) no-repeat #f3f3eb; min-height:550px; padding: 100px 55px 200px 120px;'><font face=’宋体’ color='#FF0000’ size='5'>如需重置,请点击以下链接进行操作:<br > <a href='".SITE_URL."/user/reset.html?do=resetpass&uid=" . $userinfo['uid']. "&code=" . $code . "'>立即重置</a><br>如果您无法直接点击,请将以下网址复制到浏览器访问:".SITE_URL."/user/reset.html?do=resetpass&uid=" . $userinfo['uid'] . "&code=" . $code . "</font></td></tr></tbody></table>
  209. </body>
  210. </html>";
  211. sendmail($userinfo['email'],$subject,$message,'webmaster@tailsocks.com');
  212. JsMessage("重置密码已经提交,请查收邮箱".$userinfo['email']."进行下一步操作!");
  213. }else{
  214. JsMessage("用户不存在!");
  215. }
  216. }elseif ($do=='resetpass'){
  217. $uid=$this->controller->get_gp("uid");
  218. $code = $this->controller->get_gp("code");
  219. $pass=$this->controller->get_gp("password");
  220. if (strlen($pass)>6){
  221. $result=$user->doReset($uid, $code,$pass);
  222. if ($result) {
  223. JsMessage("密码修改成功,请使用新密码登录!","/user/login.html");
  224. } else {
  225. JsMessage("密码修改失败,可能是超时造成的!", '/');
  226. }
  227. }else {
  228. $result = $user->doReset($uid, $code, true);
  229. if ($result) {
  230. $config = InitPHP::getConfig();
  231. $title = $config['sitename'];
  232. $this->view->assign("title", $title);
  233. $this->view->set_tpl("index/resetpass"); // 中间的模板
  234. $this->view->display();
  235. } else {
  236. JsMessage("非法访问!", '/user/index.html');
  237. }
  238. }
  239. }
  240. else {
  241. $config = InitPHP::getConfig();
  242. $title = $config['sitename'];
  243. $this->view->assign("title", $title);
  244. $this->view->set_tpl("index/reset"); // 中间的模板
  245. $this->view->display();
  246. }
  247. }
  248. public function unfreeze()
  249. {
  250. $user = self::getUserService();
  251. $do = $this->controller->get_gp("do");
  252. if ($do == 'reset') {
  253. $code = $this->controller->get_gp("code");
  254. $result = $user->doFreeze($this->controller->get_gp("uid"), $code, true);
  255. if ($result) {
  256. JsMessage("您的账号已经解除冻结,可以正常使用!", '/user/index.html');
  257. } else {
  258. JsMessage("解冻失败,请确认步骤是否正确!", '/user/index.html');
  259. }
  260. } else {
  261. $code = md5(uniqid("shadowsocks" . time()));
  262. $result = $user->doFreeze($this->uid, $code);
  263. $userinfo = $user->getUserById($this->uid);
  264. if ($userinfo) {
  265. $subject = '请激活你的shadowsocks5!如果不是本人操作,请忽略本邮件!';
  266. $message = "<html>
  267. <head>
  268. <title>请激活你的shadowsocks5!如果不是本人操作,请忽略本邮件!</title>
  269. </head>
  270. <body>
  271. <p>尊敬的ss5用户:".$userinfo['user_name']."</p>
  272. <table style='width: 99.8%; '><tbody><tr><td id='QQMAILSTATIONERY’ style='background:url(https://rescdn.qqmail.com/zh_CN/htmledition/images/xinzhi/bg/a_08.jpg) no-repeat #f3f3eb; min-height:550px; padding: 100px 55px 200px 120px;'><font face=’宋体’ color='#FF0000’ size='5'>如需解除冻结,请点击以下链接进行解冻:<br > <a href='".SITE_URL."/user/unfreeze.html?do=reset&uid=" . $this->uid . "&code=" . $code . "'>立即激活</a><br>如果您无法直接点击,请将以下网址复制到浏览器访问:".SITE_URL."/user/unfreeze.html?do=reset&uid=" . $this->uid . "&code=" . $code . "</font></td></tr></tbody></table>
  273. </body>
  274. </html>";
  275. sendmail($userinfo['email'],$subject,$message,'webmaster@tailsocks.com');
  276. JsMessage("解冻申请已经提交,请查收邮箱进行下一步解冻!");
  277. }else{
  278. JsMessage("操作失败!");
  279. }
  280. }
  281. }
  282. public function login()
  283. {
  284. if ($this->controller->get_gp("do") == "login") {
  285. $data = $this->controller->get_gp(array(
  286. "username",
  287. "password",
  288. "remember_me"
  289. ));
  290. $user = self::getUserService();
  291. $result = $user->login($data);
  292. if ($result) {
  293. $session = self::getSession();
  294. $function = self::getFunction();
  295. $session->set('userid', intval($result['uid']));
  296. $session->set('username', htmlspecialchars($result['user_name']));
  297. $function->goto_url('/user/index.html');
  298. } else {
  299. JsMessage("用户名或密码错误!");
  300. }
  301. } else {
  302. $config = InitPHP::getConfig();
  303. $title = $config['sitename'];
  304. $this->view->assign("title", $title);
  305. $this->view->set_tpl("index/login"); // 中间的模板
  306. $this->view->display();
  307. }
  308. }
  309. public function reg()
  310. {
  311. $config = InitPHP::getConfig();
  312. if ($this->controller->get_gp("do") == "reg") {
  313. $user = self::getUserService();
  314. $userinfo = $this->controller->get_gp(array(
  315. "username",
  316. "password",
  317. "email",
  318. "code"
  319. ));
  320. if ($userinfo['username'] == "" || $userinfo['email'] == "") {
  321. JsMessage("用户名或邮箱不能为空!");
  322. }
  323. if ($config['isinviteonly']) {
  324. if ($user->checkInvite($userinfo['code'])) { // 检测邀请码
  325. if ($user->checkUser($userinfo)) {
  326. $data = array();
  327. $data['user_name'] = $userinfo['username'];
  328. $data['email'] = $userinfo['email'];
  329. $data['pass'] = md5($userinfo['password']);
  330. $data['passwd'] = rand(10000, 99999);
  331. $data['transfer_enable'] = $config['transfer'];
  332. // $data['port'] = $port;
  333. $data['t'] = 0;
  334. $data['u'] = 0;
  335. $data['d'] = 0;
  336. $data['plan'] = "A";
  337. $data['switch'] = 1;
  338. $data['enable'] = 1;
  339. $data['type'] = 7;
  340. $data['reg_date'] = date("Y-m-d H:i:s");
  341. $data['last_login_time'] = time();
  342. $data['invite_code'] = $userinfo['code'];
  343. $data['invite_num'] = $config['invite_num'];
  344. $data['money'] = 0.00;
  345. $uid = $user->createUser($data);
  346. if ($uid) {
  347. $user->deleteInvite($userinfo['code']);
  348. $session = self::getSession();
  349. $session->set('userid', intval($uid));
  350. $session->set('username', htmlspecialchars($data['user_name']));
  351. $this->sendMail($data['email']);
  352. JsMessage("注册成功,端口及密码请查看用户中心右下角", "/user/index.html");
  353. }
  354. } else {
  355. JsMessage("用户名或者Email已经被注册了!");
  356. }
  357. } else {
  358. JsMessage("站点开启了邀请注册,当前邀请码无效!");
  359. }
  360. } else { // 直接注册
  361. if ($user->checkUser($userinfo)) {
  362. $data = array();
  363. $data['user_name'] = $userinfo['username'];
  364. $data['email'] = $userinfo['email'];
  365. $data['pass'] = md5($userinfo['password']);
  366. $data['passwd'] = rand(10000, 99999);
  367. $data['transfer_enable'] = $config['transfer'];
  368. // $data['port'] = $port;
  369. $data['t'] = 0;
  370. $data['u'] = 0;
  371. $data['d'] = 0;
  372. $data['plan'] = "A";
  373. $data['switch'] = 1;
  374. $data['enable'] = 1;
  375. $data['type'] = 7;
  376. $data['reg_date'] = date("Y-m-d H:i:s");
  377. $data['invite_num'] = $config['invite_num'];
  378. $data['money'] = 0.00;
  379. $uid = $user->createUser($data);
  380. if ($uid) {
  381. $session = self::getSession();
  382. $session->set('userid', intval($uid));
  383. $session->set('username', htmlspecialchars($data['user_name']));
  384. $this->sendMail($data['email']);
  385. JsMessage("注册成功", "/user/run");
  386. }
  387. } else {
  388. JsMessage("用户名或者Email已经被注册了!");
  389. }
  390. }
  391. } else {
  392. $title = $config['sitename'];
  393. $isinviteonly = $config['isinviteonly'];
  394. $this->view->assign("title", $title);
  395. $this->view->assign("isinviteonly", $isinviteonly);
  396. $this->view->set_tpl("index/reg"); // 中间的模板
  397. $this->view->display();
  398. }
  399. }
  400. private function sendMail($to)
  401. {
  402. $subject = "欢迎使用ShadowSocks服务";
  403. $message = "<html>
  404. <head>
  405. <title>欢迎使用ShadowSocks服务</title>
  406. </head>
  407. <body>
  408. <p>欢迎您加入shadowsocks5.com</p>
  409. <table>
  410. <tr>
  411. 现在您可以免费使用我们的服务,每个月,您会有30G的流量供您使用SS服务,记得每天签到也会送流量哦!<br><font color='red'>注意:本系统会自动删除三天内使用流量为0的用户,请注册用户务必在三天内进行使用,否则将被判定为垃圾用户,及时释放资源以供其他用户使用!同时,请勿滥用本服务器进行发送垃圾邮件以及下载涉及版权的视频,谢谢合作!</font>
  412. </tr>
  413. </table>
  414. </body>
  415. </html>";
  416. // 当发送 HTML 电子邮件时,请始终设置 content-type
  417. sendmail($to,$subject,$message,'webmaster@tailsocks.com');
  418. }
  419. private function checkLogin()
  420. {
  421. $list = array(
  422. "login",
  423. 'reg',
  424. 'reset',
  425. 'unfreeze'
  426. );
  427. $session = self::getSession();
  428. $userid = $session->get('userid');
  429. $a = $this->getA();
  430. if (empty($userid)) {
  431. if (! in_array($a, $list)) {
  432. $function = self::getFunction();
  433. $function->goto_url('/user/login.html');
  434. }
  435. } else {
  436. $this->uid = $userid;
  437. }
  438. }
  439. public function logout()
  440. {
  441. $session = self::getSession();
  442. $function = self::getFunction();
  443. $session->clear();
  444. $function->goto_url('/user/login.html');
  445. }
  446. /**
  447. *
  448. * @return userService
  449. */
  450. private function getUserService()
  451. {
  452. return InitPHP::getService("user");
  453. }
  454. /**
  455. *
  456. * @return sessionInit
  457. */
  458. private function getSession()
  459. {
  460. return InitPHP::getUtils("session");
  461. }
  462. /**
  463. *
  464. * @return functionInit
  465. */
  466. private function getFunction()
  467. {
  468. return InitPHP::getLibrarys("function");
  469. }
  470. /**
  471. *
  472. * @return emailInit
  473. */
  474. private function getEmail()
  475. {
  476. return InitPHP::getLibrarys("email");
  477. }
  478. /**
  479. *
  480. * @return nodeService
  481. */
  482. private function getNode()
  483. {
  484. return InitPHP::getService("node");
  485. }
  486. /**
  487. * 获取头像
  488. *
  489. * @param unknown $email
  490. * @param number $s
  491. * @param string $d
  492. * @param string $r
  493. * @param string $img
  494. * @param unknown $atts
  495. * @return string
  496. */
  497. function get_gravatar($email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array())
  498. {
  499. $url = 'https://secure.gravatar.com/avatar/';
  500. $url .= md5(strtolower(trim($email)));
  501. $url .= "?s=$s&d=$d&r=$r";
  502. if ($img) {
  503. $url = '<img src="' . $url . '"';
  504. foreach ($atts as $key => $val)
  505. $url .= ' ' . $key . '="' . $val . '"';
  506. $url .= ' />';
  507. }
  508. return $url;
  509. }
  510. // public function getEmail(){
  511. // return InitPHP::getLibrarys("Email");
  512. // }
  513. }