示例#1
0
 public function newToken($request, $response, $args)
 {
     // $data = $request->post('sdf');
     $email = $request->getParam('email');
     $email = strtolower($email);
     $passwd = $request->getParam('passwd');
     // Handle Login
     $user = User::where('email', '=', $email)->first();
     if ($user == null) {
         $res['ret'] = 0;
         $res['msg'] = "401 邮箱或者密码错误";
         return $this->echoJson($response, $res);
     }
     if (!Hash::checkPassword($user->pass, $passwd)) {
         $res['ret'] = 0;
         $res['msg'] = "402 邮箱或者密码错误";
         return $this->echoJson($response, $res);
     }
     $tokenStr = Tools::genToken();
     $storage = Factory::createTokenStorage();
     $expireTime = time() + 3600 * 24 * 7;
     if ($storage->store($tokenStr, $user, $expireTime)) {
         $res['ret'] = 1;
         $res['msg'] = "ok";
         $res['data']['token'] = $tokenStr;
         $res['data']['user_id'] = $user->id;
         return $this->echoJson($response, $res);
     }
     $res['ret'] = 0;
     $res['msg'] = "system error";
     return $this->echoJson($response, $res);
 }
示例#2
0
 public function login($uid, $time)
 {
     $sid = Tools::genSID();
     Cookie::set(['sid' => $sid], $time + time());
     $value = $uid;
     $this->client->setex($sid, $time, $value);
 }
示例#3
0
 public function postTraffic($request, $response, $args)
 {
     $nodeId = $args['id'];
     $node = Node::find($nodeId);
     $rate = $node->traffic_rate;
     $input = $request->getBody();
     $datas = json_decode($input, true);
     foreach ($datas as $data) {
         $user = User::find($data['user_id']);
         $user->t = time();
         $user->u = $user->u + $data['u'] * $rate;
         $user->d = $user->d + $data['d'] * $rate;
         $user->save();
         // log
         $totalTraffic = Tools::flowAutoShow(($data['u'] + $data['d']) * $rate);
         $traffic = new TrafficLog();
         $traffic->user_id = $data['user_id'];
         $traffic->u = $data['u'];
         $traffic->d = $data['d'];
         $traffic->node_id = $nodeId;
         $traffic->rate = $rate;
         $traffic->traffic = $totalTraffic;
         $traffic->log_time = time();
         $traffic->save();
     }
     $res = ["ret" => 1, "msg" => "ok"];
     return $this->echoJson($response, $res);
 }
示例#4
0
 public function update($request, $response, $args)
 {
     $id = $args['id'];
     $user = User::find($id);
     $user->email = $request->getParam('email');
     if ($request->getParam('pass') != '') {
         $user->pass = Hash::passwordHash($request->getParam('pass'));
     }
     if ($request->getParam('passwd') != '') {
         $user->passwd = $request->getParam('passwd');
     }
     $user->port = $request->getParam('port');
     $user->transfer_enable = Tools::toGB($request->getParam('transfer_enable'));
     $user->invite_num = $request->getParam('invite_num');
     $user->method = $request->getParam('method');
     $user->enable = $request->getParam('enable');
     $user->is_admin = $request->getParam('is_admin');
     $user->ref_by = $request->getParam('ref_by');
     if (!$user->save()) {
         $rs['ret'] = 0;
         $rs['msg'] = "修改失败";
         return $response->getBody()->write(json_encode($rs));
     }
     $rs['ret'] = 1;
     $rs['msg'] = "修改成功";
     return $response->getBody()->write(json_encode($rs));
 }
示例#5
0
 public static function login($uid, $time)
 {
     $sid = Tools::genSID();
     Cookie::set(['sid' => $sid], $time + time());
     $key = $sid;
     $value = $uid;
     self::getCache()->set($key, $value, $time);
 }
示例#6
0
文件: User.php 项目: nymc/ss-panel
 public function addInviteCode()
 {
     $uid = $this->attributes['id'];
     $code = new InviteCode();
     $code->code = Tools::genRandomChar(32);
     $code->user = $uid;
     $code->save();
 }
