bd_pdo.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. require_once "vendor/autoload.php";
  3. use \SeinopSys\PostgresDb;
  4. # env
  5. $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
  6. $dotenv->load();
  7. # Connect to the database
  8. try {
  9. // Postgres
  10. $pdo = new PDO("pgsql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_NAME']}", $_ENV['DB_USER'], $_ENV['DB_PASS']);
  11. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12. $db = new PostgresDb();
  13. $db->setConnection($pdo);
  14. } catch (PDOException $e) {
  15. echo "Error: " . $e->getMessage();
  16. }
  17. trait DatabaseModel
  18. {
  19. public function __construct(protected string $tableName, protected array $columns = [])
  20. {
  21. }
  22. public function get(array $params = [], string $what = '*')
  23. {
  24. global $db;
  25. $conditions = [];
  26. foreach ($params as $key => $value) {
  27. $conditions[] = "$key = :$key";
  28. }
  29. $sql = "SELECT $what FROM $this->tableName";
  30. if ($conditions) {
  31. $sql .= " WHERE " . implode(" AND ", $conditions);
  32. }
  33. $result = $db->query($sql, $params);
  34. return $result;
  35. }
  36. protected function insert__(array $params = [], ?string $where = null)
  37. {
  38. global $db;
  39. if ($where === null) {
  40. $where = $this->tableName;
  41. }
  42. $columns = [];
  43. foreach ($params as $key => $value) {
  44. $columns[] = "$key = :$key";
  45. }
  46. $sql = "INSERT INTO $where SET " . implode(", ", $columns);
  47. $result = $db->query($sql, $params);
  48. return $result;
  49. }
  50. }
  51. abstract class WebServiceSGU
  52. {
  53. const BASE_URL = "https://portal.ulsa.edu.mx/servicios/AuditoriaAsistencialRest/AuditoriaAsistencialService.svc/auditoriaAsistencial";
  54. private static array $keys = [
  55. 'username' => 'SGU_APSA_AUD_ASIST',
  56. 'password' => 'B4qa594JFPr2ufHrZdHS8A==',
  57. ];
  58. private static ?JsonSchema\Validator $validator = null;
  59. private string $baseUrl;
  60. public function __construct(protected string $endpoint, protected ?string $schema = null)
  61. {
  62. $this->baseUrl = self::BASE_URL . $endpoint;
  63. }
  64. private static function initCurl(array $options = [])
  65. {
  66. $ch = curl_init();
  67. curl_setopt_array($ch, $options);
  68. return $ch;
  69. }
  70. private static function get_token(): string
  71. {
  72. $curl = self::initCurl([
  73. CURLOPT_URL => self::BASE_URL . "/inicioSesion/seleccionar",
  74. CURLOPT_RETURNTRANSFER => true,
  75. CURLOPT_CUSTOMREQUEST => "POST",
  76. CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
  77. CURLOPT_POSTFIELDS => json_encode(self::$keys),
  78. ]);
  79. $response = curl_exec($curl);
  80. $err = curl_error($curl);
  81. curl_close($curl);
  82. if ($err)
  83. throw new Exception("cURL Error: $err");
  84. return trim($response, '"'); // remove quotes
  85. }
  86. protected function validate_schema($data): bool
  87. {
  88. if ($this->schema === null)
  89. return true;
  90. self::getValidator()->validate($data, (object) json_decode($this->schema));
  91. return self::getValidator()->isValid();
  92. }
  93. public static function getValidator(): JsonSchema\Validator
  94. {
  95. return self::$validator ??= new JsonSchema\Validator();
  96. }
  97. public function get_errors(): array
  98. {
  99. return self::getValidator()->getErrors();
  100. }
  101. public function get(array $data = []): array
  102. {
  103. if (!$this->validate_schema($data)) {
  104. throw new Exception('Invalid schema');
  105. }
  106. $ch = self::initCurl([
  107. CURLOPT_POST => 1,
  108. CURLOPT_POSTFIELDS => json_encode($data),
  109. CURLOPT_URL => $this->baseUrl,
  110. CURLOPT_HTTPHEADER => [
  111. 'Content-Type: application/json',
  112. 'Accept: application/json',
  113. 'username: ' . self::$keys['username'],
  114. 'token: ' . self::get_token(),
  115. ],
  116. CURLOPT_RETURNTRANSFER => 1,
  117. ]);
  118. $response = curl_exec($ch);
  119. if (curl_errno($ch)) {
  120. throw new Exception('cURL Error: ' . curl_error($ch));
  121. }
  122. curl_close($ch);
  123. $response = json_decode($response, true);
  124. if ($response === null) {
  125. throw new Exception('Invalid response');
  126. }
  127. return $response;
  128. }
  129. }
  130. final class Horarios extends WebServiceSGU
  131. {
  132. public function __construct()
  133. {
  134. parent::__construct(
  135. "/seleccionar",
  136. <<<JSON
  137. {
  138. "\$schema": "http://json-schema.org/draft-07/schema#",
  139. "type": "object",
  140. "required": ["idPeriodo"],
  141. "properties": {
  142. "idPeriodo": {
  143. "type": "integer",
  144. "description": "Identificador del periodo a consultar."
  145. },
  146. "claveFacultad": {
  147. "type": "string",
  148. "description": "Clave de la facultad a consultar.",
  149. "pattern": "^[a-zA-Z0-9]*$"
  150. },
  151. "claveCarrera": {
  152. "type": "string",
  153. "description": "Clave de la carrera a consultar.",
  154. "pattern": "^[a-zA-Z0-9]*$"
  155. },
  156. "claveProfesor": {
  157. "type": "string",
  158. "description": "Clave del empleado a consultar.",
  159. "pattern": "^[a-zA-Z0-9]*$"
  160. },
  161. "fecha": {
  162. "type": "string",
  163. "description": "Fecha de la clase.",
  164. "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
  165. }
  166. }
  167. }
  168. JSON
  169. );
  170. }
  171. }