. * */ namespace Vvveb\Controller\User; use function Vvveb\__; use function Vvveb\email; use function Vvveb\siteSettings; use Vvveb\System\Event; use Vvveb\System\Sites; use Vvveb\System\Traits\Spam; use Vvveb\System\User\User; use Vvveb\System\Validator; use function Vvveb\url; class Signup extends \Vvveb\Controller\Base { use Spam; function addUser() { //$this->checkAlreadyLoggedIn(); $validator = new Validator(['signup']); if ($this->request->post && ($this->view->errors['login'] = $validator->validate($this->request->post)) === true) { $isSpam = $this->isSpam($this->request->post); //allow only fields that are in the validator list and remove the rest $userInfo = $validator->filter($this->request->post); $userInfo['display_name'] = $userInfo['username'] ?? ''; if (! isset($userInfo['username']) && isset($userInfo['first_name'])) { $userInfo['username'] = $userInfo['first_name'] . $userInfo['last_name']; } $userInfo['spam'] = $isSpam; list($userInfo) = Event :: trigger(__CLASS__, __FUNCTION__ , $userInfo); //plugins can also be used to detect spam and set the flag if ($userInfo['spam']) { $this->view->errors['login'] = __('Spam'); return; } if ($userInfo) { $result = User::add($userInfo); $this->view->errors['login'] = []; if ($result) { if (isset($result['user'])) { $message = __('User created!'); $this->session->set('success', ['login' => $message]); $this->view->success['login'][] = $message; $user_id = $result['user']; $this->request->request['user_id'] = $user_id; $site = siteSettings(); $siteData = Sites :: getSiteData(); $userInfo['website'] = url('user/index', [ 'host' => $siteData['host'] ?? false, 'scheme' => $_SERVER['REQUEST_SCHEME'] ?? 'http', ]); try { $error = __('Error sending account creation mail!'); if (! email([$userInfo['email'], $site['admin-email']], __('Your account has been created!'), 'user/signup', ['user' => $userInfo] + $this->global)) { $this->session->set('errors', ['login' => $error]); $this->view->errors[] = $error; } } catch (\Exception $e) { if (DEBUG) { $error .= "\n" . $e->getMessage(); } $this->session->set('errors', ['login' => $error]); $this->view->errors['login'] = $error; } return $this->redirect('user/login/index'); } else { $this->view->errors['login'] = ''; if ($result['email'] == $userInfo['email']) { $this->view->errors['login'] = __('This email is already in use. Please use another one.'); } if ($result['username'] == $userInfo['username']) { $this->view->errors['login'] .= __('This username is already in use. Please use another one.'); } } } else { $this->view->errors['login'] = __('Error creating account!'); } } } } function index() { if ($this->request->post) { $this->addUser(); } } }