Guest User

Untitled

a guest
Jul 17th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.58 KB | None | 0 0
  1. CREATE TABLE usuarios(
  2. id INT NOT NULL UNIQUE AUTO_INCREMENT,
  3. tipo_de_usuario VARCHAR(100) NOT NULL,
  4. nombre VARCHAR(25) NOT NULL UNIQUE,
  5. email VARCHAR(255) NOT NULL UNIQUE,
  6. password VARCHAR(255) NOT NULL,
  7. fecha_registro DATETIME NOT NULL,
  8. activo TINYINT NOT NULL,
  9. PRIMARY KEY(id)
  10. );
  11.  
  12. class Usuario {
  13.  
  14. private $id;
  15. private $tipo_de_usuario;
  16. private $nombre;
  17. private $email;
  18. private $password;
  19. private $fecha_registro;
  20. private $activo;
  21.  
  22. public function __construct($id, $tipo_de_usuario, $nombre, $email, $password, $fecha_registro, $activo){
  23. $this -> id = $id;
  24. $this -> tipo_de_usuario = $tipo_de_usuario;
  25. $this -> nombre = $nombre;
  26. $this -> email = $email;
  27. $this -> password = $password;
  28. $this -> fecha_registro = $fecha_registro;
  29. $this -> activo = $activo;
  30. }
  31.  
  32. public function obtener_id(){
  33. return $this -> id;
  34. }
  35.  
  36. public function obtener_tipo_de_usuario(){
  37. return $this -> tipo_de_usuario;
  38. }
  39.  
  40. public function obtener_nombre(){
  41. return $this -> nombre;
  42. }
  43.  
  44. public function obtener_email(){
  45. return $this -> email;
  46. }
  47.  
  48. public function obtener_password(){
  49. return $this -> password;
  50. }
  51.  
  52. public function obtener_fecha_registro(){
  53. return $this -> fecha_registro;
  54. }
  55.  
  56. public function esta_activo(){
  57. return $this -> activo;
  58. }
  59.  
  60. public function cambiar_tipo_de_usuario($tipo_de_usuario){
  61. $this -> tipo_de_usuario = $tipo_de_usuario;
  62. }
  63.  
  64. public function cambiar_nombre($nombre){
  65. $this -> nombre = $nombre;
  66. }
  67.  
  68. public function cambiar_email($email){
  69. $this -> email = $email;
  70. }
  71.  
  72. public function cambiar_password($password){
  73. $this -> password = $password;
  74. }
  75.  
  76. public function cambiar_activo($activo){
  77. $this -> activo = $activo;
  78. }
  79. }
  80.  
  81. class RepositorioUsuario {
  82.  
  83. public static function obtener_todos($conexion) {
  84. $usuarios = array();
  85. if (isset($conexion)) {
  86.  
  87. try {
  88.  
  89. include_once 'usuarios.inc.php';
  90. $sql = "SELECT * FROM usuarios";
  91. $sentencia = $conexion->prepare($sql);
  92. $sentencia->execute();
  93. $resultado = $sentencia->fetchAll();
  94.  
  95. if (count($resultado)) {
  96. foreach ($resultado as $fila) {
  97. $usuarios[] = new Usuario(
  98. $fila['id'], $fila['tipo_de_usuario'], $fila['nombre'], $fila['email'], $fila['password'], $fila['fecha_registro'], $fila['activo']
  99. );
  100. }
  101. } else {
  102. print 'No hay usuarios';
  103. }
  104. } catch (PDOException $ex) {
  105. print 'ERROR' . $ex->GetMessage();
  106. }
  107. }
  108. return $usuarios;
  109. }
  110.  
  111. public static function obtener_numero_usuarios($conexion) {
  112. $total_usuarios = null;
  113. if (isset($conexion)) {
  114. try {
  115. $sql = "SELECT COUNT(*) as total FROM usuarios";
  116. $sentencia = $conexion->prepare($sql);
  117. $sentencia->execute();
  118. $resultado = $sentencia->fetch();
  119. $total_usuarios = $resultado['total'];
  120. } catch (PDOException $ex) {
  121. print 'ERROR: ' . $ex->GetMessage();
  122. }
  123. }
  124. return $total_usuarios;
  125. }
  126.  
  127. public static function insertar_usuario($conexion, $usuario) {
  128. $usuario_insertado = false;
  129.  
  130. if (isset($conexion)) {
  131. try {
  132. $sql = "INSERT INTO usuarios(tipo_de_usuario, nombre, email, password, fecha_registro, activo) VALUES(:tipo_de_usuario, :nombre, :email, :password, NOW(), 0)";
  133. $sentencia = $conexion->prepare($sql);
  134.  
  135. $tipo_de_usuario_temp = $usuario->obtener_tipo_de_usuario();
  136. $nombretemp = $usuario->obtener_nombre();
  137. $emailtemp = $usuario->obtener_email();
  138. $passwordtemp = $usuario->obtener_password();
  139.  
  140. $sentencia->bindParam(':tipo_de_usuario', $tipo_de_usuario_temp, PDO::PARAM_STR);
  141. $sentencia->bindParam(':nombre', $nombretemp, PDO::PARAM_STR);
  142. $sentencia->bindParam(':email', $emailtemp, PDO::PARAM_STR);
  143. $sentencia->bindParam(':password', $passwordtemp, PDO::PARAM_STR);
  144. $usuario_insertado = $sentencia->execute();
  145. } catch (PDOException $ex) {
  146. print 'ERROR: ' . $ex->GetMessage();
  147. }
  148. }
  149. return $usuario_insertado;
  150. }
  151.  
  152. public static function tipo_de_usuario_existe($conexion, $tipo_de_usuario){
  153. $tipo_de_usuario_existe=true;
  154. if(isset($conexion)){
  155. try{
  156. $sql="SELECT * FROM usuarios WHERE tipo_de_usuario= :tipo_de_usuario ";
  157.  
  158. $sentencia= $conexion-> prepare($sql);
  159.  
  160. $sentencia->bindParam(':tipo_de_usuario', $tipo_de_usuario, PDO::PARAM_STR);
  161.  
  162. $sentencia-> execute();
  163.  
  164. $resultado= $sentencia -> fetchAll();
  165.  
  166. if(count ($resultado)){
  167. $tipo_de_usuario_existe = true;
  168. }else{
  169. $tipo_de_usuario_existe = false;
  170. }
  171.  
  172. } catch (PDOException $ex) {
  173. print 'ERROR.' . $ex ->getMessage();
  174. }
  175. }
  176. return $tipo_de_usuario_existe;
  177. }
  178.  
  179. public static function nombre_existe($conexion, $nombre){
  180. $nombre_existe=true;
  181. if(isset($conexion)){
  182. try{
  183. $sql="SELECT * FROM usuarios WHERE nombre= :nombre ";
  184.  
  185. $sentencia= $conexion-> prepare($sql);
  186.  
  187. $sentencia->bindParam(':nombre', $nombre, PDO::PARAM_STR);
  188.  
  189. $sentencia-> execute();
  190.  
  191. $resultado= $sentencia -> fetchAll();
  192.  
  193. if(count ($resultado)){
  194. $nombre_existe = true;
  195. }else{
  196. $nombre_existe = false;
  197. }
  198.  
  199. } catch (PDOException $ex) {
  200. print 'ERROR.' . $ex ->getMessage();
  201. }
  202. }
  203. return $nombre_existe;
  204. }
  205. public static function email_existe($conexion, $email){
  206. $email_existe=true;
  207. if(isset($conexion)){
  208. try{
  209. $sql="SELECT * FROM usuarios WHERE email = :email ";
  210.  
  211. $sentencia= $conexion-> prepare($sql);
  212.  
  213. $sentencia->bindParam(':email', $email, PDO::PARAM_STR);
  214.  
  215. $sentencia-> execute();
  216.  
  217. $resultado= $sentencia -> fetchAll();
  218.  
  219. if(count ($resultado)){
  220. $email_existe = true;
  221. }else{
  222. $email_existe = false;
  223. }
  224.  
  225. } catch (PDOException $ex) {
  226. print 'ERROR.' . $ex ->getMessage();
  227. }
  228. }
  229. return $email_existe;
  230. }
  231.  
  232. public static function obtener_usuario_por_email($conexion, $email){
  233. $usuario = null;
  234.  
  235. if(isset($conexion)){
  236. try{
  237. include_once 'app/usuarios.inc.php';
  238. $sql="select * FROM usuarios WHERE email = :email";
  239. $sentencia = $conexion -> prepare($sql);
  240. $sentencia -> bindParam(':email', $email, PDO::PARAM_STR);
  241. $sentencia -> execute();
  242. $resultado = $sentencia ->fetch();
  243.  
  244. if(!empty($resultado)){
  245. $usuario = new Usuario($resultado['id'], $resultado['tipo_de_usuario'], $resultado['nombre'], $resultado['email'], $resultado['password'], $resultado['fecha_registro'], $resultado['activo']);
  246. }
  247. } catch (PDOException $ex) {
  248. print 'ERROR.' . $ex -> getMessage();
  249. }
  250. }
  251. return $usuario;
  252. }
  253.  
  254. public static function obtener_usuario_por_id($conexion, $id){
  255. $usuario = null;
  256.  
  257. if(isset($conexion)){
  258. try{
  259. include_once 'app/usuarios.inc.php';
  260. $sql="select * FROM usuarios WHERE id = :id";
  261. $sentencia = $conexion -> prepare($sql);
  262. $sentencia -> bindParam(':id', $id, PDO::PARAM_STR);
  263. $sentencia -> execute();
  264. $resultado = $sentencia ->fetch();
  265.  
  266. if(!empty($resultado)){
  267. $usuario = new Usuario($resultado['id'], $resultado['tipo_de_usuario'], $resultado['nombre'], $resultado['email'], $resultado['password'], $resultado['fecha_registro'], $resultado['activo']);
  268. }
  269. } catch (PDOException $ex) {
  270. print 'ERROR.' . $ex -> getMessage();
  271. }
  272. }
  273. return $usuario;
  274. }
  275. }
  276. ?>
  277.  
  278. class ValidadorRegistro {
  279. private $aviso_inicio;
  280. private $aviso_cierre;
  281.  
  282. private $tipo_de_usuario;
  283. private $nombre;
  284. private $email;
  285. private $clave;
  286.  
  287. private $error_nombre;
  288. private $error_email;
  289. private $error_clave1;
  290. private $error_clave2;
  291.  
  292. public function __construct($tipo_de_usuario, $nombre, $email, $clave1, $clave2, $conexion) {
  293. $this->aviso_inicio="<br><div class='alert alert-danger' role='alert'>";
  294. $this->aviso_cierre="</div>";
  295.  
  296. $this->tipo_de_usuario = "";
  297. $this->nombre = "";
  298. $this->email = "";
  299. $this->clave ="";
  300.  
  301. $this->error_tipo_de_usuario = $this->validar_tipo_de_usuario($tipo_de_usuario);
  302. $this->error_nombre = $this->validar_nombre($conexion, $nombre);
  303. $this->error_email = $this->validar_email($conexion, $email);
  304. $this->error_clave1 = $this->validar_clave1($clave1);
  305. $this->error_clave2 = $this->validar_clave2($clave1, $clave2);
  306.  
  307. if($this->error_clave1 === "" && $this->error_clave2 === ""){
  308. $this-> clave = $clave1;
  309. }
  310. }
  311.  
  312. private function variable_iniciada($variable) {
  313. if (isset($variable) && !empty($variable)) {
  314. return true;
  315. } else {
  316. return false;
  317. }
  318. }
  319.  
  320. private function validar_tipo_de_usuario($tipo_de_usuario) {
  321. if ($this->variable_iniciada($tipo_de_usuario)) {
  322. return "Debes seleccionar un tipo de usuario.";
  323. } else if ($this->variable_iniciada($tipo_de_usuario)== "Individuo (Persona Natural)"){
  324. $this-> tipo_de_usuario = $tipo_de_usuario;
  325. } else if ($this->variable_iniciada($tipo_de_usuario)== "Empresa o negocio (Persona Juridica)"){
  326. $this-> tipo_de_usuario = $tipo_de_usuario;
  327. }
  328. }
  329.  
  330. private function validar_nombre($conexion, $nombre) {
  331. if (!$this->variable_iniciada($nombre)) {
  332. return "Debes escribir un nombre de usuario.";
  333. } else {
  334. $this->nombre = $nombre;
  335. }
  336. if (strlen($nombre) < 6) {
  337. return "El nombre debe ser mas largo de 6 caracteres.";
  338. }
  339. if (strlen($nombre) > 30) {
  340. return "El nombre no debe contener mas de 30 caracteres.";
  341. }
  342. if(RepositorioUsuario :: nombre_existe($conexion, $nombre)){
  343. return "Este nombre de usuario ya esta en uso. Por favor, intenta con otro nombre.";
  344. }
  345. return "";
  346. }
  347.  
  348. private function validar_email($conexion, $email) {
  349. if (!$this->variable_iniciada($email)) {
  350. return "Debes proporcionar un email.";
  351. } else {
  352. $this->email = $email;
  353. }
  354. if(RepositorioUsuario :: email_existe($conexion, $email)){
  355. return "Este email ya esta en uso. Por favor, intenta con otro email o <a href='#'>intente recuperar su contraseña.</a>";
  356. }
  357. return "";
  358. }
  359.  
  360. private function validar_clave1($clave1) {
  361. if (!$this->variable_iniciada($clave1)){
  362. return "Debes proporcionar una contraseña.";
  363. }
  364. return "";
  365. }
  366.  
  367. private function validar_clave2($clave1, $clave2) {
  368. if (!$this->variable_iniciada($clave1)){
  369. return "Primero debes escribir en el campo anterior.";
  370. }
  371. if (!$this->variable_iniciada($clave2)){
  372. return "Debes repetir tu contraseña.";
  373. }
  374. if ($clave1 !== $clave2){
  375. return "Ambas contraseñas deben coincidir.";
  376. }
  377. return "";
  378. }
  379.  
  380. public function obtener_tipo_de_usuario(){
  381. return $this-> tipo_de_usuario;
  382. }
  383.  
  384. public function obtener_nombre(){
  385. return $this-> nombre;
  386. }
  387.  
  388. public function obtener_email(){
  389. return $this-> email;
  390. }
  391.  
  392. public function obtener_clave(){
  393. return $this-> clave;
  394. }
  395.  
  396. public function obtener_error_tipo_de_usuario(){
  397. return $this-> error_tipo_de_usuario;
  398. }
  399.  
  400. public function obtener_error_nombre(){
  401. return $this-> error_nombre;
  402. }
  403.  
  404. public function obtener_error_email(){
  405. return $this-> error_email;
  406. }
  407.  
  408. public function obtener_error_clave1(){
  409. return $this-> error_clave1;
  410. }
  411.  
  412. public function obtener_error_clave2(){
  413. return $this-> error_clave2;
  414. }
  415.  
  416. public function mostrar_tipo_de_usuario(){
  417. if($this->tipo_de_usuario!==""){
  418. echo 'value="'. $this -> tipo_de_usuario . '"';
  419. }
  420. }
  421.  
  422. public function mostrar_error_tipo_de_usuario(){
  423. if($this->error_tipo_de_usuario!==""){
  424. echo $this->aviso_inicio . $this -> error_tipo_de_usuario . $this->aviso_cierre;
  425. }
  426. }
  427.  
  428. public function mostrar_nombre(){
  429. if($this->nombre!==""){
  430. echo 'value="'. $this -> nombre . '"';
  431. }
  432. }
  433.  
  434. public function mostrar_error_nombre(){
  435. if($this->error_nombre!==""){
  436. echo $this->aviso_inicio . $this -> error_nombre . $this->aviso_cierre;
  437. }
  438. }
  439.  
  440. public function mostrar_email(){
  441. if($this->email!==""){
  442. echo 'value="'. $this -> email . '"';
  443. }
  444. }
  445.  
  446. public function mostrar_error_email(){
  447. if($this->error_email!==""){
  448. echo $this->aviso_inicio . $this -> error_email . $this->aviso_cierre;
  449. }
  450. }
  451.  
  452. public function mostrar_error_clave1(){
  453. if($this->error_clave1!==""){
  454. echo $this->aviso_inicio . $this -> error_clave1 . $this->aviso_cierre;
  455. }
  456. }
  457.  
  458. public function mostrar_error_clave2(){
  459. if($this->error_clave2!==""){
  460. echo $this->aviso_inicio . $this -> error_clave2 . $this->aviso_cierre;
  461. }
  462. }
  463.  
  464. public function registro_valido(){
  465. if($this-> error_tipo_de_usuario === "" && $this-> error_nombre === "" && $this-> error_email === "" && $this-> error_clave1 === "" && $this-> error_clave2 === ""){
  466. return true;
  467. }else{
  468. return false;
  469. }
  470. }
  471. }
  472.  
  473. if(ControlSesion :: sesion_iniciada()){
  474. Redireccion :: redirigir(SERVIDOR);
  475. }
  476.  
  477. if(isset($_POST['enviar'])){
  478. Conexion :: abrir_conexion();
  479. $validador = new ValidadorRegistro($_POST['tipo_de_usuario'], $_POST['nombre'], $_POST['email'], $_POST['clave1'], $_POST['clave2'], Conexion :: obtener_conexion());
  480. if($validador->registro_valido()){
  481. $usuario = new Usuario('', $validador-> obtener_tipo_de_usuario(), $validador-> obtener_nombre(), $validador-> obtener_email(), password_hash($validador-> obtener_clave(), PASSWORD_DEFAULT), '', '');
  482. $usuario_insertado = RepositorioUsuario :: insertar_usuario(Conexion :: obtener_conexion(), $usuario);
  483.  
  484. if($usuario_insertado){
  485. Redireccion :: redirigir(RUTA_REGISTRO_CORRECTO. '/' . $usuario -> obtener_nombre());
  486. }
  487. }
  488. Conexion :: cerrar_conexion();
  489. }
  490. $titulo = 'Registrarse';
  491.  
  492. <div class="container">
  493. <div class="jumbotron">
  494. <h1 class="text-center">Formulario de Registro</h1>
  495. </div>
  496. </div>
  497.  
  498. <div class="container">
  499. <div class="row">
  500. <div class="col-md-6 text-center">
  501. <div class="panel panel-default">
  502. <div class="panel-heading">
  503. <h3 class="panel-title">
  504. Instrucciones
  505. </h3>
  506. </div>
  507. <div class="panel-body">
  508. <br>
  509. <p class="text-justify">
  510. Para registrarte, introduce un nombre de usuario,
  511. tu email y una contraseña. El email que introduzcas debe ser real debido a que
  512. será necesario para que gestiones tu cuenta sin problemas. Te recomendamos que tu contraseña
  513. al igual que tu nombre de usuario sea alfanumerico, esto quiere decir que debe tener tanto
  514. letras minusculas y mayusculas, asi como numeros.
  515. </p>
  516. <br>
  517. <br>
  518. <a href="<?php echo RUTA_LOGIN ?>">¿Ya tienes tu cuenta?
  519. Inicia Sesion</a>
  520. <br>
  521. <br>
  522. <a href="#">¿Olvidaste tu contraseña?</a>
  523. <br>
  524. <br>
  525. </div>
  526. </div>
  527. </div>
  528. <div class="col-md-6 text-center">
  529. <div class="panel panel-default">
  530. <div class="panel-heading">
  531. <h3 class="panel-title">
  532. Introduce tus datos:
  533. </h3>
  534. </div>
  535. <div class="panel-body">
  536. <form role="form" method="post" action="<?php echo RUTA_REGISTRO?>">
  537. <?php
  538. if(isset($_POST['enviar'])){
  539. <form role="form" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
  540. <div class="form-group">
  541. <label>Tipo de Usuario: </label>
  542. <select name="tipo_de_usuario" class="form-control">
  543. <option>Seleccione... </option>
  544. <option value="Individuo (Persona Natural)">Individuo (Persona Natural)</option>
  545. <option value="Empresa o negocio (Persona Juridica)">Empresa o negocio (Persona Juridica)</option>
  546. </select>
  547. </div>
  548. <div class="form-group">
  549. <label>Nombre de Usuario: </label>
  550. <input type="text" class="form-control" name="nombre" placeholder="JuanitoPerez666">
  551. </div>
  552. <div class="form-group">
  553. <label>Email: </label>
  554. <input type="email" class="form-control" name="email" placeholder="juanitoperez_666@ejemplo.com">
  555. </div>
  556. <div class="form-group">
  557. <label>Contraseña: </label>
  558. <input type="password" class="form-control" name="clave1" placeholder="********">
  559. </div>
  560. <div class="form-group">
  561. <label>Repite tu contraseña: </label>
  562. <input type="password" class="form-control" name="clave2" placeholder="********">
  563. </div>
  564. <br>
  565. <button type="reset" class="btn btn-default">Limpiar casillas</button>
  566. <button type="submit" class="btn btn-default" name="enviar">Enviar Datos</button>
  567. </form>
  568. }else{
  569. <form role="form" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
  570. <div class="form-group">
  571. <label>Tipo de Usuario: </label>
  572. <select name="tipo_de_usuario" class="form-control">
  573. <option>Seleccione... </option>
  574. <option value="Individuo (Persona Natural)">Individuo (Persona Natural)</option>
  575. <option value="Empresa o negocio (Persona Juridica)">Empresa o negocio (Persona Juridica)</option>
  576. </select>
  577. </div>
  578. <?php
  579. $validador-> mostrar_error_tipo_de_usuario();
  580. ?>
  581. <div class="form-group">
  582. <label>Nombre de Usuario: </label>
  583. <input type="text" class="form-control" name="nombre" placeholder="JuanitoPerez666" <?php $validador -> mostrar_nombre() ?>>
  584. <?php
  585. $validador-> mostrar_error_nombre();
  586. ?>
  587. </div>
  588. <div class="form-group">
  589. <label>Email: </label>
  590. <input type="email" class="form-control" name="email" placeholder="juanitoperez_666@ejemplo.com" <?php $validador -> mostrar_email() ?>>
  591. <?php
  592. $validador-> mostrar_error_email();
  593. ?>
  594. </div>
  595. <div class="form-group">
  596. <label>Contraseña: </label>
  597. <input type="password" class="form-control" name="clave1" placeholder="********">
  598. <?php
  599. $validador-> mostrar_error_clave1();
  600. ?>
  601. </div>
  602. <div class="form-group">
  603. <label>Repite tu contraseña: </label>
  604. <input type="password" class="form-control" name="clave2" placeholder="********">
  605. <?php
  606. $validador-> mostrar_error_clave2();
  607. ?>
  608. </div>
  609. <br>
  610. <button type="reset" class="btn btn-default">Limpiar casillas</button>
  611. <button type="submit" class="btn btn-default" name="enviar">Enviar Datos</button>
  612. </form>
  613. }
  614. ?>
  615. </form>
  616. </div>
  617. </div>
  618. </div>
  619. </div>
  620. </div>
  621. <br>
  622. <br>
  623. <br>
Add Comment
Please, Sign In to add comment