just register

This commit is contained in:
themohooks 2024-07-04 13:09:20 +03:00
parent cf7f7e959a
commit 0b42a6731c
7 changed files with 282 additions and 13 deletions

View file

@ -0,0 +1,148 @@
<?php
namespace App\Controllers\Api;
use App\Services\{Auth, Router, GenerateRandomStr, DB, Json, Word};
use \App\Controllers\ExceptionRegister;
use \App\Core\Page;
use donatj\UserAgent\UserAgentParser;
class Register
{
public function __construct()
{
$ip = $_SERVER['REMOTE_ADDR'];
$lockFile = $_SERVER['DOCUMENT_ROOT'].'/lock/request_lock_' . $ip;
file_put_contents($lockFile, 'lock');
$ch = curl_init('http://' . $_SERVER['HTTP_HOST'] . '/' . $_GET['username']);
curl_setopt($ch, CURLOPT_URL, 'http://' . $_SERVER['HTTP_HOST'] . '/' . $_GET['username']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYSTATUS, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36');
curl_exec($ch);
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if (!strcasecmp(DB::query('SELECT username FROM users WHERE (LOWER(username) LIKE :username)', array(':username' => '%' . $username . '%'))[0]['username'], $username) === false || !preg_match("#^[a-zA-Z0-9]+$#", $username)) {
if (Word::strlen(ltrim($username)) >= 5 && Word::strlen(ltrim($username)) <= 20 && Router::checkCurl($_SERVER['HTTP_HOST'] . '/' . $username) != 200) {
if (Word::strlen(ltrim($password)) >= 5 && Word::strlen(ltrim($password)) <= 120) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (!DB::query('SELECT email FROM users WHERE email=:email', array(':email' => $email))) {
$content = Json::return(
array(
'route' => 'NONE',
'regdate' => time()
)
);
DB::query('INSERT INTO users VALUES (\'0\', :username, :email, :password 5, :content)', array(':username' => ltrim($username), ':password' => password_hash(ltrim($password), PASSWORD_BCRYPT), ':email' => $email, ':content' => $content));
$cstrong = True;
$token = GenerateRandomStr::gen_uuid();
$user_id = DB::query('SELECT id FROM users WHERE username=:username', array(':username' => $username))[0]['id'];
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$parser = new UserAgentParser();
$ua = $parser->parse();
$ua = $parser();
$servicekey = GenerateRandomStr::gen_uuid();
$url = 'http://ip-api.com/json/'.$ip;
$response = file_get_contents($url);
$data = json_decode($response, true);
$loc = $data['country'].', '.$data['city'];
DB::query('INSERT INTO login_tokens VALUES (\'0\', :token, :user_id, :platform, :browser, :browserversion, 0, :ip, :servicekey, :loc)', array(
':token' => $token,
':user_id' => $user_id,
':platform' => $ua->platform(),
':browser' => $ua->browser(),
':browserversion' => $ua->browserVersion(),
':ip' => $ip,
':servicekey' => $servicekey,
':loc' => $loc
));
setcookie("NGALLERYSESS", $token, time() + 120 * 180 * 240 * 720, '/', NULL, NULL, TRUE);
setcookie("NGALLERYSESS_", '1', time() + 120 * 180 * 240 * 360, '/', NULL, NULL, TRUE);
setcookie("NGALLERYID", $user_id, time() + 10 * 10 * 24 * 72, '/', NULL, NULL, TRUE);
echo json_encode(
array(
'errorcode' => '0',
'error' => 0
)
);
} else {
echo json_encode(
array(
'errorcode' => '2',
'error' => 1
)
);
}
} else {
echo json_encode(
array(
'errorcode' => '3',
'error' => 1
)
);
}
} else {
echo json_encode(
array(
'errorcode' => '4',
'error' => 1
)
);
}
} else {
echo json_encode(
array(
'errorcode' => '5',
'error' => 1
)
);
}
} else {
echo json_encode(
array(
'errorcode' => '6',
'error' => 1
)
);
}
unlink($lockFile);
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace App\Controllers;
use \App\Services\{Router, Auth, DB, Json};
use \App\Controllers\ExceptionRegister;
use \App\Core\Page;
class RegisterController
{
public static function i()
{
Page::set('Register');
}
}

View file

@ -13,6 +13,7 @@ class Routes
{
Router::get('/', 'MainController@i');
Router::get('/login', 'LoginController@i');
Router::get('/register', 'RegisterController@i');
Router::get('/photo/$id', 'PhotoController@i');
Router::post('/api/login', 'ApiController@login');

20
app/Services/Word.php Normal file
View file

@ -0,0 +1,20 @@
<?php
namespace App\Services;
use App\Services\GenerateRandomStr;
class Word
{
public static function strlen($word)
{
$len = strlen($word);
if (preg_match("/[\p{Cyrillic}]/u", $word)) {
$len /= 2;
}
return $len;
}
}

View file

@ -4,7 +4,7 @@
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=PT+Sans+Narrow:wght@400;700&amp;display=swap" rel="stylesheet">
<link rel="stylesheet" href="/static/css/fontawesome/css/all.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
<link rel="stylesheet" href="/static/css/style.css">
<link rel="stylesheet" href="/static/css/desktop.css">
<link rel="stylesheet" href="/static/css/trans.css">

View file

@ -34,8 +34,7 @@
</form>
<div style="margin-top:15px"><a href="/register.php" class="mf-button">Регистрация</a></div>
<div style="margin-top:15px"><a href="/forgot.php" class="mf-label-narrow">Забыли пароль?</a></div>
<div style="margin-top:15px"><a href="/register" class="mf-button">Регистрация</a></div>
</div><br />
<script>
@ -81,16 +80,7 @@
</center>
</td>
</tr>
<tr>
<td class="footer"><b><a href="/">Главная</a> &nbsp; &nbsp; <a href="https://forum.transphoto.org">Форум</a> &nbsp; &nbsp; <a href="/rules/">Правила</a> &nbsp; &nbsp; <a href="/admin/">Редколлегия</a></b><br>
<a href="/set.php?pcver=0">Мобильная версия</a><br><a href="/set.php?dark=1" style="display:inline-block; padding:1px 10px; margin-top:5px; background-color:#333; color:#fff">Тёмная тема</a>
<div class="sitecopy">&copy; Администрация ТрансФото и авторы материалов, 2002—2024<br>Использование фотографий и иных материалов, опубликованных на сайте, допускается только с разрешения их авторов.</div>
<div style="margin:15px 0">
</div>
</td>
</tr>
</table>

View file

@ -0,0 +1,92 @@
<html lang="ru">
<head>
<?php include($_SERVER['DOCUMENT_ROOT'] . '/views/components/LoadHead.php'); ?>
</head>
<body>
<div id="backgr"></div>
<table class="tmain">
<?php include($_SERVER['DOCUMENT_ROOT'] . '/views/components/Navbar.php'); ?>
<tr>
<td class="main">
<center>
<h1>Регистрация</h1>
<div class="mf-center-block">
<form method="post" id="form" class="p20i mf-label mf-center-block-wide">
<input type="hidden" name="accept" value="yes">
<input type="text" name="username" id="username" class="mf-input-wide" style="margin-top:10px" maxlength="50" placeholder="Ваш никнейм" value="">
<div style="color:#e00" id="err_username"></div>
<input type="email" name="email" id="email" class="mf-input-wide" style="margin-top:10px" maxlength="50" placeholder="Ваш e-mail" value="">
<div style="color:#e00" id="err_email"></div>
<input type="password" name="password" id="password" class="mf-input-wide" style="margin-top:10px" maxlength="50" placeholder="Ваш пароль" value="">
<div style="color:#e00" id="err_password"></div>
<input type="button" id="regbtn" class="mf-button-wide" style="margin-top:15px" value="Зарегистрироваться">
</form><br><br>
<br>
</div>
<script>
$(document).ready(function() {
$('#email').on('input', function() {
$('#err_email').html('');
});
$('#regbtn').on('click', function() {
var username = $('#username').val().trim();
var err_username = $('#err_username').html('');
var email = $('#email').val().trim();
var err_email = $('#err_email').html('');
var password = $('#password').val().trim();
var err_password = $('#err_password').html('');
if (username == '') err_username.html('Поле не заполнено');
if (password == '') err_password.html('Поле не заполнено');
if (email == '') err_email.html('Поле не заполнено');
else
if (!email.match(/^[0-9a-z_\-.]+@[0-9a-z_\-^.]+\.[a-z]{2,4}$/i)) err_email.html('Некорректный адрес');
if (err_email.html() == '' && captchaPassed) {
$(this).prop('disabled', true).val('Отправка данных...');
$.get('/api/register', {
username: username,
email: email,
password: password
}, function(r) {
if (r != 0) {
$('#err_email').html(r == 1 ? 'Пользователь с таким e-mail уже зарегистрирован' : 'На этот адрес уже было отправлено письмо со ссылкой для подтверждения.<br /><br />Проверьте, не попало ли оно в папку "Спам"!');
$('#regbtn').prop('disabled', false).val('Зарегистрироваться');
} else $('#form').submit();
})
.fail(function(jx) {
if (jx.responseText != '') alert(jx.responseText);
});
}
});
});
</script>
</center>
</td>
</tr>
<tr>
</tbody>
</table>
</body>
</html>