2022-03-14 19:13:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
use Gregwar\Captcha\CaptchaBuilder;
|
|
|
|
use Gregwar\Captcha\PhraseBuilder;
|
|
|
|
use App\Models\UsersModel;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
|
|
|
class Register extends BaseController
|
|
|
|
{
|
2024-04-07 22:40:57 +02:00
|
|
|
protected object $users;
|
|
|
|
protected object $captcha;
|
|
|
|
|
2022-03-14 19:13:10 +01:00
|
|
|
use ResponseTrait;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->captcha = new CaptchaBuilder;
|
2024-04-07 22:40:57 +02:00
|
|
|
$this->users = new UsersModel;
|
2022-03-14 19:13:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$this->captcha->build();
|
2024-04-07 22:40:57 +02:00
|
|
|
$this->session->set('phrase', $this->captcha->getPhrase());
|
2022-03-14 19:13:10 +01:00
|
|
|
echo get_header('Register');
|
|
|
|
echo view('register', ['captcha' => $this->captcha->inline()]);
|
|
|
|
echo get_footer(['register.js']);
|
|
|
|
}
|
|
|
|
|
2024-04-07 22:40:57 +02:00
|
|
|
public function newUser()
|
2022-03-14 19:13:10 +01:00
|
|
|
{
|
|
|
|
$data = $this->request->getVar();
|
|
|
|
$response = [
|
|
|
|
'ok' => false,
|
|
|
|
'msg' => ''
|
|
|
|
];
|
|
|
|
|
|
|
|
// First verify the captcha
|
2024-04-07 22:40:57 +02:00
|
|
|
if (
|
|
|
|
$this->session->has('phrase') &&
|
|
|
|
PhraseBuilder::comparePhrases($this->session->phrase, $data['phrase'])
|
|
|
|
) {
|
2022-03-14 19:13:10 +01:00
|
|
|
// Move the image
|
|
|
|
$file = $this->request->getFile('imginput');
|
2024-04-07 22:40:57 +02:00
|
|
|
if ($file)
|
|
|
|
{
|
2022-03-14 19:13:10 +01:00
|
|
|
// Verify is the file is correct and not, for example, a .exe renamed to .jpg
|
|
|
|
$ext = $file->guessExtension();
|
|
|
|
|
|
|
|
if ($ext != $file->getExtension()) $response['msg'] = 'The image is not valid';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Now we check if the user and/or email is already in use.
|
2024-04-07 22:40:57 +02:00
|
|
|
if ($this->users->compUser($data['username'], $data['email']))
|
2022-03-14 19:13:10 +01:00
|
|
|
$response['msg'] = 'Username and/or email is already in use';
|
2024-04-07 22:40:57 +02:00
|
|
|
else $response = $this->users->addUser($data, $file);
|
2022-03-14 19:13:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$response['msg'] = 'Captcha not valid';
|
|
|
|
}
|
|
|
|
return $this->respond($response);
|
|
|
|
}
|
|
|
|
|
2024-04-07 22:40:57 +02:00
|
|
|
public function newCaptcha()
|
2022-03-14 19:13:10 +01:00
|
|
|
{
|
|
|
|
$this->captcha->build();
|
2024-04-07 22:40:57 +02:00
|
|
|
$this->session->phrase = $this->captcha->getPhrase();
|
2022-03-14 19:13:10 +01:00
|
|
|
return $this->captcha->inline();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function ok()
|
|
|
|
{
|
|
|
|
echo get_header('Register');
|
|
|
|
echo view('register_ok');
|
|
|
|
echo get_footer();
|
|
|
|
}
|
|
|
|
}
|