1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-26 12:23:06 +08:00
koogua 0336a54911 1.源文件增加版权信息
2.群组状态和课程协同
2021-06-13 15:49:47 +08:00

65 lines
1.2 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Library;
use Phalcon\Crypt;
use Phalcon\Di;
use Phalcon\Text;
class CsrfToken
{
/**
* @var Crypt
*/
protected $crypt;
protected $lifetime = 600;
protected $delimiter = '@@';
protected $fixed = 'KG';
public function __construct()
{
$this->crypt = Di::getDefault()->get('crypt');
}
public function getToken()
{
$content = [
time() + $this->lifetime,
$this->fixed,
Text::random(8),
];
$text = implode($this->delimiter, $content);
return $this->crypt->encryptBase64($text);
}
public function checkToken($token)
{
if (!$token) return false;
$text = $this->crypt->decryptBase64($token);
$params = explode($this->delimiter, $text);
if (count($params) != 3) {
return false;
}
if ($params[0] < time() || $params[1] != $this->fixed || strlen($params[2]) != 8) {
return false;
}
return true;
}
}