示例#7
0
 function getTrafficFromLogs()
 {
     $id = $this->attributes['id'];
     $traffic = TrafficLog::where('node_id', $id)->sum('u') + TrafficLog::where('node_id', $id)->sum('d');
     if ($traffic == 0) {
         return "暂无数据";
     }
     return Tools::flowAutoShow($traffic);
 }
示例#8
0
 public function testCheckInLog()
 {
     $log = new CheckInLog();
     $log->user_id = 1;
     $log->checkin_at = time();
     $log->traffic = 1024;
     $log->save();
     $this->assertEquals(Tools::toDateTime($log->checkin_at), $log->CheckInTime());
     $this->assertEquals(Tools::flowAutoShow($log->traffic), $log->traffic());
 }
示例#9
0
 public function sendMailPost($request, $response, $args)
 {
     $to = $request->getParam('email');
     try {
         Mail::send($to, "Test", 'test.tpl', ['time' => Tools::toDateTime(time())], [BASE_PATH . '/LICENSE']);
         $res = ["ret" => 1, "msg" => "ok"];
     } catch (\Exception $e) {
         $res = ["ret" => 0, "msg" => $e->getMessage()];
     }
     return $this->echoJson($response, $res);
 }
示例#10
0
 public function traffic($request, $response, $args)
 {
     $id = $args['id'];
     $counts = TrafficCount::where('user_id', '=', $id)->get();
     foreach ($counts as $counter) {
         $counter->u = Tools::flowAutoShow($counter->u);
         $counter->d = Tools::flowAutoShow($counter->d);
         $counter->v = Tools::flowAutoShow($counter->v);
     }
     return $this->view()->assign('counts', $counts)->display('admin/user/traffic.tpl');
 }
示例#11
0
 public function testNewTrafficLog()
 {
     // log
     $u = 1024;
     $d = 1024;
     $rate = 1;
     $id = 1;
     $nodeId = 1;
     $totalTraffic = Tools::flowAutoShow(($u + $d) * $rate);
     $traffic = new TrafficLog();
     $traffic->user_id = $id;
     $traffic->u = $u;
     $traffic->d = $d;
     $traffic->node_id = $nodeId;
     $traffic->rate = $rate;
     $traffic->traffic = $totalTraffic;
     $traffic->log_time = time();
     $this->assertEquals(true, $traffic->save());
 }
示例#12
0
 /**
  * @param $email string
  * @return bool
  */
 public static function sendResetEmail($email)
 {
     $pwdRst = new PasswordReset();
     $pwdRst->email = $email;
     $pwdRst->init_time = time();
     $pwdRst->expire_time = time() + 3600 * 24;
     // @todo
     $pwdRst->token = Tools::genRandomChar(64);
     if (!$pwdRst->save()) {
         return false;
     }
     $subject = Config::get('appName') . "重置密码";
     $resetUrl = Config::get('baseUrl') . "/password/token/" . $pwdRst->token;
     try {
         Mail::send($email, $subject, 'password/reset.tpl', ["resetUrl" => $resetUrl], []);
     } catch (Exception $e) {
         return false;
     }
     return true;
 }
示例#13
0
 public function addInvite($request, $response, $args)
 {
     $n = $request->getParam('num');
     $prefix = $request->getParam('prefix');
     $uid = $request->getParam('uid');
     if ($n < 1) {
         $res['ret'] = 0;
         return $response->getBody()->write(json_encode($res));
     }
     for ($i = 0; $i < $n; $i++) {
         $char = Tools::genRandomChar(32);
         $code = new InviteCode();
         $code->code = $prefix . $char;
         $code->user_id = $uid;
         $code->save();
     }
     $res['ret'] = 1;
     $res['msg'] = "邀请码添加成功";
     return $response->getBody()->write(json_encode($res));
 }
