Esempio n. 1
0
 /**
  * User registration page
  */
 public function registration(Request $request, $matches)
 {
     $this->guest_only();
     $data = [];
     if ($request->isMethod('post')) {
         $errors = [];
         $captcha = $request->get('code');
         $data = ['username' => $request->get('username'), 'password' => $request->get('password'), 'email' => $request->get('email'), 'group_id' => $this->settings->get('users_group', \Group::first()->id)];
         // Create user instance
         $user = new User($data);
         // Check captcha
         if (!NCModuleCore::verify_captcha($captcha)) {
             $errors[] = $this->lang->translate('user.auth.code_wrong');
         }
         // Validate password
         if (strlen($user->password) < 6) {
             $errors[] = $this->lang->translate('user.edit.short_password');
         }
         // Validate username
         if (strlen($user->username) < 4) {
             $errors[] = $this->lang->translate('user.edit.short_username');
         }
         if (User::count(['conditions' => ['username = ?', $user->username]]) > 0) {
             $errors[] = $this->lang->translate('user.edit.exists', $user->username);
         }
         // Validate email
         if (strlen($user->email) < 5 || strpos($user->email, '@') < 1) {
             $errors[] = $this->lang->translate('user.edit.wrong_email', $user->email);
         } elseif (User::count(['conditions' => ['email = ?', $user->email]]) > 0) {
             $errors[] = $this->lang->translate('user.edit.exists_email', $user->email);
         }
         if ($errors) {
             $this->view->assign('errors', $errors);
         } else {
             if (!$user->save(true)) {
                 $this->view->twig->addGlobal('errors', [$this->lang->translate('form.failed')]);
             } else {
                 $autenticated = $this->auth->authenticate($data['username'], $data['password']);
                 if ($autenticated) {
                     $this->auth->login($autenticated);
                     return static::redirect_response('/');
                 }
             }
         }
     }
     return $this->view->twig->render('user/registration.twig', ['title' => $this->lang->translate('user.registration.title'), 'data' => $data]);
 }
Esempio n. 2
0
 public function login(Request $request)
 {
     if ($request->isMethod('POST')) {
         if (!NCModuleCore::verify_captcha($request->get('captcha'))) {
             return static::json_response(['error' => 'failed']);
         }
         /** @var Auth $service */
         $service = NCService::load('User.Auth');
         $user = $service->authenticate($request->get('username'), $request->get('password'));
         if ($user && $user->can('access')) {
             $service->login($user);
             return static::json_response(['status' => 'ok']);
         } else {
             return static::json_response(['error' => 'failed']);
         }
     }
     return $this->view->render('users/login.twig', ['title' => $this->lang->translate('user.auth.title')]);
 }
Esempio n. 3
0
 /**
  * @param $url
  * @param $theme
  * @param $namespace
  */
 public function __construct($url, $theme = 'default', $namespace = '')
 {
     // Authentication
     /** @var Auth auth */
     $this->auth = NCService::load('User.Auth');
     /** @var \User user */
     $this->user = $this->auth->identify(Env::$request->cookies->get('sess'));
     // Settings
     /** @var Settings settings */
     $this->settings = NCService::load('Application.Settings');
     // Analytics module
     if ($this->analytics) {
         $counter = NCService::load('Application.Analytics', [$this->user ? $this->user->id : null]);
         $counter->save();
     }
     // Renderring
     /** @var Theme view */
     $this->view = NCModuleCore::load_view($theme);
     // Translation
     /** @var Translate lang */
     $this->lang = NCModuleCore::load_lang();
     // Module level routing
     /** @var NCRouter map */
     $this->map = new NCRouter($this, $namespace);
     // Adding sitemap to urls
     $this->map->addRoute('sitemap.xml', [$this, 'sitemap'], 'sitemap');
     // Register reverse url filter
     $this->view->twig->addFilter(new \Twig_SimpleFilter('url', [$this->map, 'reverse_filter']));
     // Register translate filters
     $this->view->twig->addFilter(new \Twig_SimpleFilter('lang', [$this->lang, 'translate']));
     $this->view->twig->addFilter(new \Twig_SimpleFilter('dlang', [$this->lang, 'translate_date']));
     // Assign user
     $this->view->assign('user', $this->user ? $this->user->to_array() : []);
     $this->view->assign('group', $this->user ? $this->user->group : []);
     // Assign captcha URL
     $this->view->assign('secure_image', NCModuleCore::$captcha_url);
     // Loading modules globals
     $this->view->load_globals($this, $this->lang);
     // Disable access to banned users
     if ($this->user->ban_time > time() || $this->user->ban_time == -1) {
         Env::$response->setContent($this->errorBanned(Env::$request, $this->user->ban_reason));
         Env::$response->send();
         return;
     }
     // Check access to current module
     if (!$this->access()) {
         Env::$response->setContent($this->error403(Env::$request));
         Env::$response->send();
         return;
     }
     // Build current module map
     $this->route();
     /** @var NCRoute $route */
     $route = $this->map->match($url);
     // Check route
     if (!is_callable($route->callback)) {
         Env::$response->setContent($this->error404(Env::$request));
         Env::$response->send();
         return;
     }
     // Bufferization content
     ob_start();
     $this->configure();
     if (strpos($url, 'sitemap.xml') > -1) {
         Env::$response->headers->set('Content-Type', 'application/xml');
         $response = call_user_func($route->callback, new NCSitemapBuilder(), $this->map);
         $response = strval($response);
     } else {
         $response = call_user_func($route->callback, Env::$request, $route->matches);
     }
     $buffer = ob_get_clean();
     Env::$response->setContent(!is_null($response) ? $response : $buffer);
     // Flush content
     Env::$response->send();
 }