Skip to content

Commit

Permalink
Merge pull request #424 from orvice/dev
Browse files Browse the repository at this point in the history
some update
  • Loading branch information
orvice committed Mar 27, 2016
2 parents d9af44e + b5640aa commit 4f279e4
Show file tree
Hide file tree
Showing 33 changed files with 1,055 additions and 140 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ muKey = ''
// 邮件
mailDriver = 'mailgun' // mailgun or smtp

// 邮箱验证设置
emailVerifyEnabled = 'false' // 是否开启注册时邮箱验证 (true:开启 false:关闭)
emailVerifyCodeLength = '8' // 邮箱验证代码长度
emailVerifyTTL = '30' // 验证代码有效时间 单位分钟

// 用户签到设置
checkinTime = '22' // 签到间隔时间 单位小时
checkinMin = '93' // 签到最少流量 单位MB
Expand Down Expand Up @@ -47,6 +52,7 @@ redis_scheme = 'tcp'
redis_host = '127.0.0.1'
redis_port = '6379'
redis_database = '0'
redis_pass = ''

# mailgun
mailgun_key = ''
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea
.env
vendor/
composer.phar
composer.phar
4 changes: 3 additions & 1 deletion app/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function config($request, $response, $args)
"home-code" => DbConfig::get('home-code'),
"analytics-code" => DbConfig::get('analytics-code'),
"user-index" => DbConfig::get('user-index'),
"user-node" => DbConfig::get('user-node'),
];
return $this->view()->assign('conf', $conf)->display('admin/config.tpl');
}
Expand All @@ -74,6 +75,7 @@ public function updateConfig($request, $response, $args)
"home-code" => $request->getParam('homeCode'),
"app-name" => $request->getParam('appName'),
"user-index" => $request->getParam('userIndex'),
"user-node" => $request->getParam('userNode'),
];
foreach ($config as $key => $value) {
DbConfig::set($key, $value);
Expand All @@ -83,4 +85,4 @@ public function updateConfig($request, $response, $args)
return $response->getBody()->write(json_encode($res));
}

}
}
70 changes: 53 additions & 17 deletions app/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

namespace App\Controllers;

use App\Models\InviteCode;
use App\Services\Config;
use App\Utils\Check, App\Utils\Tools, App\Utils\Http;
use App\Models\InviteCode, App\Models\User;
use App\Services\Config, App\Services\Auth\EmailVerify, App\Services\Auth, App\Services\Mail;
use App\Utils\Check, App\Utils\Tools, App\Utils\Http, App\Utils\Hash;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

use App\Utils\Hash;
use App\Services\Auth;
use App\Models\User;