示例#14
0
 public function addTraffic($request, $response, $args)
 {
     // $data = json_decode($request->getParsedBody(),true);
     $id = $args['id'];
     $u = $request->getParam('u');
     $d = $request->getParam('d');
     $nodeId = $request->getParam('node_id');
     $node = Node::find($nodeId);
     $rate = $node->traffic_rate;
     $user = User::find($id);
     $user->t = time();
     $user->u = $user->u + $u * $rate;
     $user->d = $user->d + $d * $rate;
     if (!$user->save()) {
         $res = ["msg" => "update failed"];
         return $this->echoJson($response, $res, 400);
     }
     // log
     $totalTraffic = Tools::flowAutoShow(($u + $d) * $rate);
     $traffic = new TrafficLog();
     $traffic->user_id = $id;
     $traffic->u = $u;
     $traffic->d = $d;
     $traffic->node_id = $nodeId;
     $traffic->rate = $rate;
     $traffic->traffic = $totalTraffic;
     $traffic->log_time = time();
     $traffic->save();
     $res = ["ret" => 1, "msg" => "ok"];
     if (Config::get('log_traffic_dynamodb')) {
         try {
             $client = new DynamoTrafficLog();
             $id = $client->store($u, $d, $nodeId, $id, $totalTraffic, $rate);
             $res["id"] = $id;
         } catch (\Exception $e) {
             $res["msg"] = $e->getMessage();
             Logger::error($e->getMessage());
         }
     }
     return $this->echoJson($response, $res);
 }
示例#15
0
 public function createAdmin()
 {
     echo "add admin/ 创建管理员帐号.....";
     // ask for input
     fwrite(STDOUT, "Enter your email/输入管理员邮箱: ");
     // get input
     $email = trim(fgets(STDIN));
     // write input back
     fwrite(STDOUT, "Enter password for: {$email} / 为 {$email} 添加密码 ");
     $passwd = trim(fgets(STDIN));
     echo "Email: {$email}, Password: {$passwd}! ";
     fwrite(STDOUT, "Press [Y] to create admin..... 按下[Y]确认来确认创建管理员账户..... ");
     $y = trim(fgets(STDIN));
     if (strtolower($y) == "y") {
         echo "start create admin account";
         // create admin user
         // do reg user
         $user = new User();
         $user->user_name = "admin";
         $user->email = $email;
         $user->pass = Hash::passwordHash($passwd);
         $user->passwd = Tools::genRandomChar(6);
         $user->port = Tools::getLastPort() + 1;
         $user->t = 0;
         $user->u = 0;
         $user->d = 0;
         $user->transfer_enable = Tools::toGB(Config::get('defaultTraffic'));
         $user->invite_num = Config::get('inviteNum');
         $user->ref_by = 0;
         $user->is_admin = 1;
         if ($user->save()) {
             echo "Successful/添加成功!";
             return true;
         }
         echo "添加失败";
         return false;
     }
     echo "cancel";
     return false;
 }
示例#16
0
 /**
  * @param $email string
  * @return bool
  */
 public static function sendVerification($email)
 {
     $ttl = Config::get('emailVerifyTTL');
     $verification = EmailVerifyModel::where('email', '=', $email)->first();
     if ($verification == null) {
         $verification = new EmailVerifyModel();
         $verification->email = $email;
     }
     $verification->token = Tools::genRandomChar(Config::get('emailVerifyCodeLength'));
     $verification->expire_at = time() + $ttl * 60;
     if (!$verification->save()) {
         return false;
     }
     $appName = Config::get('appName');
     $subject = $appName . ' 邮箱验证';
     try {
         Mail::send($email, $subject, 'auth/verify.tpl', ['verification' => $verification, 'ttl' => $ttl], []);
     } catch (Exception $e) {
         return false;
     }
     return true;
 }
