PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/RAcceso.php

https://gitlab.com/pedroperafan18/ikel
PHP | 212 lines | 191 code | 19 blank | 2 comment | 48 complexity | 093aeb5db6dfc74c5b6c64f593807a21 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Acceso extends CI_Controller {
  3. public function __construct()
  4. {
  5. parent::__construct();
  6. $this->load->library(array('session','form_validation','user_agent'));
  7. $this->load->helper('url', 'security');
  8. $this->load->model('macceso');
  9. }
  10. public function index()
  11. {
  12. if($this->session->userdata('identificador') && $this->session->userdata('perfil')){
  13. $perfil = $this->session->userdata('perfil');
  14. if ($perfil == 1) {
  15. $perfil = "Administrador";
  16. }elseif ($perfil == 2 || $perfil == 4) {
  17. $perfil = "Maestro";
  18. }elseif ($perfil == 3) {
  19. $perfil = "Usuario";
  20. }
  21. redirect($perfil."/dashboard","refresh");
  22. }else{
  23. redirect("acceso/login","refresh");
  24. }
  25. }
  26. function logData(){
  27. date_default_timezone_set("America/Mexico_City");
  28. $times = $this->CI->db->query_times;
  29. foreach ($this->CI->db->queries as $key => $query) {
  30. if (!preg_match('/SELECT/', $query)) {
  31. $query = str_replace("INTO ", "", $query);
  32. $sql = explode(' ',$query);
  33. $data["IDUsuario"] = $this->session->userdata('identificador');
  34. $data["Accion"] = $sql[0];
  35. $data["Tabla"] = str_replace("`", "", $sql[1]);
  36. $data["ExTime"] = $times[$key];
  37. $data["IP"] = $this->input->ip_address();
  38. $patron = "/([0-9]+)/";
  39. if (preg_match($patron, $query,$id)) {
  40. $data["IDElemento"] = $id[1];
  41. }else{
  42. $data["IDElemento"] = 0;
  43. }
  44. if ($this->agent->is_browser())
  45. {
  46. $data["Navegador"] = $this->agent->browser().' '.$this->agent->version();
  47. }
  48. elseif ($this->agent->is_robot())
  49. {
  50. $data["Navegador"] = $this->agent->robot();
  51. }
  52. elseif ($this->agent->is_mobile())
  53. {
  54. $data["Navegador"] = $this->agent->mobile();
  55. }
  56. else
  57. {
  58. $data["Navegador"] = 'OS sin identifiar';
  59. }
  60. $data["SistemaOperativo"] = $this->agent->platform();
  61. $data['Fecha'] = date('Y-m-d H:i:s');
  62. $this->macceso->setLog($data);
  63. }
  64. }
  65. }
  66. public function login(){
  67. if($this->session->userdata('identificador') && $this->session->userdata('perfil')){
  68. $perfil = $this->session->userdata('perfil');
  69. if ($perfil == 1) {
  70. $perfil = "Administrador";
  71. }elseif ($perfil == 2 || $perfil == 4) {
  72. $perfil = "Maestro";
  73. }elseif ($perfil == 3) {
  74. $perfil = "Usuario";
  75. }
  76. redirect($perfil."/dashboard","refresh");
  77. }else{
  78. if($this->input->is_ajax_request()!=FALSE){
  79. $data = ($this->input->post()==NULL)? $this->input->get() : $this->input->post();
  80. if($this->input->post()!=NULL){
  81. foreach ($data as $key => $value) {
  82. $data["$key"] = $this->security->xss_clean($value);
  83. }
  84. $this->form_validation->set_rules('Correo', 'Correo electrónico', 'trim|required|valid_email');
  85. $this->form_validation->set_rules('Password', 'Contraseña', 'trim|required|min_length[8]|max_length[16]');
  86. if ($this->form_validation->run() == FALSE) {
  87. $errors = $this->form_validation->error_array();
  88. $array = array("result" => FALSE,"error" => $errors);
  89. }else {
  90. $data["Password"] = sha1(md5($data["Password"]));
  91. $result = $this->macceso->login($data);
  92. if($result!=FALSE){
  93. $id = $result["0"]["ID"];
  94. $perfil = $result["0"]["Tipo"];
  95. $array = array(
  96. 'identificador' => $id,
  97. 'perfil' => $perfil
  98. );
  99. $this->session->set_userdata($array);
  100. $array = array('result' => TRUE);
  101. }else{
  102. $array = array('result' => FALSE, 'error' => 'Usuario / Contraseña incorrecto(s)');
  103. }
  104. }
  105. }
  106. $this->output->set_content_type("application/json")->set_output(json_encode($array));
  107. }else{
  108. redirect('acceso/login/','refresh');
  109. }
  110. }
  111. }
  112. public function logout()
  113. {
  114. $this->session->unset_userdata('identificador');
  115. $this->session->unset_userdata('perfil');
  116. redirect('acceso/login','refresh');
  117. }
  118. public function registro(){
  119. if($this->session->userdata('identificador') && $this->session->userdata('perfil')){
  120. $perfil = $this->session->userdata('perfil');
  121. if ($perfil == 1) {
  122. $perfil = "Administrador";
  123. }elseif ($perfil == 2 || $perfil == 4) {
  124. $perfil = "Maestro";
  125. }elseif ($perfil == 3) {
  126. $perfil = "Usuario";
  127. }
  128. redirect($perfil."/dashboard","refresh");
  129. }else{
  130. if($this->input->is_ajax_request()){
  131. $data = ($this->input->post()==NULL)? $this->input->get() : $this->input->post();
  132. if($this->input->post()!=NULL){
  133. foreach ($data as $key => $value) {
  134. $data["$key"] = $this->security->xss_clean($value);
  135. }
  136. $this->form_validation->set_rules('Nombres','Nombre completo','trim|required|min_length[4]|max_length[150]');
  137. $this->form_validation->set_rules('Apellido Paterno','Apellido Paterno','trim|required|min_length[4]|max_length[150]');
  138. $this->form_validation->set_rules('Apellido Materno','Apellido Materno','trim|min_length[4]|max_length[150]');
  139. $this->form_validation->set_rules('Correo', 'Correo', 'trim|required|is_unique[usuarios.Correo]');
  140. $this->form_validation->set_rules('Password','Contraseña','trim|required|min_length[8]|max_length[16]');
  141. $this->form_validation->set_rules('Tipo','Tipo de cuenta','trim|required|numeric');
  142. if ($this->form_validation->run() != FALSE) {
  143. date_default_timezone_set('America/Mexico_City');
  144. $data["Password"] = sha1(md5($data["Password"]));
  145. $result = $this->macceso->nuevousuario($data);
  146. if ($result != FALSE) {
  147. $array = array("result" => TRUE);
  148. }else{
  149. $array = array("result" => FALSE,"error" => "Error al insertar los datos, vuelve a intentarlo");
  150. }
  151. } else {
  152. $errors = $this->form_validation->error_array();
  153. $array = array("result" => FALSE,"error" => $errors);
  154. }
  155. $this->output->set_content_type("application/json")->set_output(json_encode($array));
  156. }
  157. }
  158. }
  159. }
  160. public function gustos(){
  161. if(!$this->session->userdata('identificador') || !$this->session->userdata('perfil')){
  162. redirect("acceso/login","refresh");
  163. }else{
  164. if($this->input->is_ajax_request()){
  165. $data = ($this->input->post()==NULL)? $this->input->get() : $this->input->post();
  166. if($this->input->post()!=NULL){
  167. foreach ($data as $key => $value) {
  168. $data["$key"] = $this->security->xss_clean($value);
  169. }
  170. $categorias=explode(',',$data['Categorias']);
  171. $data = "";
  172. $result = TRUE
  173. $data["ID"] = $this->session->userdata('identificador');
  174. for($i=0;$i<count($categorias);$i++) {
  175. $data["Categoria"] = $categorias[$i];
  176. $result = $this->madmin->cargarcaracteristicas($data);
  177. if($result==FALSE) {
  178. $result = FALSE;
  179. }
  180. }
  181. if ($result != FALSE) {
  182. $array = array("result" => TRUE);
  183. }else{
  184. $array = array("result" => FALSE,"error" => "Error al insertar los datos, vuelve a intentarlo");
  185. }
  186. $this->output->set_content_type("application/json")->set_output(json_encode($array));
  187. }
  188. }
  189. }
  190. }
  191. }
  192. /* End of file acceso.php */
  193. /* Location: ./application/controllers/acceso.php */