예제 #1
0
파일: Register.php 프로젝트: visapi/amun
 public function onPost()
 {
     try {
         $name = $this->post->name('string', array(new Filter\Length(3, 32)), 'name', 'Name');
         $identity = $this->post->identity('string', array(new Filter\Length(3, 128), new Filter\Email()), 'email', 'Email');
         $pw = $this->post->pw('string');
         $pwRepeat = $this->post->pwRepeat('string');
         $longitude = $this->post->longitude('float');
         $latitude = $this->post->latitude('float');
         $captcha = $this->post->captcha('string');
         if (!$this->validate->hasError()) {
             // check whether registration is enabled
             if (!$this->registry['login.registration_enabled']) {
                 throw new Exception('Registration is disabled');
             }
             // compare pws
             if (strcmp($pw, $pwRepeat) != 0) {
                 throw new Exception('Password ist not the same');
             }
             // check captcha if anonymous
             $captchaProvider = Captcha::factory($this->config['amun_captcha']);
             if (!$captchaProvider->verify($captcha)) {
                 throw new Exception('Invalid captcha');
             }
             // create account record
             $handler = $this->getHandler('AmunService\\User\\Account');
             $account = $handler->getRecord();
             $account->setGroupId($this->registry['core.default_user_group']);
             $account->setStatus(Account\Record::NOT_ACTIVATED);
             $account->setIdentity($identity);
             $account->setName($name);
             $account->setPw($pw);
             $account->setLongitude($longitude);
             $account->setLatitude($latitude);
             $account = $handler->create($account);
             if (isset($account->id)) {
                 // send activation mail
                 $date = new DateTime('NOW', $this->registry['core.default_timezone']);
                 $values = array('account.name' => $account->name, 'account.identity' => $identity, 'host.name' => $this->base->getHost(), 'register.link' => $this->page->getUrl() . '/register/activate?token=' . $account->token, 'register.date' => $date->format($this->registry['core.format_date']));
                 $mail = new Mail($this->registry);
                 $mail->send('LOGIN_REGISTRATION', $identity, $values);
                 $this->template->assign('success', true);
             } else {
                 throw new Exception('Your account was added for approval');
             }
         } else {
             throw new Exception($this->validate->getLastError());
         }
     } catch (\Exception $e) {
         $this->template->assign('name', htmlspecialchars($name));
         $this->template->assign('identity', htmlspecialchars($identity));
         $this->template->assign('error', $e->getMessage());
     }
 }
예제 #2
0
파일: Captcha.php 프로젝트: visapi/amun
 public function onLoad()
 {
     parent::onLoad();
     try {
         $captcha = \Amun\Captcha::factory($this->config['amun_captcha']);
         $captcha->serve();
         exit;
     } catch (Exception $e) {
         header('Content-type: image/png');
         $im = imagecreatetruecolor(300, 57);
         $textcolor = imagecolorallocate($im, 0, 0, 0);
         $bgcolor = imagecolorallocate($im, 255, 255, 255);
         imagefill($im, 0, 0, $bgcolor);
         imagestring($im, 3, 4, 4, $e->getMessage(), $textcolor);
         imagepng($im);
         imagedestroy($im);
     }
 }