示例#17
0
 public function registerHandle($request, $response, $args)
 {
     $name = $request->getParam('name');
     $email = $request->getParam('email');
     $email = strtolower($email);
     $passwd = $request->getParam('passwd');
     $repasswd = $request->getParam('repasswd');
     $code = $request->getParam('code');
     $verifycode = $request->getParam('verifycode');
     // check code
     $c = InviteCode::where('code', $code)->first();
     if ($c == null) {
         $res['ret'] = 0;
         $res['error_code'] = self::WrongCode;
         $res['msg'] = "邀请码无效";
         return $this->echoJson($response, $res);
     }
     // check email format
     if (!Check::isEmailLegal($email)) {
         $res['ret'] = 0;
         $res['error_code'] = self::IllegalEmail;
         $res['msg'] = "邮箱无效";
         return $this->echoJson($response, $res);
     }
     // check pwd length
     if (strlen($passwd) < 8) {
         $res['ret'] = 0;
         $res['error_code'] = self::PasswordTooShort;
         $res['msg'] = "密码太短";
         return $this->echoJson($response, $res);
     }
     // check pwd re
     if ($passwd != $repasswd) {
         $res['ret'] = 0;
         $res['error_code'] = self::PasswordNotEqual;
         $res['msg'] = "两次密码输入不符";
         return $this->echoJson($response, $res);
     }
     // check email
     $user = User::where('email', $email)->first();
     if ($user != null) {
         $res['ret'] = 0;
         $res['error_code'] = self::EmailUsed;
         $res['msg'] = "邮箱已经被注册了";
         return $this->echoJson($response, $res);
     }
     // verify email
     if (Config::get('emailVerifyEnabled') && !EmailVerify::checkVerifyCode($email, $verifycode)) {
         $res['ret'] = 0;
         $res['msg'] = '邮箱验证代码不正确';
         return $this->echoJson($response, $res);
     }
     // check ip limit
     $ip = Http::getClientIP();
     $ipRegCount = Check::getIpRegCount($ip);
     if ($ipRegCount >= Config::get('ipDayLimit')) {
         $res['ret'] = 0;
         $res['msg'] = '当前IP注册次数超过限制';
         return $this->echoJson($response, $res);
     }
     // do reg user
     $user = new User();
     $user->user_name = $name;
     $user->email = $email;
     $user->pass = Hash::passwordHash($passwd);
     $user->passwd = Tools::genRandomChar(6);
     $user->port = Tools::getLastPort() + 1;
     $user->t = 0;
     $user->u = 0;
     $user->d = 0;
     $user->transfer_enable = Tools::toGB(Config::get('defaultTraffic'));
     $user->invite_num = Config::get('inviteNum');
     $user->reg_ip = Http::getClientIP();
     $user->ref_by = $c->user_id;
     if ($user->save()) {
         $res['ret'] = 1;
         $res['msg'] = "注册成功";
         $c->delete();
         return $this->echoJson($response, $res);
     }
     $res['ret'] = 0;
     $res['msg'] = "未知错误";
     return $this->echoJson($response, $res);
 }
示例#18
0
 public function doCheckIn($request, $response, $args)
 {
     if (!$this->user->isAbleToCheckin()) {
         $res['msg'] = "您似乎已经签到过了...";
         $res['ret'] = 1;
         return $response->getBody()->write(json_encode($res));
     }
     $traffic = rand(Config::get('checkinMin'), Config::get('checkinMax'));
     $this->user->transfer_enable = $this->user->transfer_enable + Tools::toMB($traffic);
     $this->user->last_check_in_time = time();
     $this->user->save();
     $res['msg'] = sprintf("获得了 %u MB流量.", $traffic);
     $res['ret'] = 1;
     return $response->getBody()->write(json_encode($res));
 }
示例#19
0
 /**
  * @param $ip
  * @param int $time
  * @return int
  */
 public static function getIpRegCount($ip, $time = 3600 * 24)
 {
     return User::where('reg_ip', $ip)->where('reg_date', '>', Tools::toDateTime(time() - $time))->count();
 }
示例#20
0
 protected function getRandomEmail()
 {
     return Tools::genRandomChar(8) . "@sp3.me";
 }
示例#21
0
 public function unusedTraffic()
 {
     $total = $this->attributes['u'] + $this->attributes['d'];
     $enable = $this->attributes['transfer_enable'];
     return Tools::flowAutoShow($enable - $total);
 }
示例#22
0
 public function totalTraffic()
 {
     $u = User::all()->sum('u');
     $d = User::all()->sum('d');
     return Tools::flowAutoShow($u + $d);
 }
