1: <?php
2: class User
3: {
4: function __construct($libs=null)
5: {
6: if(!($libs instanceof Incluir)){
7: require_once('../core/incluir.php');
8: $nivel_dir = 3;
9: $this->libs = new Incluir($nivel_dir);
10: }else{
11: $this->libs = $libs;
12: }
13: $this->bd = $this->libs->incluir('db');
14: }
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: public function crear_usuario($args)
28: {
29: $respuesta = array();
30:
31: $this->libs->incluir_clase('includes/auth/Login.class.php');
32: $pass = Login::encriptar($args['password']);
33:
34: $query = 'call crearUsuario("'.$args['nombre'].'", "'.$args['apellido'].'", '.$args['id_genero'].', '.$args['id_escuela'].',"'.$args['username'].'", "'.$pass['string'].'", "'.$args['mail'].'", "'.$pass['key'].'")';
35: $stmt = $this->bd->ejecutar($query, true);
36: if($user = $this->bd->ejecutar_procedimiento($stmt)){
37: $respuesta['msj']= 'si';
38: if(is_array($args['extra'])){
39: $query_update = 'UPDATE usr_persona SET _id='.$user['_id'];
40: foreach ($args['extra'] as $key => $campo) {
41: $query_update .= ', '.$key.'="'.$campo.'"';
42: }
43: $query_update .= 'WHERE _id='.$user['_id'];
44: $stmt = $this->bd->ejecutar($query_update, true);
45: }
46: return $respuesta;
47: }
48: }
49:
50: public function validar_nombre($filtro, $tabla='user')
51: {
52: $query = "select _id from ".$tabla." where ".$filtro['campo']."='".$filtro['valor']."'";
53: $stmt = $this->bd->ejecutar($query);
54: if($this->bd->obtener_fila($stmt)){
55: return array('valid' => false);
56: }
57: else{
58: return array('valid' => true);
59: }
60: }
61:
62: 63: 64: 65: 66: 67:
68: public function abrir_usuario(Array $filtros, $campos = '*')
69: {
70: if(is_array($filtros)){
71: $condicion = ' where user._id>0 ';
72: foreach ($filtros as $key => $filtro) {
73: $condicion .= ' and '.$key.'="'.$filtro.'" ';
74: }
75: }
76: $query = "select ".$campos." from user
77: inner join usr_persona on usr_persona._id=user._id
78: ".$condicion;
79: $stmt = $this->bd->ejecutar($query, true);
80: if($usuario = $this->bd->obtener_fila($stmt)){
81: return $usuario;
82: }
83: else{
84: return false;
85: }
86: }
87:
88: 89: 90: 91: 92: 93: 94: 95:
96: public function editar_usuario($id, $campo, $valor, $tabla='user')
97: {
98: $respuesta = array('msj'=>'no');
99: $query = "UPDATE ".$tabla." SET ".$campo."='".$valor."' where _id=".$id;
100: if($this->bd->ejecutar($query)){
101: $respuesta['msj'] = 'si';
102: }
103: else{
104: $respuesta['error'] = 'El campo no se pudo modificar';
105: }
106: return $respuesta;
107: }
108:
109: 110: 111: 112: 113: 114: 115: 116:
117: public function cambiar_password($id_user, $old_pass, $new_pass)
118: {
119: $respuesta = array('msj'=>'no');
120: $this->libs->incluir_clase('includes/auth/Login.class.php');
121:
122: $query_salt = "select salt from user where _id=".$id_user;
123: $stmt_salt = $this->bd->ejecutar($query_salt);
124: $salt = $this->bd->obtener_fila($stmt_salt);
125: $password = Login::desencriptar($old_pass).$salt['salt'];
126: $password = hash('sha256', $password);
127:
128: $query_old = "select _id from user where _id=".$id_user." and password='".$password."' ";
129: $stmt_old = $this->bd->ejecutar($query_old);
130: $usuario = $this->bd->obtener_fila($stmt_old);
131: if(!empty($usuario)){
132: $nuevo = Login::encriptar(Login::desencriptar($new_pass));
133: $respuesta = $this->editar_usuario($id_user, 'password', $nuevo['string']);
134: $respuesta = $this->editar_usuario($id_user, 'salt', $nuevo['key']);
135: $respuesta['msj'] = 'si';
136: }
137: else{
138: $respuesta['error'] = 'La contraseña actual no coincide';
139: }
140: return $respuesta;
141: }
142:
143: 144: 145: 146: 147:
148: public function datos_password($mail)
149: {
150: $respuesta = array('msj'=>'no');
151: $arr_encriptado = array();
152: $this->libs->incluir_clase('includes/auth/Login.class.php');
153:
154: $usuario = $this->abrir_usuario(array('mail'=>$mail), 'username, mail, salt');
155:
156: if(!empty($usuario)){
157: $usuario['username'] = Login::esconder_string($usuario['username']);
158: }
159: return $usuario;
160: }
161:
162: public function nuevo_password($username, $new_pass, $salt)
163: {
164: $this->libs->incluir_clase('includes/auth/Login.class.php');
165: $respuesta = array('msj' => 'no');
166: $usuario = $this->abrir_usuario(array('username'=>$username, 'salt'=>$salt), 'user._id');
167: if(!empty($usuario)){
168: $nuevo = Login::encriptar(Login::desencriptar($new_pass));
169: $respuesta = $this->editar_usuario($usuario['_id'], 'password', $nuevo['string']);
170: $respuesta = $this->editar_usuario($usuario['_id'], 'salt', $nuevo['key']);
171: $respuesta = array('msj'=> 'si', 'username' => $username);
172: }
173: return $respuesta;
174: }
175: }
176: ?>