예제 #3
0
파일: Recover.php 프로젝트: visapi/amun
 public function onPost()
 {
     try {
         $email = $this->post->email('string', array(new Filter\Length(3, 64), new Filter\Email()));
         $captcha = $this->post->captcha('string');
         // check captcha if anonymous
         $captchaProvider = Captcha::factory($this->config['amun_captcha']);
         if (!$captchaProvider->verify($captcha)) {
             throw new Exception('Invalid captcha');
         }
         if (!$this->validate->hasError()) {
             $handler = $this->getHandler('AmunService\\User\\Account');
             $account = $handler->getOneByIdentity(sha1($this->config['amun_salt'] . $email), array('id', 'name', 'status', 'email'), Sql::FETCH_OBJECT);
             if ($account instanceof Account\Record) {
                 if (!in_array($account->status, array(Account\Record::NORMAL, Account\Record::ADMINISTRATOR))) {
                     throw new Exception('Account has an invalid status');
                 }
                 if (!empty($account->email)) {
                     $token = Security::generateToken();
                     $link = $this->page->getUrl() . '/login/resetPw?token=' . $token;
                     $date = new DateTime('NOW', $this->registry['core.default_timezone']);
                     // update status
                     $account->setStatus(Account\Record::RECOVER);
                     $account->setToken($token);
                     $handler->update($account);
                     // send mail
                     $values = array('account.name' => $account->name, 'host.name' => $this->base->getHost(), 'recover.ip' => $_SERVER['REMOTE_ADDR'], 'recover.link' => $this->page->getUrl() . '/resetPw?token=' . $token, 'recover.date' => $date->format($this->registry['core.format_date']));
                     $mail = new Mail($this->registry);
                     $mail->send('LOGIN_RECOVER', $account->email, $values);
                     $this->template->assign('success', true);
                 } else {
                     throw new Exception('No public email address is set for this account');
                 }
             } else {
                 throw new Exception('Account does not exist');
             }
         } else {
             throw new Exception($this->validate->getLastError());
         }
     } catch (\Exception $e) {
         $this->template->assign('error', $e->getMessage());
     }
 }
예제 #4
0
 protected function handleCaptcha(RecordAbstract $record)
 {
     if ($this->user->isAnonymous() || $this->user->hasInputExceeded()) {
         $captcha = Captcha::factory($this->config['amun_captcha']);
         if ($captcha->verify($record->captcha)) {
             $this->session->set('captcha_verified', time());
         } else {
             throw new Exception('Invalid captcha');
         }
     }
 }
예제 #5
0
파일: Index.php 프로젝트: visapi/amun
 public function onPost()
 {
     if ($this->post->register('string', array(), null, null, false)) {
         header('Location: ' . $this->page->getUrl() . '/register');
         exit;
     }
     $redirect = $this->getRedirect($this->post);
     $identity = $this->post->identity('string', array(new Account\Filter\Identity()));
     $pw = $this->post->pw('string', array(new Account\Filter\Pw(new Security($this->registry))));
     $captcha = $this->post->captcha('integer');
     try {
         if (empty($identity)) {
             throw new Exception('Invalid identity');
         }
         // check captcha if needed
         if ($this->level == Attempt::TRYING) {
             if (!Captcha::factory($this->config['amun_captcha'])->verify($captcha)) {
                 throw new Exception('Invalid captcha');
             }
         }
         // load handles
         $handles = array_map('trim', explode(',', $this->registry['login.provider']));
         foreach ($handles as $handler) {
             $handler = HandlerFactory::factory($handler, $this->container);
             if ($handler instanceof HandlerAbstract && $handler->isValid($identity)) {
                 $handler->setPageUrl($this->page->getUrl());
                 if ($handler->hasPassword() && empty($pw)) {
                     throw new Exception('Invalid password');
                 }
                 try {
                     if ($handler->handle($identity, $pw) === true) {
                         // clear attempts
                         if ($this->level != Attempt::NONE) {
                             $this->attempt->clear();
                         }
                         // redirect
                         $url = $redirect === false ? $this->config['psx_url'] : $redirect;
                         header('Location: ' . $url);
                         exit;
                         break;
                     }
                 } catch (InvalidPasswordException $e) {
                     // increase login attempt
                     $this->attempt->increase();
                     // if none assign captcha
                     if ($this->level == Attempt::NONE) {
                         $captcha = $this->config['psx_url'] . '/' . $this->config['psx_dispatch'] . 'api/core/captcha';
                         $this->template->assign('captcha', $captcha);
                     }
                 }
             }
         }
         throw new Exception('Authentication failed');
     } catch (\Exception $e) {
         $this->template->assign('error', $e->getMessage());
     }
 }