Skip to content
Open

s #102

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8920473
Add or update the Azure App Service build and deployment workflow config
cefireazure2505 May 28, 2025
c506050
Update index.php
cefireazure2505 May 28, 2025
04a84b6
Update index.php
cefireazure2505 May 28, 2025
86e6618
Create prueba_conexion.php
cefireazure2505 May 28, 2025
ef599e6
Update prueba_conexion.php
cefireazure2505 May 28, 2025
57e3b35
Update prueba_conexion.php
cefireazure2505 May 28, 2025
bd8e388
Update prueba_conexion.php
cefireazure2505 May 28, 2025
2c27a13
Update prueba_conexion.php
cefireazure2505 May 28, 2025
473c123
Rename prueba_conexion.php to conexion.php
cefireazure2505 May 28, 2025
d66afe7
Update conexion.php
cefireazure2505 May 28, 2025
8be135d
Update conexion.php
cefireazure2505 May 28, 2025
7473834
Update conexion.php
cefireazure2505 May 28, 2025
21d9574
Update conexion.php
cefireazure2505 May 28, 2025
dfed7cd
Update conexion.php
cefireazure2505 May 28, 2025
06bac53
Update conexion.php
cefireazure2505 May 28, 2025
f3d4204
Update conexion.php
cefireazure2505 May 28, 2025
5e2ef5b
Update conexion.php
cefireazure2505 May 28, 2025
88fd40c
Update conexion.php
cefireazure2505 May 28, 2025
195cc83
Update conexion.php
cefireazure2505 May 28, 2025
2fbcdb7
Create descomprimir.php
cefireazure2505 Jun 13, 2025
65b04a7
Create composer.json
cefireazure2505 Jun 13, 2025
7e6a7eb
Create storage.php
cefireazure2505 Jun 15, 2025
c30ff98
Update storage.php
cefireazure2505 Jun 15, 2025
5076b30
Delete .github/workflows directory
cefireazure2505 Jun 19, 2025
9aa1228
Update storage.php
cefireazure2505 Jun 23, 2025
cffd554
Update storage.php
cefireazure2505 Jun 24, 2025
a0591dd
Add or update the Azure App Service build and deployment workflow config
Jonman-JM Nov 28, 2025
7debaff
Update index.php
Jonman-JM Nov 28, 2025
34e32b5
Add or update the Azure App Service build and deployment workflow config
Jonman-JM Dec 12, 2025
cdf317c
Update index.php
Jonman-JM Dec 12, 2025
4419067
Create prueba_conexion.php
Jonman-JM Dec 12, 2025
f864347
Update index.php
Jonman-JM Dec 12, 2025
d16e2e2
Delete prueba_conexion.php
Jonman-JM Dec 12, 2025
f9434d0
Update index.php
Jonman-JM Dec 12, 2025
7c6e774
Create prueba_conexion.php
Jonman-JM Dec 12, 2025
e483d1e
Delete prueba_conexion.php
Jonman-JM Dec 12, 2025
fd31a05
Update conexion.php
Jonman-JM Dec 12, 2025
575b88e
Create storage.php
Jonman-JM Dec 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions .github/workflows/master_cefire-webapp0001.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
# More GitHub Actions for Azure: https://github.com/Azure/actions

name: Build and deploy PHP app to Azure Web App - cefire-webapp0001

on:
push:
branches:
- master
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read #This is required for actions/checkout

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Check if composer.json exists
id: check_files
uses: andstor/file-existence-action@v1
with:
files: 'composer.json'

- name: Run composer install if composer.json exists
if: steps.check_files.outputs.files_exists == 'true'
run: composer validate --no-check-publish && composer install --prefer-dist --no-progress


- name: Upload artifact for deployment job
uses: actions/upload-artifact@v4
with:
name: php-app
path: .

deploy:
runs-on: ubuntu-latest
needs: build
permissions:
id-token: write #This is required for requesting the JWT
contents: read #This is required for actions/checkout

steps:
- name: Download artifact from build job
uses: actions/download-artifact@v4
with:
name: php-app

- name: Login to Azure
uses: azure/login@v2
with:
client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_A3827D44356A4A7FA0ADB157B3B9C544 }}
tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_E4626EAF47E74041ACEC4737F46488CB }}
subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_A9D64345FFE446A5AB3F1A897C9FF8B2 }}

- name: 'Deploy to Azure Web App'
uses: azure/webapps-deploy@v3
id: deploy-to-webapp
with:
app-name: 'cefire-webapp0001'
slot-name: 'Production'
package: .

79 changes: 79 additions & 0 deletions blob/master/storage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
require 'vendor/autoload.php';

use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;

// Configuración
$connectionString = getenv('AZURE_STORAGE_CONNECTION_STRING');
$containerName = 'comprimidos'; // cambia esto por el nombre de tu contenedor

$blobClient = BlobRestProxy::createBlobService($connectionString);

// Eliminar archivo si se solicita
if (isset($_GET['delete'])) {
$blobToDelete = $_GET['delete'];
try {
$blobClient->deleteBlob($containerName, $blobToDelete);
echo "<p style='color:green;'>Archivo $blobToDelete eliminado correctamente.</p>";
} catch (ServiceException $e) {
echo "<p style='color:red;'>Error al eliminar: " . $e->getMessage() . "</p>";
}
}

// Subida de archivo ZIP
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['zipfile'])) {
$uploadedFile = $_FILES['zipfile'];

if ($uploadedFile['type'] !== 'application/zip') {
echo "<p style='color:red;'>Solo se permiten archivos ZIP.</p>";
} else {
$blobName = basename($uploadedFile['name']);
$content = fopen($uploadedFile['tmp_name'], "r");

try {
$blobClient->createBlockBlob($containerName, $blobName, $content);
echo "<p style='color:green;'>Archivo $blobName subido correctamente.</p>";
} catch (ServiceException $e) {
echo "<p style='color:red;'>Error al subir: " . $e->getMessage() . "</p>";
}
}
}

// Listar archivos
try {
$listOptions = new ListBlobsOptions();
$blobs = $blobClient->listBlobs($containerName, $listOptions);
} catch (ServiceException $e) {
die("Error al listar archivos: " . $e->getMessage());
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Gestor de archivos ZIP en Azure Blob</title>
</head>
<body>
<h1>Archivos ZIP en el contenedor <?= htmlspecialchars($containerName) ?></h1>

<ul>
<?php foreach ($blobs as $blob): ?>
<li>
<a href="<?= htmlspecialchars($blob->getUrl()) ?>" target="_blank">
<?= htmlspecialchars($blob->getName()) ?>
</a>
[<a href="?delete=<?= urlencode($blob->getName()) ?>"
onclick="return confirm('¿Eliminar este archivo?')">Eliminar</a>]
</li>
<?php endforeach; ?>
</ul>

<h2>Subir nuevo archivo ZIP</h2>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="zipfile" accept=".zip" required>
<button type="submit">Subir</button>
</form>
</body>
</html>
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"microsoft/azure-storage-blob": "^1.5"
}
}
39 changes: 39 additions & 0 deletions conexion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
// Recuperar variables de entorno
$dbHost = getenv('DB_HOST');
$dbName = "prueba";
$dbUser = getenv('DB_USER');
$dbPass = getenv('DB_PASSWORD');
if (!$dbHost || !$dbUser || $dbPass === false) {
throw new \RuntimeException('Faltan variables de entorno para la
conexión a la base de datos.');
}
// DSN con charset utf8mb4
$dsn = "mysql:host={$dbHost};dbname={$dbName};charset=utf8mb4";
try {
$options = [
// Excepciones en errores
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
// Fetch como array asociativo
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
// Desactivar emulación de prepares
PDO::ATTR_EMULATE_PREPARES => false,
// Asegurar la conexión TLS hacia Azure Database for MySQL
PDO::MYSQL_ATTR_SSL_CA =>
'/etc/ssl/certs/BaltimoreCyberTrustRoot.crt.pem',
// Desactivamos la validación del certificado SSL
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,
];
// Crear la conexión PDO
$pdo = new PDO($dsn, $dbUser, $dbPass, $options);
// Ejemplo: consulta sencilla
$stmt = $pdo->query('SELECT NOW() AS fecha_actual;');
$fila = $stmt->fetch();
echo "Conectado correctamente. Hora del servidor: " .
$fila['fecha_actual'];
} catch (PDOException $e) {
error_log('Error de conexión PDO: ' . $e->getMessage());
echo "Error al conectar con la base de datos: " .
htmlspecialchars($e->getMessage());
exit;
}
53 changes: 53 additions & 0 deletions descomprimir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

require 'vendor/autoload.php';

use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions;

$connectionString = getenv("AZURE_STORAGE_CONNECTION_STRING");
$sourceContainer = "comprimidos";
$targetContainer = "descomprimidos";

$blobClient = BlobRestProxy::createBlobService($connectionString);

// Opcional: Obtener el primer archivo ZIP del contenedor
$options = new ListBlobsOptions();
$options->setPrefix(""); // Sin prefijo específico
$blobs = $blobClient->listBlobs($sourceContainer, $options);

foreach ($blobs->getBlobs() as $blob) {
if (strtolower(pathinfo($blob->getName(), PATHINFO_EXTENSION)) !== "zip") {
continue;
}

echo "Procesando ZIP: " . $blob->getName() . PHP_EOL;

// Descargar el ZIP
$zipContent = $blobClient->getBlob($sourceContainer, $blob->getName())->getContentStream();
$tempZip = tempnam(sys_get_temp_dir(), 'zip');
file_put_contents($tempZip, stream_get_contents($zipContent));

$zip = new ZipArchive();
if ($zip->open($tempZip) === TRUE) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$entry = $zip->getNameIndex($i);
$fileContent = $zip->getFromIndex($i);
if ($fileContent !== false) {
$uploadOptions = new CreateBlockBlobOptions();
$blobClient->createBlockBlob($targetContainer, $entry, $fileContent, $uploadOptions);
echo "Extraído y subido: $entry" . PHP_EOL;
}
}
$zip->close();
} else {
echo "Error abriendo el ZIP: " . $blob->getName() . PHP_EOL;
}

unlink($tempZip);
break; // Solo procesamos el primer ZIP para este ejemplo
}

?>
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

echo "Hello World!";
echo "Hello World! J";
110 changes: 110 additions & 0 deletions storage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

require 'vendor/autoload.php';

use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;

ini_set('display_errors', 1);
error_reporting(E_ALL);

// Configuración
$connectionString = getenv("AZURE_STORAGE_CONNECTION_STRING");
$containerName = "comprimidos";

if (!$connectionString) {
die("La variable AZURE_STORAGE_CONNECTION_STRING no está configurada.");
}

$blobClient = BlobRestProxy::createBlobService($connectionString);

// Descargar archivo si se solicita
if (isset($_GET['download_blob'])) {
$blobName = $_GET['download_blob'];
try {
$blob = $blobClient->getBlob($containerName, $blobName);
$content = stream_get_contents($blob->getContentStream());

header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . basename($blobName) . '"');
header('Content-Length: ' . strlen($content));

echo $content;
exit;
} catch (Exception $e) {
http_response_code(500);
echo "Error al descargar el archivo: " . $e->getMessage();
exit;
}
}

// Eliminar archivo si se envió solicitud
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_blob'])) {
try {
$blobClient->deleteBlob($containerName, $_POST['delete_blob']);
echo "<p style='color:green;'>Archivo eliminado: {$_POST['delete_blob']}</p>";
} catch (Exception $e) {
echo "<p style='color:red;'>Error al eliminar: {$e->getMessage()}</p>";
}
}

// Subir archivo nuevo
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['zipfile'])) {
$file = $_FILES['zipfile'];
if ($file['error'] === UPLOAD_ERR_OK && mime_content_type($file['tmp_name']) === 'application/zip') {
$blobName = basename($file['name']);
try {
$content = fopen($file['tmp_name'], 'r');
$blobClient->createBlockBlob($containerName, $blobName, $content);
echo "<p style='color:green;'>Archivo subido: {$blobName}</p>";
} catch (Exception $e) {
echo "<p style='color:red;'>Error al subir: {$e->getMessage()}</p>";
}
} else {
echo "<p style='color:red;'>Solo se permiten archivos .zip válidos.</p>";
}
}

// Listar blobs
try {
$blobList = $blobClient->listBlobs($containerName, new ListBlobsOptions());
$blobs = $blobList->getBlobs();
} catch (Exception $e) {
die("Error al listar blobs: " . $e->getMessage());
}
?>

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Gestor de archivos ZIP en Azure Blob</title>
</head>
<body>
<h1>Archivos ZIP en '<?= htmlspecialchars($containerName) ?>'</h1>

<ul>
<?php if (empty($blobs)): ?>
<li>No hay archivos ZIP.</li>
<?php else: ?>
<?php foreach ($blobs as $blob): ?>
<li>
<a href="?download_blob=<?= urlencode($blob->getName()) ?>" target="_blank">
<?= htmlspecialchars($blob->getName()) ?>
</a>
<form method="POST" style="display:inline;" onsubmit="return confirm('¿Eliminar <?= htmlspecialchars($blob->getName()) ?>?')">
<input type="hidden" name="delete_blob" value="<?= htmlspecialchars($blob->getName()) ?>">
<button type="submit" style="color:red;">Eliminar</button>
</form>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>

<h2>Subir nuevo archivo ZIP</h2>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="zipfile" accept=".zip" required>
<button type="submit">Subir</button>
</form>
</body>
</html>