示例#23
0
 public function logTime()
 {
     return Tools::toDateTime($this->attributes['log_time']);
 }
示例#24
0
 public function doCheckIn($request, $response, $args)
 {
     if (!$this->user->isAbleToCheckin()) {
         $res['msg'] = "您似乎已经签到过了...";
         $res['ret'] = 1;
         return $response->getBody()->write(json_encode($res));
     }
     $traffic = rand(Config::get('checkinMin'), Config::get('checkinMax'));
     $trafficToAdd = Tools::toMB($traffic);
     $this->user->transfer_enable = $this->user->transfer_enable + $trafficToAdd;
     $this->user->last_check_in_time = time();
     $this->user->save();
     // checkin log
     try {
         $log = new CheckInLog();
         $log->user_id = Auth::getUser()->id;
         $log->traffic = $trafficToAdd;
         $log->checkin_at = time();
         $log->save();
     } catch (\Exception $e) {
     }
     $res['msg'] = sprintf("获得了 %u MB流量.", $traffic);
     $res['ret'] = 1;
     return $this->echoJson($response, $res);
 }
示例#25
0
 public function lastCheckInTime()
 {
     return Tools::toDateTime($this->attributes['last_check_in_time']);
 }
示例#26
0
 public function registerHandle($request, $response, $next)
 {
     $name = $request->getParam('name');
     $email = $request->getParam('email');
     $email = strtolower($email);
     $passwd = $request->getParam('passwd');
     $repasswd = $request->getParam('repasswd');
     $code = $request->getParam('code');
     // check code
     $c = InviteCode::where('code', $code)->first();
     if ($c == null) {
         $res['ret'] = 0;
         $res['msg'] = "邀请码无效";
         return $response->getBody()->write(json_encode($res));
     }
     // check email format
     if (!Check::isEmailLegal($email)) {
         $res['ret'] = 0;
         $res['msg'] = "邮箱无效";
         return $response->getBody()->write(json_encode($res));
     }
     // check pwd length
     if (strlen($passwd) < 8) {
         $res['ret'] = 0;
         $res['msg'] = "密码太短";
         return $response->getBody()->write(json_encode($res));
     }
     // check pwd re
     if ($passwd != $repasswd) {
         $res['ret'] = 0;
         $res['msg'] = "两次密码输入不符";
         return $response->getBody()->write(json_encode($res));
     }
     // check email
     $user = User::where('email', $email)->first();
     if ($user != null) {
         $res['ret'] = 0;
         $res['msg'] = "邮箱已经被注册了";
         return $response->getBody()->write(json_encode($res));
     }
     // do reg user
     $user = new User();
     $user->user_name = $name;
     $user->email = $email;
     $user->pass = Hash::passwordHash($passwd);
     $user->passwd = Tools::genRandomChar(6);
     $user->port = Tools::getLastPort() + 1;
     $user->t = 0;
     $user->u = 0;
     $user->d = 0;
     $user->transfer_enable = Tools::toGB(Config::get('defaultTraffic'));
     $user->invite_num = Config::get('inviteNum');
     $user->ref_by = $c->user_id;
     if ($user->save()) {
         $res['ret'] = 1;
         $res['msg'] = "注册成功";
         $c->delete();
         return $response->getBody()->write(json_encode($res));
     }
     $res['ret'] = 0;
     $res['msg'] = "未知错误";
     return $response->getBody()->write(json_encode($res));
 }
示例#27
0
 public function testCheckHtml()
 {
     $legalText = 'xoxo';
     $this->assertEquals($legalText, Tools::checkHtml($legalText));
 }
示例#28
0
 /**
  * @return mixed
  */
 public function CheckInTime()
 {
     return Tools::toDateTime($this->attributes['checkin_at']);
 }
示例#29
0
 public function getTrafficUsage()
 {
     $total = User::sum('u') + USer::sum('d');
     return Tools::flowAutoShow($total);
 }
示例#30
0
 public function testCookieHash()
 {
     $str = Tools::genRandomChar();
     Hash::cookieHash($str);
 }