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); }
public function login($uid, $time) { $sid = Tools::genSID(); Cookie::set(['sid' => $sid], $time + time()); $value = $uid; $this->client->setex($sid, $time, $value); }
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); }
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)); }
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); }
public function addInviteCode() { $uid = $this->attributes['id']; $code = new InviteCode(); $code->code = Tools::genRandomChar(32); $code->user = $uid; $code->save(); }
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); }
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()); }
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); }
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'); }
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()); }
/** * @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; }
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)); }
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); }
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; }
/** * @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; }
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); }
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)); }
/** * @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(); }
protected function getRandomEmail() { return Tools::genRandomChar(8) . "@sp3.me"; }
public function unusedTraffic() { $total = $this->attributes['u'] + $this->attributes['d']; $enable = $this->attributes['transfer_enable']; return Tools::flowAutoShow($enable - $total); }
public function totalTraffic() { $u = User::all()->sum('u'); $d = User::all()->sum('d'); return Tools::flowAutoShow($u + $d); }
public function logTime() { return Tools::toDateTime($this->attributes['log_time']); }
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); }
public function lastCheckInTime() { return Tools::toDateTime($this->attributes['last_check_in_time']); }
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)); }
public function testCheckHtml() { $legalText = 'xoxo'; $this->assertEquals($legalText, Tools::checkHtml($legalText)); }
/** * @return mixed */ public function CheckInTime() { return Tools::toDateTime($this->attributes['checkin_at']); }
public function getTrafficUsage() { $total = User::sum('u') + USer::sum('d'); return Tools::flowAutoShow($total); }
public function testCookieHash() { $str = Tools::genRandomChar(); Hash::cookieHash($str); }