PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/component/models/register.php

https://gitlab.com/lazypdf/afi-framework
PHP | 268 lines | 196 code | 28 blank | 44 comment | 64 complexity | 9da15a6218584008e6a02fa2e8d750c4 MD5 | raw file
  1. <?php
  2. /**
  3. * @version 1.0.0 Afi Framework $
  4. * @package Afi Framework
  5. * @copyright Copyright © 2014 - All rights reserved.
  6. * @license GNU/GPL
  7. * @author kim
  8. * @author mail kim@afi.cat
  9. * @website http://www.afi.cat
  10. *
  11. */
  12. defined('_Afi') or die ('restricted access');
  13. include('includes/model.php');
  14. class register extends model
  15. {
  16. /**
  17. * Method to check if username exists
  18. */
  19. function checkUsername()
  20. {
  21. if(isset($_GET['task']) && $_GET['task'] == 'checkUsername') {
  22. $db = factory::getDatabase();
  23. $username = $_GET['username'];
  24. $db->query('select id from #_users where username = '.$db->quote($username));
  25. if($id = $db->loadResult()) {
  26. echo false;
  27. } else {
  28. echo true;
  29. }
  30. }
  31. }
  32. /**
  33. * Method to check if email exists
  34. */
  35. function checkEmail()
  36. {
  37. if(isset($_GET['task']) && $_GET['task'] == 'checkEmail') {
  38. $db = factory::getDatabase();
  39. $email = $_GET['email'];
  40. $db->query('select id from #_users where email = '.$db->quote($email));
  41. if($id = $db->loadResult()) {
  42. echo false;
  43. } else {
  44. echo true;
  45. }
  46. }
  47. }
  48. /**
  49. * Method to register a new user
  50. */
  51. function register()
  52. {
  53. if(isset($_GET['task']) && $_GET['task'] == 'register') {
  54. $config = factory::getConfig();
  55. $app = factory::getApplication();
  56. $db = factory::getDatabase();
  57. $user = factory::getUser();
  58. $lang = factory::getLanguage();
  59. //si un campo esta vacio abortamos...
  60. if($_POST['username'] == "" || $_POST['email'] == "" || $_POST['password'] == "" || $_POST['password2'] == "") {
  61. $app->setMessage($lang->get('Rellena todos los campos por favor'), 'danger');
  62. $app->redirect($config->site.'/index.php?view=register');
  63. return false;
  64. }
  65. //check if username exists...
  66. $db->query('select id from #_users where username = '.$db->quote($_POST['username']));
  67. if($id = $db->loadResult()) {
  68. $app->setMessage($lang->get('El nombre '.$_POST['username'].' ya existe, por favor elige otro'), 'danger');
  69. $app->redirect($config->site.'/index.php?view=register');
  70. return false;
  71. }
  72. //check if email exists...
  73. $db->query('select id from #_users where email = '.$db->quote($_POST['email']));
  74. if($id = $db->loadResult()) {
  75. $app->setMessage($lang->get('El email ya existe, por favor elige otro'), 'danger');
  76. $app->redirect($config->site.'/index.php?view=register');
  77. return false;
  78. }
  79. $app->getToken($_POST['auth_token'], $config->token_time);
  80. if($_POST['password'] === $_POST['password2']) {
  81. unset($_POST['password2']);
  82. unset($_POST['auth_token']);
  83. $_POST['password'] = $app->encryptPassword($_POST['username'], $_POST['password']);
  84. $_POST['registerDate'] = date('Y-m-d H:i:s');
  85. $token = uniqid();
  86. $_POST['token'] = $token;
  87. $_POST['language'] = 'en-gb';
  88. $result = $db->insertRow('#_users', $_POST);
  89. if($result) {
  90. //send a confirmation to the user...
  91. $subject = $lang->replace('CW_REGISTER_WELCOME_SUBJECT', $config->sitename);
  92. $link = $config->site.'/index.php?view=register&task=validate&token='.$token;
  93. $body = $lang->replace('CW_REGISTER_WELCOME_BODY', $_POST['username'], $config->sitename, $link);
  94. $send = $this->sendMail($_POST['email'], $_POST['username'], $subject, $body);
  95. if($send) {
  96. $app->setMessage($lang->replace('CW_REGISTER_SUCCESS_MSG', $config->sitename), 'success');
  97. $app->redirect($config->site.'/index.php');
  98. exit(0);
  99. } else {
  100. //mostrar el link de activacion en el mensaje ya que fallo el email...
  101. $app->setMessage($lang->replace('CW_REGISTER_EMAIL_ERROR_MSG', $link), 'danger');
  102. $app->redirect($config->site.'/index.php?view=register');
  103. return true;
  104. }
  105. } else {
  106. $app->setMessage($lang->get('CW_REGISTER_ERROR_MSG'), 'danger');
  107. $app->redirect($config->site.'/index.php?view=register');
  108. return false;
  109. }
  110. } else {
  111. $app->setMessage($lang->get('CW_REGISTER_PASSWORDS_NOT_MATCH_MSG'), 'danger');
  112. $app->redirect($config->site.'/index.php?view=register');
  113. return false;
  114. }
  115. }
  116. }
  117. /**
  118. * Method to reset the user password
  119. */
  120. function reset()
  121. {
  122. if(isset($_GET['task']) && $_GET['task'] == 'reset') {
  123. $config = factory::getConfig();
  124. $app = factory::getApplication();
  125. $db = factory::getDatabase();
  126. $user = factory::getUser();
  127. $lang = factory::getLanguage();
  128. //si un campo esta vacio abortamos...
  129. if($_POST['username'] == "" || $_POST['email'] == "") {
  130. $app->setMessage($lang->get('Rellena todos los campos por favor'), 'danger');
  131. $app->redirect($config->site.'/index.php?view=register&layout=reset');
  132. return false;
  133. }
  134. $username = $db->quote($_POST['username']);
  135. $email = $db->quote($_POST['email']);
  136. $secret = $db->quote($_POST['secret']);
  137. $db->query("SELECT id FROM #_users WHERE email = $email AND username = $username AND token = $secret AND block = 0");
  138. $id = $db->loadResult();
  139. $newpassword = uniqid();
  140. $password = $app->encryptPassword($_POST['username'], $newpassword);
  141. $result = $db->updateField('#_users', 'password', $password, 'id', $id);
  142. //send email to user...
  143. if($result) {
  144. //send a confirmation to the user...
  145. $subject = $lang->replace('CW_REGISTER_RESET_SUBJECT', $config->sitename);
  146. $body = $lang->replace('CW_REGISTER_RESET_BODY', $_POST['username'], $config->sitename, $newpassword);
  147. $send = $this->sendMail($_POST['email'], $_POST['username'], $subject, $body);
  148. if($send) {
  149. $app->setMessage($lang->get('CW_REGISTER_RESET_SUCCESS_MSG'), 'success');
  150. $app->redirect($config->site.'/view=register');
  151. } else {
  152. $app->setMessage($lang->get('CW_REGISTER_RESET_ERROR_MSG'), 'danger');
  153. }
  154. } else {
  155. $app->setMessage($lang->get('CW_REGISTER_RESET_ERROR_MSG'), 'danger');
  156. }
  157. }
  158. }
  159. /**
  160. * Method to login into the application
  161. */
  162. function login()
  163. {
  164. if(isset($_GET['task']) && $_GET['task'] == 'login') {
  165. $config = factory::getConfig();
  166. $app = factory::getApplication();
  167. $db = factory::getDatabase();
  168. $user = factory::getUser();
  169. $lang = factory::getLanguage();
  170. if($_SERVER["REQUEST_METHOD"] == "POST")
  171. {
  172. //si un campo esta vacio abortamos...
  173. if($_POST['username'] == "" || $_POST['password'] == "") {
  174. $app->setMessage($lang->get('Rellena todos los campos por favor'), 'danger');
  175. $app->redirect($config->site.'/index.php?view=register');
  176. return false;
  177. }
  178. //check token
  179. $app->getToken($_POST['auth_token'], $config->token_time);
  180. $username = $db->quote($_POST['username']);
  181. $password = $db->quote($app->encryptPassword($_POST['username'], $_POST['password']));
  182. $remember = "";
  183. $db->query("SELECT id FROM #_users WHERE username = $username AND password = $password AND block = 0");
  184. if($id = $db->loadResult()) {
  185. $user->setAuth($id);
  186. //if remember set cookie...
  187. if($_POST['remember'] == 1) {
  188. $user->setCookie();
  189. //$remember = "&remember=1";
  190. }
  191. $db->updateField('#_users', 'lastvisitDate', $_POST['lastvisitDate'], 'id', $id);
  192. $app->setMessage($lang->replace('CW_LOGIN_SUCCESS_MSG', $_POST['username']), 'success');
  193. $link = $config->site.'/index.php?view=wishlist&user='.$id.'&username='.$user->username;
  194. } else {
  195. $app->setMessage($lang->get('CW_LOGIN_ERROR_MSG'), 'danger');
  196. $link = $config->site.'/index.php?view=register';
  197. }
  198. $app->redirect($link);
  199. }
  200. }
  201. }
  202. /**
  203. * Method to logout the application
  204. */
  205. function logout()
  206. {
  207. if(isset($_GET['task']) && $_GET['task'] == 'logout') {
  208. $config = factory::getConfig();
  209. $app = factory::getApplication();
  210. unset($_SESSION['cw_userid']);
  211. $app->redirect($config->site);
  212. }
  213. }
  214. /**
  215. * Method to validate user for the first time into the application after a successful registration
  216. */
  217. function validate()
  218. {
  219. if(isset($_GET['task']) && $_GET['task'] == 'validate') {
  220. $config = factory::getConfig();
  221. $app = factory::getApplication();
  222. $db = factory::getDatabase();
  223. $user = factory::getUser();
  224. $lang = factory::getLanguage();
  225. //if token...
  226. if(isset($_GET['token'])) {
  227. $result = $db->updateField('#_users', 'block', 0, 'token', $_GET['token']);
  228. if($result) {
  229. if($config->admin_mails == 1) {
  230. $this->sendAdminMail('Nuevo registro en Deziro', "Un nuevo usuario se ha registrado en Deziro.");
  231. }
  232. $app->setMessage($lang->replace('CW_REGISTER_WELCOME_MSG_SUCCESS', $config->sitename), 'success');
  233. } else {
  234. $app->setMessage($lang->get('CW_REGISTER_WELCOME_MSG_ERROR'), 'danger');
  235. }
  236. $app->redirect($config->site.'/index.php');
  237. }
  238. }
  239. }
  240. }