/** * contar los logins * @param $cantidad */ private function loginCount($cantidad = 20) { $user = new getIP(); $ip = $user->ip; $fechaUnix = time(); if (DATABASE_TYPE == 'sqlite') { $sq = "CREATE TABLE IF NOT EXISTS logins (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n ip VARCHAR(256),\n fechaHora TEXT,\n tipoInt VARCHAR(100) DEFAULT 'intento',\n fecha TIMESTAMP DEFAULT current_timestamp\n )"; } else { $sq = "CREATE TABLE IF NOT EXISTS logins (\n id INTEGER PRIMARY KEY AUTO_INCREMENT,\n ip VARCHAR(256),\n fechaHora TEXT,\n tipoInt VARCHAR(100) DEFAULT 'intento',\n fecha TIMESTAMP DEFAULT current_timestamp\n )"; } $create = $this->db->query($sq); $create->closeCursor(); //insertar el primer intento $in1 = $this->db->prepare("insert into logins (id, ip, fechaHora) VALUES (null,'{$ip}','{$fechaUnix}')") or die(new ErrorMessage($this->db)); $in1->execute(); $in1->closeCursor(); //verificar logins $ing = $this->db->prepare("SELECT count(*) AS t FROM logins WHERE ip =:ip") or die(new ErrorMessage($this->db)); $ing->execute(array('ip' => $ip)); $intentos = $ing->fetchObject(); $ing->closeCursor(); if ($intentos->t >= $cantidad) { //verificar que paso mas tiempo $ultimo = $this->db->prepare("SELECT * FROM logins WHERE ip=:ip AND tipoInt='intento' ORDER BY id DESC LIMIT 1"); $ultimo->execute(array('ip' => $ip)); $ultimoA = $ultimo->fetch(); $ultimo->closeCursor(); //ultimo registro $fechaActual = time(); $fechaUlt = $ultimoA['fecha']; $mas5 = strtotime('+3 minute', strtotime($fechaUlt)); if ($fechaActual >= $mas5) { //borrar los intentos $del = $this->db->prepare("DELETE FROM logins WHERE ip=:ip") or die(new ErrorMessage($this->db)); $del->execute(array('ip' => $ip)); $del->closeCursor(); } else { echo 'Demasiados intentos fallidos de inicio de sesion<br>Debe esperar 3 minutos'; //insertar los posteriores $in1 = $this->db->prepare("INSERT INTO logins (id, ip, fechaHora,tipoInt) VALUES (NULL,:ip,:fecha,'forzado')"); $in1->execute(array('ip' => $ip, 'fecha' => $fechaUnix)); $in1->closeCursor(); $intentosFallidos = $this->db->prepare("SELECT count(*) AS t FROM logins WHERE ip=:ip AND tipoInt='forzado'") or die(new ErrorMessage($this->db)); $intentosFallidos->execute(array('ip' => $ip)); $intentosFallidosA = $intentosFallidos->fetchObject(); $intentosFallidos->closeCursor(); if ($intentosFallidosA->t >= $cantidad + 1) { sleep(6); $script = new Scripts(); $script->scriptEcho('http://yahoo.com.ar'); http_response_code(404); exit; } exit; } } else { //inserto un valor en la lista $insert = $this->db->prepare("INSERT INTO logins (id,ip,fechaHora)VALUES(NULL,:ip,:fecha)") or die(new ErrorMessage($this->db)); $insert->execute(array('ip' => $ip, 'fecha' => $fechaUnix)); } }