function __construct() { $driver = array("mysql" => "mysql:dbname=%s;host=%s", "oracle" => "oci:dbname=%s;host=%s;port=1521", "pgsql" => "pgsql:dbname=%s host=%s"); try { $this->PDO = new PDO(sprintf($driver[Config::Reader("BD_DRIVE")], Config::Reader("BD_NAME"), Config::Reader("BD_HOST")), Config::Reader("BD_USER"), base64_decode(Config::Reader("BD_PASS")), array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_TIMEOUT => 5, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'')); } catch (PDOException $e) { return json_encode(["data" => "Ha Ocurrido un error al conectarse (" . $e->getMessage() . ")"]); } }
function procesar() { $c = "Control\\" . Config::Reader("control"); if (class_exists($c, true)) { $funcion = Config::Reader("function"); $control = new $c(); $control->Run($funcion, explode("/", Config::Reader("parametros"))); } else { echo json_encode(["data" => "Recurso no disponible"]); } }
function before($funcion) { if ($funcion != "authenticate") { try { $secretKey = base64_decode(Config::Reader("SALT")); $header = apache_request_headers(); $header["Authorization"] = trim(str_replace("Bearer", "", $header["Authorization"])); $jwt = JWT::decode($header["Authorization"], $secretKey, array('HS512')); $this->UserToken = $jwt; } catch (ExpiredException $e) { $this->Response["error"] = $e->getMessage(); return false; } } return true; }
function authenticate() { $usuario = $this->Usuarios->findByUsuario($this->Post["usuario"]); if (!empty($usuario)) { if (password_verify($this->Post["password"], $usuario["pass"])) { unset($usuario["pass"]); $secretKey = base64_decode(Config::Reader("SALT")); $data = ['iss' => SITE, 'aud' => SITE, 'iat' => time(), 'exp' => time() + 60 * 60 * 24 * 7, 'sub' => 'usuario', 'admin' => true, 'data' => $usuario]; $jwt = JWT::encode($data, $secretKey, 'HS512'); $this->Response = ['token' => $jwt]; } else { $this->Response = ["error" => "La contraseña o usuario incorrecto"]; } } else { $this->Response = ["error" => "El usuario no existe"]; } }
function __construct() { $this->NameForm = ucwords(substr(Config::Reader('control'), 0, -7)) . ucwords(Config::Reader('function')); }