123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?
- $ruta = '../';
- require "{$_SERVER['DOCUMENT_ROOT']}/class/c_login.php";
- trait DatabaseModel
- {
- public function __construct(protected string $tableName, protected array $columns = [])
- {
- }
- public function get(array $params = [], string $what = '*')
- {
- global $db;
- $conditions = [];
- foreach ($params as $key => $value) {
- $conditions[] = "$key = :$key";
- }
- $sql = "SELECT $what FROM $this->tableName";
- if ($conditions) {
- $sql .= " WHERE " . implode(" AND ", $conditions);
- }
- $result = $db->query($sql, $params);
- return $result;
- }
- protected function insert__(array $params = [], ?string $where = null)
- {
- global $db;
- if ($where === null) {
- $where = $this->tableName;
- }
- $columns = [];
- foreach ($params as $key => $value) {
- $columns[] = "$key = :$key";
- }
- $sql = "INSERT INTO $where SET " . implode(", ", $columns);
- $result = $db->query($sql, $params);
- return $result;
- }
- }
- abstract class WebServiceSGU
- {
- const BASE_URL = "https://portal.ulsa.edu.mx/servicios/AuditoriaAsistencialRest/AuditoriaAsistencialService.svc/auditoriaAsistencial";
- private static array $keys = [
- 'username' => 'SGU_APSA_AUD_ASIST',
- 'password' => 'B4qa594JFPr2ufHrZdHS8A==',
- ];
- private static ?JsonSchema\Validator $validator = null;
- private string $baseUrl;
- public function __construct(protected string $endpoint, protected ?string $schema = null)
- {
- $this->baseUrl = self::BASE_URL . $endpoint;
- }
- private static function initCurl(array $options = [])
- {
- $ch = curl_init();
- curl_setopt_array($ch, $options);
- return $ch;
- }
- private static function get_token(): string
- {
- $curl = self::initCurl([
- CURLOPT_URL => self::BASE_URL . "/inicioSesion/seleccionar",
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_CUSTOMREQUEST => "POST",
- CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
- CURLOPT_POSTFIELDS => json_encode(self::$keys),
- ]);
- $response = curl_exec($curl);
- $err = curl_error($curl);
- curl_close($curl);
- if ($err)
- throw new Exception("cURL Error: $err");
- return trim($response, '"'); // remove quotes
- }
- protected function validate_schema($data): bool
- {
- if ($this->schema === null)
- return true;
- self::getValidator()->validate($data, (object) json_decode($this->schema));
- return self::getValidator()->isValid();
- }
- public static function getValidator(): JsonSchema\Validator
- {
- return self::$validator ??= new JsonSchema\Validator();
- }
- public function get_errors(): array
- {
- return self::getValidator()->getErrors();
- }
- public function get(array $data = []): array
- {
- if (!$this->validate_schema($data)) {
- throw new Exception('Invalid schema');
- }
- $ch = self::initCurl([
- CURLOPT_POST => 1,
- CURLOPT_POSTFIELDS => json_encode($data),
- CURLOPT_URL => $this->baseUrl,
- CURLOPT_HTTPHEADER => [
- 'Content-Type: application/json',
- 'Accept: application/json',
- 'username: ' . self::$keys['username'],
- 'token: ' . self::get_token(),
- ],
- CURLOPT_RETURNTRANSFER => 1,
- ]);
- $response = curl_exec($ch);
- if (curl_errno($ch)) {
- throw new Exception('cURL Error: ' . curl_error($ch));
- }
- curl_close($ch);
- $response = json_decode($response, true);
- if ($response === null) {
- throw new Exception('Invalid response');
- }
- return $response;
- }
- }
|