Skip to content

Instantly share code, notes, and snippets.

@oneluiz
Created January 3, 2019 04:58
Show Gist options
  • Save oneluiz/a8524387a51ca0015037f6a83a2cb035 to your computer and use it in GitHub Desktop.
Save oneluiz/a8524387a51ca0015037f6a83a2cb035 to your computer and use it in GitHub Desktop.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
use Restserver\Libraries\REST_Controller;
require APPPATH . '/libraries/REST_Controller.php';
require APPPATH . '/libraries/Format.php';
class Rest extends REST_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model(
array(
'user_model',
'login_model',
'user_model',
'app_model',
'venta_model',
'numero_model',
'api_model'
)
);
$this->load->helper('venta_helper');
}
public function index_get()
{
$this->response(array(
'Nombre' => 'Servicreditos Api',
'Tipo' => 'ApiRest',
'version' => '1.0.0v'
));
}
public function usuario_get($id)
{
$usuario = $this->api_model->usuario($id)->result();
$this->response($usuario);
}
public function caja_get($usuario)
{
date_default_timezone_set('America/Costa_Rica');
$fecha = date('Y-m-d');
$caja = $this->venta_model->caja_vendedor($usuario, $fecha);
foreach ($caja->result() as $cv) {
$c = array("caja" => $cv->saldo);
$this->response($c, REST_Controller::HTTP_OK);
}
}
public function sorteo_get()
{
$sorteo = $this->app_model->TipoSorteo();
$this->response($sorteo);
}
public function config_sorteo_get($sorteo, $vendedor)
{
$sorteo = $this->venta_model->ListarTipoDeSorteo($sorteo, $vendedor);
$this->response($sorteo);
}
public function iniciar_sesion_post()
{
$correo = $this->post("email", true);
$contrasena = $this->post("password", true);
$config = $this->config->item("cookieprefix");
if ($this->user->loggedin) {
$this->response([
'status' => FALSE,
'message' => lang("error_27")
], REST_Controller::HTTP_NOT_FOUND);
}
if ($this->settings->info->login_protect) {
// Comprobar el usuario para 5 intentos de inicio de sesión
$s = $this->login_model->get_login_attempts($_SERVER['REMOTE_ADDR'], $correo, (15 * 60));
if ($s->num_rows() > 0) {
$s = $s->row();
if ($s->count >= 5) {
$this->response([
'status' => FALSE,
'message' => lang("error_68")
], REST_Controller::HTTP_NOT_FOUND);
}
}
}
if (empty($correo) || empty($contrasena)) {
$this->response([
'status' => FALSE,
'message' => lang("error_28")
], REST_Controller::HTTP_NOT_FOUND);
}
$login = $this->login_model->getUserByEmail($correo);
if ($login->num_rows() == 0) {
$login = $this->login_model->getUserByUsername($correo);
if ($login->num_rows() == 0) {
$this->login_protect($correo);
$this->response([
'status' => FALSE,
'message' => lang("error_29")
], REST_Controller::HTTP_NOT_FOUND);
}
}
$r = $login->row();
$userid = $r->ID;
$correo = $r->email;
$phpass = new PasswordHash(12, false);
if (!$phpass->CheckPassword($contrasena, $r->password)) {
$this->login_protect($correo);
$this->response([
'status' => FALSE,
'message' => lang("error_29")
], REST_Controller::HTTP_NOT_FOUND);
}
if ($this->settings->info->activate_account) {
if (!$r->active) {
$this->response([
'status' => FALSE,
'message' => lang("error_72") . " " . site_url("register/send_activation_code/" . $r->ID . "/" . urlencode($r->email)) . " " . lang("error_73") . " " . lang("error_74")
], REST_Controller::HTTP_NOT_FOUND);
}
}
if ($this->settings->info->secure_login) {
// Generar un token
$token = rand(1, 100000) . $correo;
$token = md5(sha1($token));
// Guárdalo
$this->login_model->updateUserToken($userid, $token);
} else {
if (empty($r->token)) {
// Generar un token
$token = rand(1, 100000) . $correo;
$token = md5(sha1($token));
// Guárdalo
$this->login_model->updateUserToken($userid, $token);
} else {
if ($r->online_timestamp + (3600 * 24 * 30 * 2) < time()) {
// Generar un token
$token = rand(1, 100000) . $correo;
$token = md5(sha1($token));
// Guárdalo
$this->login_model->updateUserToken($userid, $token);
} else {
$token = $r->token;
}
}
}
$this->user_model->add_log(array(
"userid" => $userid,
"IP" => $_SERVER['REMOTE_ADDR'],
"user_agent" => $_SERVER['HTTP_USER_AGENT'],
"timestamp" => time(),
"message" => lang("ctn_435")
));
$info_user = $this->user_model->get_user_by_id($userid)->result();
$this->set_response($info_user, REST_Controller::HTTP_CREATED);
}
private function login_protect($correo)
{
if ($this->settings->info->login_protect) {
// Add Count
$s = $this->login_model->get_login_attempts($_SERVER['REMOTE_ADDR'], $correo, (15 * 60));
if ($s->num_rows() > 0) {
$s = $s->row();
$this->login_model->update_login_attempt($s->ID, array(
"count" => $s->count + 1
));
} else {
$this->login_model->add_login_attempt(array(
"IP" => $_SERVER['REMOTE_ADDR'],
"username" => $correo,
"count" => 1,
"timestamp" => time()
));
}
}
}
public function facturar_post()
{
/**
* Variables POST para la venta
*/
$usuario = $this->common->nohtml($this->input->post('usuario', true));
$tipo_sorteo= $this->common->nohtml($this->input->post('TipoDeSorteo', true));
$codigo = CodigoPremio($usuario);
$cliente = $this->common->nohtml($this->input->post('cliente', true));
$tn = $this->common->nohtml($this->input->post('tn', true));
$total = $this->common->nohtml($this->input->post('total', true));
$sorteo = $this->common->nohtml($this->input->post('Sorteo', true));
$numero = $this->input->post('numero', true);
$monto = $this->input->post('monto', true);
$TipoSorteoFactura = $this->venta_model->Buscar_Sorteo_Factura($tipo_sorteo);
$TipoSorteoFactura = $TipoSorteoFactura->row();
/**
* Comprobar Numero Bloqueado
* @var integer
*/
for($i = 0; $i < count($numero); $i++)
{
$comprobar = $this->venta_model->ComprobarNumero($tipo_sorteo, $numero[$i], $usuario, $monto[$i])->row();
if ($comprobar->id == 0) {
$this->response($info_user, REST_Controller::HTTP_OK);
}
}
/**
* Genera y guarda la factura
*/
$id_factura = $this->venta_model->GuardarFactura($usuario, $tipo_sorteo, $cliente, $codigo);
/**
* Genera y guarda el detalle de la factura
*/
foreach ($id_factura as $key => $idf)
{
for($i = 0; $i < count($numero); $i++)
{
$this->venta_model->GuardarDetalleFactura($idf->idF, $numero[$i], $monto[$i]);
}
$detalleFactura = $this->api_model->detalle_factura($idf->idF);
$detalleFactura = $detalleFactura->row();
date_default_timezone_set('America/Costa_Rica');
$fecha = Date('Y-m-d h:i:s A');
$json = '{
"factura": '.$idf->idF.',
"fecha": "'.$fecha.'",
"cliente": "'.$cliente.'",
"Sorteo": "'.$TipoSorteoFactura->sorteo.'",
"detalle": [
'.$detalleFactura->detalle_factura.'
],
"total": '.$total.'
}';
$this->response(json_decode($json), REST_Controller::HTTP_OK);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment