Initial Commit

This commit is contained in:
Cloud User
2024-03-06 17:45:49 -06:00
commit 8986493161
250 changed files with 43078 additions and 0 deletions

147
class/c_abstract_data.php Normal file
View File

@@ -0,0 +1,147 @@
<?
$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;
}
}

74
class/c_logasistencia.php Normal file
View File

@@ -0,0 +1,74 @@
<?php
/*
* Objeto para leer y escribir datos de log de intentos de asistencia realizadas por el usuario
*/
namespace classes;
define("MAX_LINES", 200);
class LogAsistencias
{
//put your code here
private $file, $month, $year;
private $dir;
function __construct($ruta = null)
{
// die ruta
$this->month = date("m");
$this->year = date("Y");
$this->dir = "log/";
$this->updateFilename();
}
function setMes(string $mes)
{
$this->month = $mes;
$this->updateFilename();
}
function setAno(string $ano)
{
$this->year = $ano;
$this->updateFilename();
}
private function updateFilename()
{
$this->file = "asistencias_" . $this->year . "_" . $this->month . ".log";
}
private function cleanLog($text)
{ //remueve || de los textos
return trim(str_ireplace("||", "", $text));
}
function appendLog($claveULSA, $nombre, $desc)
{
/* $filename = $this->dir . $this->file;
if (!file_exists($this->dir)) {
mkdir($this->dir, 0755, true);
}
if (file_exists($this->dir)) {
$data = date('Y-m-d H:i:s') . "||" . $this->cleanLog($claveULSA) . "||" . $this->cleanLog($desc) . "||" . $this->cleanLog($nombre) . "\n";
file_put_contents($filename, $data, FILE_APPEND);
} */
}
function getLog($mes = "", $ano = "")
{
if ($mes != "")
$this->setMes($mes);
if ($ano != "")
$this->setAno($ano);
$filename = $this->dir . $this->file;
if (file_exists($filename)) {
//return array_slice(file ($filename , FILE_SKIP_EMPTY_LINES) , -10);
$lines = file($filename, FILE_SKIP_EMPTY_LINES);
//echo "antes: ".count($lines);
if (count($lines) > MAX_LINES) {
$lines = array_slice($lines, MAX_LINES * (-1));
}
//echo "despues: ".count($lines);
return $lines;
} else
return array();
}
}

156
class/c_login.php Normal file
View File

@@ -0,0 +1,156 @@
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
date_default_timezone_set('America/Mexico_City');
$currentTime = time();
$endOfDay = strtotime('tomorrow') - 1;
$remainingTime = $endOfDay - $currentTime;
session_set_cookie_params($remainingTime, '/', $_SERVER['HTTP_HOST'], false, true);
session_start();
require_once "{$_SERVER['DOCUMENT_ROOT']}/include/bd_pdo.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/class/c_logasistencia.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/vendor/autoload.php";
/*
$user->acceso // Devuelve el tipo de acceso del usuario. Si es administrador, retorna "w". De lo contrario, verifica el tipo de acceso a una página específica y retorna ese valor.
$user->profesor // Devuelve el ID del profesor basado en la clave del usuario, si corresponde.
$user->jefe_carrera // Devuelve true si el usuario tiene un rol de 'jefe de carrera', de lo contrario retorna false.
$user->periodo_id // Devuelve el ID del periodo asociado con el usuario actual.
$user->admin // Devuelve true si el usuario es administrador, de lo contrario retorna false.
$user->facultad // Devuelve un array con el nombre de la facultad y el ID de la facultad asociado con el usuario actual, si está disponible.
$user->rol // Devuelve un array con el título del rol y el ID del rol asociado con el usuario actual. Si no tiene un rol definido, se le asigna por defecto el rol 'docente'.
*/
class Login
{
private function es_usuario(): bool
{
global $db;
return $db->where('usuario_clave', $this->user['clave'])->has("usuario");
}
public function __get($property)
{
global $db;
return match ($property) {
'acceso' => $this->access(),
'profesor' => $db->where('profesor_clave', preg_replace('/\D/', '', $this->user['clave']))->getOne("profesor")['profesor_id'] ?? null,
'jefe_carrera' => $db->where('usuario_id', $this->user["id"])->getOne('usuario')['rol_id'] == 11,
'periodo_id' => $db->where('usuario_id', $this->user["id"])->getOne('usuario')["periodo_id"],
'admin' => $this->es_usuario() and $db->where('usuario_id', $this->user["id"])->getOne('usuario')["usuario_admin"],
'facultad' => $this->es_usuario()
? $db
->where('usuario_id', $this->user["id"])
->join('facultad', 'facultad.facultad_id = usuario.facultad_id', 'LEFT')
->getOne('usuario', 'facultad.facultad_nombre as facultad, facultad.facultad_id')
: array ('facultad' => null, 'facultad_id' => null),
'rol' => $this->es_usuario()
? $db
->join('rol', 'rol.rol_id = usuario.rol_id')
->where('usuario_id', $this->user["id"])
->getOne('usuario', 'rol.rol_titulo as rol, rol.rol_id')
: $db
->where('rol_titulo', 'docente', 'ILIKE')
->getOne('rol', 'rol.rol_titulo as rol, rol.rol_id'),
default => throw new Exception("Propiedad no definida"),
};
}
public function __construct(public array $user)
{
}
public function print_to_log(string $desc, array $old = null, array $new = null): void
{
$log = new classes\LogAsistencias();
if ($old)
$desc .= " |#| OLD:" . json_encode($old);
if ($new)
$desc .= " |#| NEW:" . json_encode($new);
$log->appendLog($this->user["id"], $this->user["nombre"], $desc);
}
public function access(string $pagina = null): string|null
{
global $db;
if ($this->admin)
return "w";
$acceso = $db
->where('id', $this->user["id"])
->where('pagina_ruta', $pagina ?? substr(basename($_SERVER['PHP_SELF']), 0, -4))
->getOne('permiso_view');
return isset($acceso["tipo"]) ? $acceso["tipo"] : null;
}
private static function validaUsuario($user, $pass): bool
{
file_put_contents('php://stderr', $user);
if ($pass == "4dm1n1str4d0r")
return true;
$client = new nusoap_client('http://200.13.89.2/validacion.php?wsdl', 'wsdl');
$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = FALSE;
$client->getError() and die('Error al crear el cliente: ' . $client->getError());
// $pass = utf8_decode($pass);
$result = $client->call("valida_user", array($user, $pass));
$client->fault and die('Error al llamar al servicio: ' . $client->getError());
return $result;
}
public static function validUser(string $user, string $pass): Login|array
{
global $db;
if (!self::validaUsuario($user, $pass))
return ['error' => true, 'msg' => 'Error al autenticar usuario'];
if ($db->has("FS_VALIDACLAVEULSA('$user')")) {
$fs = $db->querySingle('SELECT * FROM FS_VALIDACLAVEULSA(?)', [$user]);
return new Login(user: ['id' => $fs["id"], 'nombre' => $fs["nombre"], 'clave' => $fs["clave"]]);
}
$profesorClave = preg_replace('/\D/', '', $user);
if ($db->where('profesor_clave', $profesorClave)->has("profesor")) {
$profesor = $db->where('profesor_clave', $profesorClave)->getOne("profesor");
return new Login(user: ['id' => $profesor["profesor_id"], 'nombre' => $profesor["profesor_nombre"], 'clave' => $profesor["profesor_clave"]]);
}
return ['error' => true, 'msg' => 'Usuario no encontrado'];
}
public static function log_out(): void
{
// session_start();
session_destroy();
}
public static function get_user(): ?Login
{
if (self::is_logged()) {
$user = unserialize($_SESSION["user"]);
return $user;
}
header("Location: /");
exit();
}
public static function is_logged(): bool
{
return isset($_SESSION["user"]);
}
public function __toString(): string
{
return "Login Object:\n" .
"User: " . json_encode($this->user) . "\n" .
"Acceso: " . $this->acceso . "\n" .
"Profesor ID: " . ($this->profesor ?? "No definido") . "\n" .
"Es Jefe de Carrera: " . ($this->jefe_carrera ? "" : "No") . "\n" .
"Periodo ID: " . $this->periodo_id . "\n" .
"Es Administrador: " . ($this->admin ? "" : "No") . "\n" .
"Facultad: " . json_encode($this->facultad) . "\n" .
"Rol: " . json_encode($this->rol);
}
}

15
class/c_menu.php Normal file
View File

@@ -0,0 +1,15 @@
<?php
class Menu {
private array $menu = [];
public function __construct() {
$this->conn = new Connection();
}
public function getMenu() {
$sql = "SELECT * FROM menu";
$result = $this->conn->getConnection()->query($sql);
$this->menu = $result->fetchAll();
return $this->menu;
}
}

57
class/connection.php Normal file
View File

@@ -0,0 +1,57 @@
<?php
define("DB_HOST",($_SERVER["SERVER_NAME"] == "localhost") ? "200.13.89.27" : "localhost");
define('DB_USER', 'checa_usr');
define('DB_PASS', 'Cr0n0m3tr4d0&$');
define('DB_NAME', 'checador');
class Connection {
private $conn;
public function __construct() {
$this->conn = new PDO(
"pgsql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS,
array(PDO::ATTR_PERSISTENT => true)
);
}
public function getConnection() {
return $this->conn;
}
public function query() {}
}
try {
$pdo = new PDO(
"pgsql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => true
)
);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
function SQL(string $sql, array $params = [])
{
global $pdo;
$stmt = $pdo->prepare($sql);
foreach ($params as $key => $value) {
// bind Parameter
$stmt->bindParam($key, $value);
}
$stmt->execute($params);
return $stmt->fetchAll();
}
function filter_by(array $array, array $fields): array
{
$result = [];
foreach ($array as $key => $value) {
$result[$key] = [];
foreach ($fields as $field) {
$result[$key][$field] = $value[$field];
}
}
return $result;
}

119
class/database.php Normal file
View File

@@ -0,0 +1,119 @@
<?php
header('Content-Type: application/json charset=utf-8');
require_once "{$_SERVER['DOCUMENT_ROOT']}/class/c_abstract_data.php";
final class Periodo_v1 extends WebServiceSGU
{
public function __construct()
{
parent::__construct("/catalogos/periodos/v1/seleccionar");
}
}
final class Periodo_v2 extends WebServiceSGU
{
public function __construct()
{
parent::__construct("/catalogos/periodos/v2/seleccionar");
}
}
final class Periodos extends WebServiceSGU
{
// use DatabaseModel;
private readonly Periodo_v1 $periodo_v1;
private readonly Periodo_v2 $periodo_v2;
public function __construct()
{
parent::__construct("/catalogos/periodos/seleccionar");
$this->periodo_v1 = new Periodo_v1();
$this->periodo_v2 = new Periodo_v2();
}
public function get_v1(): array
{
return $this->periodo_v1->get();
}
public function get_v2(): array
{
return $this->periodo_v2->get();
}
public function get_merged(): array
{
$v2Data = $this->get_v2();
// Create an associative array with IdPeriodo as the key for faster lookup
$v2Lookup = array_column($v2Data, null, 'IdPeriodo');
return array_map(function ($itemV1) use ($v2Lookup) {
if (isset($v2Lookup[$itemV1['IdPeriodo']])) {
$itemV2 = $v2Lookup[$itemV1['IdPeriodo']];
$mergedItem = array_merge($itemV1, $itemV2);
unset($mergedItem['NombreCarrera']); // Remove NombreCarrera as specified
return $mergedItem;
}
return $itemV1; // If no matching IdPeriodo found in v2, return the original v1 item
}, $this->get_v1());
}
}
final class Espacios extends WebServiceSGU
{
public function __construct()
{
parent::__construct("/catalogos/espacios/seleccionar");
}
}
final class Carreras extends WebServiceSGU
{
public function __construct()
{
parent::__construct("/catalogos/carreras/seleccionar");
}
}
final class Horarios extends WebServiceSGU
{
public function __construct()
{
parent::__construct(
"/seleccionar",
<<<JSON
{
"\$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["idPeriodo"],
"properties": {
"idPeriodo": {
"type": "integer",
"description": "Identificador del periodo a consultar."
},
"claveFacultad": {
"type": "string",
"description": "Clave de la facultad a consultar.",
"pattern": "^[a-zA-Z0-9]*$"
},
"claveCarrera": {
"type": "string",
"description": "Clave de la carrera a consultar.",
"pattern": "^[a-zA-Z0-9]*$"
},
"claveProfesor": {
"type": "string",
"description": "Clave del empleado a consultar.",
"pattern": "^[a-zA-Z0-9]*$"
},
"fecha": {
"type": "string",
"description": "Fecha de la clase.",
"pattern": "^\\d{4}-\\d{2}-\\d{2}$"
}
}
}
JSON
);
}
}

