PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/backend/modules/login/utils/functions.inc.php

https://gitlab.com/boxnia/NFU_MOVIL
PHP | 346 lines | 178 code | 61 blank | 107 comment | 43 complexity | 2c84238f37b6bfe2e21ed880b5678cfd MD5 | raw file
  1. <?php
  2. function validate_users($value) {
  3. $error = array();
  4. $valido = true;
  5. $error1 = "";
  6. $error2 = "";
  7. $filtro = array(
  8. /* 'codigo' => array(
  9. 'filter' => FILTER_VALIDATE_REGEXP,
  10. 'options' => array('regexp' => '/^.{5,10}$/')
  11. ), */
  12. 'nombre' => array(
  13. 'filter' => FILTER_VALIDATE_REGEXP,
  14. 'options' => array('regexp' => '/^[a-zA-Z0-9](_(?!(\.|_))|\.(?!(_|\.))|[a-zA-Z0-9]){0,18}[a-zA-Z0-9]$/')
  15. ), /*
  16. 'apellidos' => array(
  17. 'filter' => FILTER_VALIDATE_REGEXP,
  18. 'options' => array('regexp' => '/^\D{3,30}$/')
  19. ),
  20. 'direccion' => array(
  21. 'filter' => FILTER_VALIDATE_REGEXP,
  22. 'options' => array('regexp' => '/^\D{3,300}$/')
  23. ),
  24. 'numero' => array(
  25. 'filter' => FILTER_VALIDATE_INT,
  26. 'options' => array('min_range' => 0, 'max_range' => 1000)
  27. ),
  28. 'pais' => array(
  29. 'filter' => FILTER_VALIDATE_REGEXP,
  30. 'options' => array('regexp' => '/^[a-zA-Z_]*$/')
  31. )
  32. ,
  33. 'provincia' => array(
  34. 'filter' => FILTER_VALIDATE_REGEXP,
  35. 'options' => array('regexp' => '/^[a-zA-Z0-9, _]*$/')
  36. ),
  37. 'poblacion' => array(
  38. 'filter' => FILTER_CALLBACK,
  39. 'options' => 'validate_poblacion'
  40. ),
  41. 'nivel' => array(
  42. // 'filter'=>FILTER_CALLBACK,
  43. //'options'=>'validar_checked'
  44. ), */
  45. 'email' => array(
  46. 'filter' => FILTER_CALLBACK,
  47. 'options' => 'validatemail'
  48. ),
  49. 'password' => array(
  50. 'filter' => FILTER_VALIDATE_REGEXP,
  51. 'options' => array('regexp' => '/^.{6,12}$/')
  52. ),
  53. 'password2' => array(
  54. 'filter' => FILTER_VALIDATE_REGEXP,
  55. 'options' => array('regexp' => '/^.{6,12}$/')
  56. ),
  57. 'fecha_nac' => array(
  58. 'filter' => FILTER_VALIDATE_REGEXP,
  59. 'options' => array('regexp' => '/\d{2}.\d{2}.\d{4}$/')
  60. ), /*
  61. 'fecha_registro' => array(
  62. 'filter' => FILTER_VALIDATE_REGEXP,
  63. 'options' => array('regexp' => '/\d{2}.\d{2}.\d{4}$/')
  64. ), */
  65. );
  66. $resultado = filter_var_array($value, $filtro);
  67. //$resultado['deportes'] = $value['deportes'];
  68. /* if (!$resultado['nivel']) {
  69. // $error['nivel'] = 'Seleccionar un deporte';
  70. } else {
  71. $resultado['nivel'] = $value['nivel'];
  72. $valido = true;
  73. }
  74. */
  75. /* $error1 = validar_facilities_checked($value['deportes']);
  76. if ($error1 !== "") {
  77. $error['deportes'] = $error1;
  78. $valido = false;
  79. } else {
  80. $resultado['deportes'] = $value['deportes'];
  81. };
  82. */
  83. if ($resultado != null && $resultado) {
  84. $error2 = validar_passwd($value['password'], $value['password2']);
  85. if ($error2 !== "") {
  86. $error['password'] = $error2;
  87. $valido = false;
  88. } else {
  89. $resultado['password'] = $value['password'];
  90. $valido = true;
  91. }
  92. /* if (!$resultado['codigo']) {
  93. $error['codigo'] = 'El codigo introducido no es valido, mínimo 5 carácteres';
  94. $resultado['codigo'] = $value['codigo'];
  95. $valido = false;
  96. }
  97. */
  98. if (!$resultado['nombre']) {
  99. $error['nombre'] = 'El nombre introducido no es valido';
  100. $resultado['nombre'] = $value['nombre'];
  101. $valido = false;
  102. } else {
  103. $resultado['nombre'] = $value['nombre'];
  104. $valido = true;
  105. }
  106. if (!$resultado['email']) {
  107. $error['email'] = 'El email no es valido';
  108. $resultado['email'] = $value['email'];
  109. $valido = false;
  110. } else {
  111. $resultado['email'] = $value['email'];
  112. $valido = true;
  113. }
  114. /* if(!$resultado['password'] || $resultado['password']!=$_POST['password2'] ){
  115. $error['password'] = 'Password debe tener de 6 a 12 caracteres y las dos contrasenyas deben ser iguales';
  116. $resultado['password'] = $_POST['password'];
  117. $valido = false;
  118. }
  119. */
  120. if ($value['fecha_nac'] === "" || $value['fecha_registro'] === "" || !compruebaFecha($value['fecha_nac']) || !valida_dates($value['fecha_nac'], $value['fecha_registro'])) {
  121. $error['fecha_nac'] = 'El usuario debe ser mayor de edad';
  122. $error['fecha_registro'] = 'El campo esta vacio';
  123. $resultado['fecha_nac'] = $value['fecha_nac'];
  124. $valido = false;
  125. } else if (!compruebaFecha($value['fecha_nac'])) {
  126. $error['fecha_nac'] = 'La fecha no es válida';
  127. $valido = false;
  128. } else {
  129. $resultado['fecha_nac'] = $value['fecha_nac'];
  130. $resultado['fecha_registro'] = $value['fecha_registro'];
  131. $valido = true;
  132. }
  133. } else {
  134. $valido = false;
  135. }
  136. $return = array('resultado' => $valido, 'error' => $error, 'datos' => $resultado);
  137. return $return;
  138. }
  139. function valida_dates($start_day, $daylight) {
  140. // list($mes_one, $dia_one, $anio_one) = split('/', $start_day);
  141. //list($mes_two, $dia_two, $anio_two)= split('/', $daylight);
  142. $date = date_create_from_format("m-d-Y", $start_day);
  143. $myDateTime = date_format($date, "Y/m/d");
  144. $date1 = date_create_from_format("Y-m-d", $daylight);
  145. $myDateTime1 = date_format($date1, "Y/m/d");
  146. $newDate = strtotime($myDateTime);
  147. $newDate1 = strtotime($myDateTime1);
  148. $days_between = ceil(abs($newDate - $newDate1) / 84600);
  149. //pongo 365 para no buscar la fecha, para ser mayor de 18 años, tendría que ser mayor de 6570 días.
  150. if ($days_between > 6570) {
  151. return true;
  152. }
  153. return false;
  154. /*
  155. $newDate = strtotime($start_day);
  156. $newDate1 = strtotime($daylight);
  157. $days_between = ceil(abs($newDate - $newDate1) / 86400);
  158. if ($days_between>= 365) {
  159. return true;
  160. }
  161. return false;
  162. */
  163. }
  164. function login($value) {
  165. $filtro = array(
  166. 'user' => array(
  167. 'filter' => FILTER_VALIDATE_REGEXP,
  168. 'options' => array('regexp' => '/^\D{3,30}$/')
  169. ),
  170. 'password' => array(
  171. 'filter' => FILTER_VALIDATE_REGEXP,
  172. 'options' => array('regexp' => '/^.{6,100}$/')
  173. ),
  174. );
  175. $resultado = filter_var_array($value, $filtro);
  176. if ($resultado != null && $resultado) {
  177. if (!$resultado['user']) {
  178. $error['user'] = 'El usuario introducido no es valido';
  179. $resultado['user'] = $value['user'];
  180. $valido = false;
  181. } else {
  182. $resultado['user'] = $value['user'];
  183. $valido = true;
  184. }
  185. if (!$resultado['password']) {
  186. $error['password'] = 'El password no es valido';
  187. $resultado['password'] = $value['password'];
  188. $valido = false;
  189. } else {
  190. $resultado['password'] = $value['password'];
  191. $valido = true;
  192. }
  193. } else {
  194. $valido = false;
  195. }
  196. return $return = array('resultado' => $valido, 'error' => $error, 'datos' => $resultado);
  197. }
  198. /* $usuario = "Usuario o password incorrecto";
  199. $password = "El password no puede estar vacío";
  200. if ($resultado) {
  201. $resultado = count_chars($value['password']);
  202. if ($resultado > 0) {
  203. $resultado = true;
  204. } else {
  205. $password = "El password no puede estar vacío";
  206. $resultado = false;
  207. }
  208. }
  209. $errores = array('usuario' => $usuario, 'password' => $password);
  210. $datos = array('usuario' => $_POST['nombre'], 'password' => $_POST['password']);
  211. return $return = array('resultado' => $resultado, 'errores' => $errores, 'datos' => $datos);
  212. } */
  213. function validatemail($email) {
  214. $email = filter_var($email, FILTER_SANITIZE_EMAIL);
  215. if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  216. if (filter_var($email, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^.{5,50}$/')))) {
  217. return $email;
  218. }
  219. }
  220. return false;
  221. }
  222. function validar_facilities_checked($facilities) {
  223. $_error = "";
  224. if (empty($facilities)) {
  225. return $_error = "Seleccione al menos dos opciones";
  226. } else {
  227. $no_checked = count($facilities);
  228. if ($no_checked < 2)
  229. return $_error = "Seleccione al menos dos opciones";
  230. }
  231. return $_error = "";
  232. }
  233. function validar_passwd($passwd, $passwd2) {
  234. $errorpassw = "";
  235. if (strlen($passwd) < 6) {
  236. return $errorpassw = "Password debe tener al menos 6 caracteres";
  237. }
  238. if (strlen($passwd) > 16) {
  239. return $errorpassw = "Password no puede tener m�s de 16 caracteres";
  240. }
  241. if (!preg_match('`[a-z]`', $passwd)) {
  242. return $errorpassw = "Password debe tener al menos una letra minúscula";
  243. }
  244. if (!preg_match('`[A-Z]`', $passwd)) {
  245. return $errorpassw = "Password debe tener al menos una letra mayúscula";
  246. }
  247. if (!preg_match('`[0-9]`', $passwd)) {
  248. return $errorpassw = "Password debe tener al menos un caracter numérico";
  249. }
  250. if ($passwd != $passwd2) {
  251. return $errorpassw = "Los password deben coincidir";
  252. }
  253. return $errorpassw = "";
  254. }
  255. function compruebaFecha($date) {
  256. $test_arr = explode('-', $date);
  257. if (count($test_arr) == 3) {
  258. if (checkdate($test_arr[0], $test_arr[1], $test_arr[2])) {
  259. // valid date ...
  260. return true;
  261. } else {
  262. // problem with dates ...
  263. return false;
  264. }
  265. } else {
  266. return false;
  267. // problem with input ...
  268. }
  269. }
  270. function validate_poblacion($poblacion) {
  271. //$poblacion = addslashes($poblacion);
  272. $poblacion = filter_var($poblacion, FILTER_SANITIZE_STRING);
  273. return $poblacion;
  274. }