c_abstract_data.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?
  2. $ruta = '../';
  3. require "{$_SERVER['DOCUMENT_ROOT']}/class/c_login.php";
  4. trait DatabaseModel
  5. {
  6. public function __construct(protected string $tableName, protected array $columns = [])
  7. {
  8. }
  9. public function get(array $params = [], string $what = '*')
  10. {
  11. global $db;
  12. $conditions = [];
  13. foreach ($params as $key => $value) {
  14. $conditions[] = "$key = :$key";
  15. }
  16. $sql = "SELECT $what FROM $this->tableName";
  17. if ($conditions) {
  18. $sql .= " WHERE " . implode(" AND ", $conditions);
  19. }
  20. $result = $db->query($sql, $params);
  21. return $result;
  22. }
  23. protected function insert__(array $params = [], ?string $where = null)
  24. {
  25. global $db;
  26. if ($where === null) {
  27. $where = $this->tableName;
  28. }
  29. $columns = [];
  30. foreach ($params as $key => $value) {
  31. $columns[] = "$key = :$key";
  32. }
  33. $sql = "INSERT INTO $where SET " . implode(", ", $columns);
  34. $result = $db->query($sql, $params);
  35. return $result;
  36. }
  37. }
  38. abstract class WebServiceSGU
  39. {
  40. const BASE_URL = "https://portal.ulsa.edu.mx/servicios/AuditoriaAsistencialRest/AuditoriaAsistencialService.svc/auditoriaAsistencial";
  41. private static array $keys = [
  42. 'username' => 'SGU_APSA_AUD_ASIST',
  43. 'password' => 'B4qa594JFPr2ufHrZdHS8A==',
  44. ];
  45. private static ?JsonSchema\Validator $validator = null;
  46. private string $baseUrl;
  47. public function __construct(protected string $endpoint, protected ?string $schema = null)
  48. {
  49. $this->baseUrl = self::BASE_URL . $endpoint;
  50. }
  51. private static function initCurl(array $options = [])
  52. {
  53. $ch = curl_init();
  54. curl_setopt_array($ch, $options);
  55. return $ch;
  56. }
  57. private static function get_token(): string
  58. {
  59. $curl = self::initCurl([
  60. CURLOPT_URL => self::BASE_URL . "/inicioSesion/seleccionar",
  61. CURLOPT_RETURNTRANSFER => true,
  62. CURLOPT_CUSTOMREQUEST => "POST",
  63. CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
  64. CURLOPT_POSTFIELDS => json_encode(self::$keys),
  65. ]);
  66. $response = curl_exec($curl);
  67. $err = curl_error($curl);
  68. curl_close($curl);
  69. if ($err)
  70. throw new Exception("cURL Error: $err");
  71. return trim($response, '"'); // remove quotes
  72. }
  73. protected function validate_schema($data): bool
  74. {
  75. if ($this->schema === null)
  76. return true;
  77. self::getValidator()->validate($data, (object) json_decode($this->schema));
  78. return self::getValidator()->isValid();
  79. }
  80. public static function getValidator(): JsonSchema\Validator
  81. {
  82. return self::$validator ??= new JsonSchema\Validator();
  83. }
  84. public function get_errors(): array
  85. {
  86. return self::getValidator()->getErrors();
  87. }
  88. public function get(array $data = []): array
  89. {
  90. if (!$this->validate_schema($data)) {
  91. throw new Exception('Invalid schema');
  92. }
  93. $ch = self::initCurl([
  94. CURLOPT_POST => 1,
  95. CURLOPT_POSTFIELDS => json_encode($data),
  96. CURLOPT_URL => $this->baseUrl,
  97. CURLOPT_HTTPHEADER => [
  98. 'Content-Type: application/json',
  99. 'Accept: application/json',
  100. 'username: ' . self::$keys['username'],
  101. 'token: ' . self::get_token(),
  102. ],
  103. CURLOPT_RETURNTRANSFER => 1,
  104. ]);
  105. $response = curl_exec($ch);
  106. if (curl_errno($ch)) {
  107. throw new Exception('cURL Error: ' . curl_error($ch));
  108. }
  109. curl_close($ch);
  110. $response = json_decode($response, true);
  111. if ($response === null) {
  112. throw new Exception('Invalid response');
  113. }
  114. return $response;
  115. }
  116. }