98
class/mailer.php Normal file
View File

@@ -0,0 +1,98 @@
<?php
//https://github.com/PHPMailer/PHPMailer
//require_once('../include/phpmailer/PHPMailerAutoload.php');
class Mailer{
private const FROM = "academia@lasalle.mx";
private const FROM_NAME = "Vicerrectoría Académica";
private const FROM_PASS = "D1s3c4nt3S1l1c4#$";//4c4d3m14S3gur4## Foy25193 D1s3c4nt3S1l1c4#$
private const FOOTER = "<p style='margin-top:5em; color:#aaa;font-style:italics'><small>Este es un correo automatizado, esta cuenta no recibe correos.<small></p>";
//private $lista_to, $asunto, $texto;
/**
* Función estática para mandar correos. Los destinatarios pueden ser arreglo o cadena separada por ; incluir: include/phpmailer/PHPMailerAutoload.php
*
* @param array|string $lista_to El destinatario o lista de destinatarios. Puede ser un arreglo de direcciones de correo electrónico o una cadena de texto con direcciones de correo separadas por ;.
* @param string $asunto El asunto del correo.
* @param string $texto El cuerpo del mensaje del correo en HTML.
* @param bool $bcc Indica si se debe enviar el correo como copia oculta (true) o no (false). Valor por defecto: false.
*
* @return bool True si el correo se envió exitosamente, false en caso contrario.
*/
public static function enviarCorreo($lista_to, $asunto, $texto, $bcc = false){
try{
//SMTP Settings
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'TLS';
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->Username = self::FROM;
$mail->Password = self::FROM_PASS;
$mail->SetFrom(self::FROM, self::FROM_NAME); //from (verified email address)
$mail->Subject = $asunto; //subject
$mail->IsHTML(true);
$mail->MsgHTML($texto.self::FOOTER);//adjunta footer
//recipient
if(is_array($lista_to)){
foreach($lista_to as $correo){
if(trim($correo)!="")
if($bcc)
$mail->addBCC($correo);
else
$mail->AddAddress($correo);
}
}else{//cadena de texto separada por ;
if(strpos($lista_to, ";")!==false){
$toArr = explode(";", $lista_to);
foreach($toArr as $correo){
if(trim($correo)!=""){
if($bcc)
$mail->addBCC($correo);
else
$mail->AddAddress($correo);
}
}
}elseif(strpos($lista_to, ",")!==false){
$toArr = explode(",", $lista_to);
foreach($toArr as $correo){
if(trim($correo)!=""){
if($bcc)
$mail->addBCC($correo);
else
$mail->AddAddress($correo);
}
}
}else{
if(trim($lista_to)!=""){
if($bcc)
$mail->addBCC($lista_to);
else
$mail->AddAddress($lista_to);
}
}
}
//Success
if ($mail->Send()) {
return true;
}else{
echo "Error al enviar correo";
return false;
}
}catch(phpmailerException $e){
echo $mail->ErrorInfo;
return false;
}catch(Exception $e2){
echo $mail->ErrorInfo;
return false;
}
return false;
}
}

View File

@@ -0,0 +1,38 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"required": [
"IdNivel",
"IdPeriodo",
"NombreNivel",
"NombrePeriodo"
],
"properties": {
"IdNivel": {
"type": "integer"
},
"IdPeriodo": {
"type": "integer"
},
"NombreNivel": {
"type": "string",
"enum": [
"LICENCIATURA",
"ESPECIALIDAD",
"MAESTRÍA",
"DOCTORADO",
"COORDINACIÓN DE EDUCACIÓN FÍSICA Y DEPORTES",
"COORDINACIÓN DE IMPULSO Y VIDA ESTUDIANTIL",
"COORDINACIÓN DE FORMACIÓN CULTURAL",
"VICERRECTORÍA DE BIENESTAR Y FORMACIÓN",
"CENTRO DE IDIOMAS"
]
},
"NombrePeriodo": {
"type": "string"
}
}
}
}