PageRenderTime 27ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/app/frontendModule/presenters/registrationPresenter.php

https://github.com/Buri/Aragorn.cz
PHP | 247 lines | 199 code | 30 blank | 18 comment | 21 complexity | b2fa3a7219883abdc6d67a45c20b502f MD5 | raw file
  1. <?php
  2. /*
  3. * TODO:
  4. * predavani parametru v url
  5. * .
  6. *
  7. */
  8. namespace frontendModule{
  9. use Nette\Environment;
  10. use Nette\Application\UI\Form;
  11. use \DB;
  12. class registrationPresenter extends \BasePresenter {
  13. /**
  14. *
  15. * @var array
  16. */
  17. protected $config;
  18. public function startup(){
  19. $this->template->banned = false;
  20. $this->template->reg = $this->getContext()->parameters['registration'];
  21. $this->config = $this->getContext()->parameters;
  22. $bans = $this->context->database->bans('ip LIKE ?', '%'.$_SERVER['REMOTE_ADDR'].'%')->where('expires > ?', time());
  23. if(count($bans) > 0){
  24. $this->template->banned = true;
  25. }else{
  26. $hosts = file(CFG_DIR . '/blacklist/host');
  27. $ip = $_SERVER['REMOTE_ADDR'];
  28. $hostname = gethostbyaddr($ip);
  29. foreach($hosts as $host){
  30. if(preg_match("/$host/i", $ip) > 0 || preg_match("/$host/i", $hostname) > 0){
  31. $this->template->banned = true;
  32. break;
  33. }
  34. }
  35. }
  36. parent::startup();
  37. }
  38. public function createComponentRegisterForm() {
  39. $db = $this->context->database;
  40. $form = new Form;
  41. #$form->addProtection('Cross Site Request Forgery!');
  42. $form->addText('username', 'Přezdívka: ')
  43. ->addRule(Form::FILLED, 'Musíte vyplnit uživatelské jméno!')
  44. ->addRule(function (\Nette\Forms\Controls\TextInput $user) use($db){
  45. if(count($db->users()->where("username LIKE ? OR urlfragment = ?", array($user->getValue(), \Utilities::string2url($user->getValue()))))){
  46. return false;
  47. }
  48. return true;
  49. }, 'Uživatelské jméno je již obsazené.');
  50. $form->addPassword('password', 'Heslo')
  51. ->addRule(Form::FILLED, 'Musíte vyplnit heslo.')
  52. ->addRule(Form::MIN_LENGTH, 'Heslo je příliš krátké.', 6);
  53. $form->addText('mail', 'E-mail')
  54. ->addRule(Form::EMAIL, 'Email není validní!')
  55. ->addRule(function(\Nette\Forms\Controls\TextInput $mail) use ($db) {
  56. if(count($db->users()->where("mail LIKE ?", $mail->getValue()))){
  57. return false;
  58. }
  59. return true;
  60. }, 'Emailová adresa je již obsazená.')
  61. ->addRule(function(\Nette\Forms\Controls\TextInput $mail){
  62. $val = $mail->getValue();
  63. $blacklist = file(CFG_DIR . '/blacklist/mail');
  64. foreach($blacklist as $entry){
  65. $entry = trim($entry);
  66. //dump("/$entry/i ~ $val ? " . (preg_match("/$entry/i", $val) ? 'yes' : 'no'));
  67. if(preg_match("/$entry/i", $val)){
  68. return false;
  69. }
  70. }
  71. return true;
  72. }, "Vaše mailová adresa je na blacklistu.");
  73. /*$form->addCheckbox('eula', 'Souhlasím s podmínkami.')
  74. ->addRule(Form::FILLED, 'Musíte souhlasit.');*/
  75. $form->addText('spambot', 'Toto musíte vyplnit.')
  76. ->addRule(Form::LENGTH, 'Špatně.', 0)
  77. ->getControlPrototype()->class("hidden");
  78. $form['spambot']->getLabelPrototype()->class("hidden");
  79. $form->addGroup('Povinné údaje')->add($form['username'],$form['password'],$form['mail'],/*$form['eula'],*/$form['spambot']);
  80. $form->addSubmit('save', 'Souhlasím s podmínkami, registrovat');
  81. $form->onSuccess[] = callback($this, 'processRegisterForm');
  82. $form->setAction($this->link("register"));
  83. return $form;
  84. }
  85. public function processRegisterForm(\Nette\Application\UI\Form $form) {
  86. if($this->template->banned) return false;
  87. $data = $form->getValues();
  88. $data["token"] = md5(uniqid());
  89. $data["create_time"] = time();
  90. $data["password"] = sha1($data["password"]);
  91. unset($data['eula']);
  92. unset($data['spambot']);
  93. if(!$this->context->database->registration()->where("token = ?", $data["token"])->count()){
  94. $this->context->database->registration()->insert($data);
  95. }
  96. $this->getTemplate()->mail = $data["mail"];
  97. $this->redirect(301, "mail", serialize(array("mail"=> $data["mail"], "token" => $data["token"])));
  98. return true;
  99. }
  100. /**
  101. *
  102. * @param string $data
  103. */
  104. public function actionMail($data){
  105. $data = unserialize($data);
  106. $l = $this->link("//finish", $data["token"]);
  107. $template = new \Nette\Templating\FileTemplate(__DIR__. '/../templates/registration/mail/confirm-mail.latte');
  108. $template->registerFilter(new \Nette\Latte\Engine);
  109. $template->link =$l;
  110. $mail = new \Nette\Mail\Message;
  111. $mail->setFrom($this->config['registration']['mail']);
  112. $mail->addTo($data['mail']);
  113. $mail->setHtmlBody($template);
  114. $mail->send();
  115. $this->template->mail = $data['mail'];
  116. }
  117. public function actionFinish( $id ){
  118. $db = $this->context->database;
  119. if($this->template->banned) return false;
  120. $this->getTemplate()->message = "";
  121. $reg = $db->registration()->where("token = ?", $id);
  122. if(!count($reg)){
  123. $this->getTemplate()->message = "Registrace nebyla nalezena. Nevypršela už platnost odkazu?";
  124. }else{
  125. foreach($reg as $r){
  126. $row = $r;
  127. break;
  128. }
  129. if($this->context->database->users()->where("username LIKE ?", $row["username"])->count() || DB::users()->where("mail = ?", $row["mail"])->count()){
  130. $this->getTemplate()->message = "Uživatelské jméno/mail je již obsazen. Je nám líto.";
  131. $reg->delete();
  132. return false;
  133. }
  134. $r = $db->users()->insert(array(
  135. "id"=>0,
  136. "username" => $row["username"],
  137. "password" => $row["password"],
  138. "mail" => $row["mail"],
  139. "created" => $row["create_time"],
  140. "login"=>0,
  141. 'urlfragment'=> \Utilities::string2url($row['username'])
  142. ));
  143. $db->users_prerferences()->insert(array(
  144. "id"=> $r['id'],
  145. "color" => "#fff"
  146. ));
  147. $reg->delete(); /* Vloží se pouze jednou, ale smažou se všechny odpovídající tokeny */
  148. $this->getTemplate()->message = "Registrace byla dokončena. Nyní se můžete přihlásit.";
  149. }
  150. }
  151. public function actionRecoverpassword(){
  152. }
  153. public function createComponentRecoverPassword(){
  154. $form = new Form;
  155. $form->addText('mail', '')
  156. ->addRule(Form::FILLED, 'Je nutné vyplnit e-mail.')
  157. ->addRule(Form::EMAIL, 'Zadaný e-mail není platný');
  158. $form->addSubmit('send', 'Obnovit heslo');
  159. $form->onSuccess[] = callback($this, 'handleRecoverPassword');
  160. return $form;
  161. }
  162. protected function generatePassword($length=9, $strength=0) {
  163. $vowels = 'aeuy';
  164. $consonants = 'bdghjmnpqrstvz';
  165. if ($strength & 1) {
  166. $consonants .= 'BDGHJLMNPQRSTVWXZ';
  167. }
  168. if ($strength & 2) {
  169. $vowels .= "AEUY";
  170. }
  171. if ($strength & 4) {
  172. $consonants .= '23456789';
  173. }
  174. if ($strength & 8) {
  175. $consonants .= '@#$%';
  176. }
  177. $password = '';
  178. $alt = time() % 2;
  179. for ($i = 0; $i < $length; $i++) {
  180. if ($alt == 1) {
  181. $password .= $consonants[(rand() % strlen($consonants))];
  182. $alt = 0;
  183. } else {
  184. $password .= $vowels[(rand() % strlen($vowels))];
  185. $alt = 1;
  186. }
  187. }
  188. return $password;
  189. }
  190. public function handleRecoverPassword(Form $form){
  191. $vals = $form->getValues();
  192. $db = $this->context->database;
  193. $row = $db->users('mail', $vals->mail);
  194. if($row->count()){
  195. $row = $row->fetch();
  196. $user = $row['username'];
  197. $password = $this->generatePassword(9, 1 | 2 | 4);
  198. $mail = new \Nette\Mail\Message();
  199. $mail->addTo($vals->mail);
  200. $mail->setFrom('system@' . $this->context->parameters['servers']['domain']);
  201. $template = new \Nette\Templating\FileTemplate(__DIR__. '/../templates/registration/mail/recover-password.latte');
  202. $template->registerFilter(new \Nette\Latte\Engine);
  203. $template->username = $user;
  204. $template->password = $password;
  205. $mail->setHtmlBody($template);
  206. $mail->send();
  207. $row->update(array(
  208. "password" => sha1($password)
  209. ));
  210. $this->flashMessage('Na váš e-mail bylo odesláno nové heslo.');
  211. $this->redirect('this');
  212. }else{
  213. $form->addError('Zadaný mail nebyl nalezen v databázi.');
  214. }
  215. }
  216. }
  217. }