/**
* AuthController
Expand All @@ -37,13 +34,13 @@ public function loginHandle($request, $response, $args)
if ($user == null) {
$rs['ret'] = 0;
$rs['msg'] = "401 邮箱或者密码错误";
return $response->getBody()->write(json_encode($rs));
return $this->echoJson($response, $res);
}

if (!Hash::checkPassword($user->pass, $passwd)) {
$rs['ret'] = 0;
$rs['msg'] = "402 邮箱或者密码错误";
return $response->getBody()->write(json_encode($rs));
return $this->echoJson($response, $res);
}
// @todo
$time = 3600 * 24;
Expand All @@ -53,7 +50,7 @@ public function loginHandle($request, $response, $args)
Auth::login($user->id, $time);
$rs['ret'] = 1;
$rs['msg'] = "欢迎回来";
return $response->getBody()->write(json_encode($rs));
return $this->echoJson($response, $res);
}

public function register($request, $response, $next)
Expand All @@ -63,7 +60,8 @@ public function register($request, $response, $next)
if (isset($ary['code'])) {
$code = $ary['code'];
}
return $this->view()->assign('code', $code)->display('auth/register.tpl');
$requireEmailVerification = Config::get('emailVerifyEnabled');
return $this->view()->assign('code', $code)->assign('requireEmailVerification', $requireEmailVerification)->display('auth/register.tpl');
}

public function registerHandle($request, $response, $next)
Expand All @@ -74,40 +72,49 @@ public function registerHandle($request, $response, $next)
$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['msg'] = "邀请码无效";
return $response->getBody()->write(json_encode($res));
return $this->echoJson($response, $res);
}

// check email format
if (!Check::isEmailLegal($email)) {
$res['ret'] = 0;
$res['msg'] = "邮箱无效";
return $response->getBody()->write(json_encode($res));
return $this->echoJson($response, $res);
}
// check pwd length
if (strlen($passwd) < 8) {
$res['ret'] = 0;
$res['msg'] = "密码太短";
return $response->getBody()->write(json_encode($res));
return $this->echoJson($response, $res);
}

// check pwd re
if ($passwd != $repasswd) {
$res['ret'] = 0;
$res['msg'] = "两次密码输入不符";
return $response->getBody()->write(json_encode($res));
return $this->echoJson($response, $res);
}

// check email
$user = User::where('email', $email)->first();
if ($user != null) {
$res['ret'] = 0;
$res['msg'] = "邮箱已经被注册了";
return $response->getBody()->write(json_encode($res));
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);
}

// do reg user
Expand All @@ -129,11 +136,40 @@ public function registerHandle($request, $response, $next)
$res['ret'] = 1;
$res['msg'] = "注册成功";
$c->delete();
return $response->getBody()->write(json_encode($res));
return $this->echoJson($response, $res);
}
$res['ret'] = 0;
$res['msg'] = "未知错误";
return $response->getBody()->write(json_encode($res));
return $this->echoJson($response, $res);
}

public function sendVerifyEmail($request, $response, $next)
{
$res = array();
$email = $request->getParam('email');

if (!Check::isEmailLegal($email)) {
$res['ret'] = 0;
$res['msg'] = '邮箱无效';
return $this->echoJson($response, $res);
}

// check email
$user = User::where('email', $email)->first();
if ($user != null) {
$res['ret'] = 0;
$res['msg'] = "邮箱已经被注册了";
return $this->echoJson($response, $res);
}

if (EmailVerify::sendVerification($email)) {
$res['ret'] = 1;
$res['msg'] = '验证代码已发送至您的邮箱,请在登录邮箱后将验证码填到相应位置.';
} else {
$res['ret'] = 0;
$res['msg'] = '邮件发送失败,请联系管理员';
}
return $this->echoJson($response, $res);
}

public function logout($request, $response, $next)
Expand Down
5 changes: 3 additions & 2 deletions app/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Models\InviteCode;
use App\Services\Auth;
use App\Services\Config, App\Services\DbConfig;
use App\Services\Config, App\Services\DbConfig, App\Services\Logger;
use App\Utils\Http;

/**
Expand All @@ -22,7 +22,7 @@ public function code()
{
$msg = DbConfig::get('home-code');
$codes = InviteCode::where('user_id', '=', '0')->take(10)->get();
return $this->view()->assign('codes', $codes)->assign('msg',$msg)->display('code.tpl');
return $this->view()->assign('codes', $codes)->assign('msg', $msg)->display('code.tpl');
}

public function debug($request, $response, $args)
Expand All @@ -31,6 +31,7 @@ public function debug($request, $response, $args)
"ip" => Http::getClientIP(),
"version" => Config::get('version'),
];
Logger::debug(json_encode($res));
return $this->echoJson($response, $res);
}

Expand Down
22 changes: 11 additions & 11 deletions app/Controllers/Mu/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Controllers\BaseController;
use App\Models\Node, App\Models\TrafficLog, App\Models\User;
use App\Storage\Dynamodb\TrafficLog as DynamoTrafficLog;
use App\Services\Config;
use App\Services\Config, App\Services\Logger;
use App\Utils\Tools;

class UserController extends BaseController
Expand Down Expand Up @@ -55,20 +55,20 @@ public function addTraffic($request, $response, $args)
$traffic->log_time = time();
$traffic->save();

$msg = "ok";
$res = [
"ret" => 1,
"msg" => "ok",
];
if (Config::get('log_traffic_dynamodb')) {
try{
try {
$client = new DynamoTrafficLog();
$client->store($u, $d, $nodeId, $id, $totalTraffic, $rate);
}catch(\Exception $e){
$msg = $e->getMessage();
$id = $client->store($u, $d, $nodeId, $id, $totalTraffic, $rate);
$res["id"] = $id;
} catch (\Exception $e) {
$res["msg"] = $e->getMessage();
Logger::error($e->getMessage());
}
}

$res = [
"ret" => 1,
"msg" => $msg,
];
return $this->echoJson($response, $res);
}
}
14 changes: 14 additions & 0 deletions app/Models/EmailVerify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Models;

class EmailVerify extends Model
{

public $incrementing = false;

protected $table = 'sp_email_verify';

protected $primaryKey = 'email';

}
10 changes: 10 additions & 0 deletions app/Models/Log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace App\Models;


class Log extends Model
{
protected $table = "sp_log";
}
57 changes: 57 additions & 0 deletions app/Services/Auth/EmailVerify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php


namespace App\Services\Auth;

use App\Services\Config, App\Services\Mail;
use App\Models\EmailVerify as EmailVerifyModel;
use App\Utils\Tools;

class EmailVerify
{
/**
* @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;
}

/**
* @param string $email
* @param string $verify_code
* @return bool
*/
public static function checkVerifyCode($email, $verify_code)
{
$verification = EmailVerifyModel::where('email', '=', $email)->first();
if ($verification == null || $verification->expire_at < time() || $verification->token !== $verify_code) {
return false;
}
$verification->delete();
return true;
}
}
34 changes: 34 additions & 0 deletions app/Services/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php


namespace App\Services;

use App\Models\Log;

class Logger
{

public static function newLog($type, $msg)
{
$log = new Log();
$log->type = $type;
$log->msg = $msg;
$log->created_time = time();
return $log->save();
}

public static function info($msg)
{
return self::newLog("info", $msg);
}

public static function error($msg)
{
return self::newLog("error", $msg);
}

public static function debug($msg)
{
return self::newLog("debug", $msg);
}
}
Loading

0 comments on commit 4f279e4

Please sign in to comment.