181 lines
6.9 KiB
PHP
181 lines
6.9 KiB
PHP
<!-- ************************** -->
|
|
<!-- Examen práctico - UF1844 -->
|
|
<!-- ALUMNO: MARCOS LOPEZ GOMEZ -->
|
|
<!-- ************************** -->
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<link rel="stylesheet" type="text/css" href="estilos.css" />
|
|
<title>Reserva alquiler!!!</title>
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<?php
|
|
// Validar el método de solicitud HTTP
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$datosReserva = recopilarDatosReserva($_POST, ['dias', 'fecha_inicio', 'tipo', 'silla', 'gps', 'riesgo', 'menor', 'total', 'nombre', 'email', 'telefono', 'dni', 'info', 'entrega', 'recargo']);
|
|
$resultadoProcesamiento = procesarReserva($datosReserva);
|
|
mostrarResultado($resultadoProcesamiento);
|
|
} else {
|
|
echo "<h2>No se han recibido datos del formulario</h2>";
|
|
}
|
|
|
|
|
|
// Función para recopilar datos del formulario y sanitizarlos
|
|
function recopilarDatosReserva($formulario, $campos)
|
|
{
|
|
$datos = [];
|
|
foreach ($campos as $campo) {
|
|
if (isset($_POST[$campo])) {
|
|
$datos[$campo] = $formulario[$campo];
|
|
} else {
|
|
$datos[$campo] = "";
|
|
}
|
|
}
|
|
return $datos;
|
|
}
|
|
|
|
|
|
// Función para procesar la reserva
|
|
function procesarReserva($datosReserva)
|
|
{
|
|
// Variables para el resultado del procesamiento
|
|
$exito = true;
|
|
$msgExito = [];
|
|
$msgError = [];
|
|
|
|
$dias = $datosReserva['dias'];
|
|
$fecha_inicio = $datosReserva['fecha_inicio'];
|
|
$tipo = $datosReserva['tipo'];
|
|
$silla = $datosReserva['silla'] ? "Seleccionado" : "No seleccionado";
|
|
$gps = $datosReserva['gps'] ? "Seleccionado" : "No seleccionado";
|
|
$riesgo = $datosReserva['riesgo'] ? "Seleccionado" : "No seleccionado";
|
|
$menor = $datosReserva['menor'] ? "Seleccionado" : "No seleccionado";
|
|
$total = $datosReserva['total'];
|
|
$nombre = $datosReserva['nombre'];
|
|
$email = $datosReserva['email'];
|
|
$telefono = $datosReserva['telefono'];
|
|
$dni = $datosReserva['dni'];
|
|
$info = $datosReserva['info'];
|
|
$entrega = $datosReserva['entrega'];
|
|
$recargo = $datosReserva['recargo'];
|
|
|
|
// Generar identificador de reserva
|
|
$idReserva = time() . '_' . mt_rand(1000, 9999);
|
|
// Fecha de reserva
|
|
$fechaReserva = date("d-m-Y H:i:s");
|
|
|
|
$fichaReserva = "Id alquiler: $idReserva \r\n";
|
|
$fichaReserva .= "Fecha de alquiler: $fechaReserva \r\n";
|
|
$fichaReserva .= "Fecha de inicio: $fecha_inicio \r\n";
|
|
$fichaReserva .= "Tipo de coche: $tipo \r\n";
|
|
$fichaReserva .= "Gps: $gps \r\n";
|
|
$fichaReserva .= "Silla: $silla \r\n";
|
|
$fichaReserva .= "Todo riesgo: $riesgo \r\n";
|
|
$fichaReserva .= "Menor de 30: $menor \r\n";
|
|
$fichaReserva .= "Duración del alquiler: $dias \r\n";
|
|
$fichaReserva .= "Coste Total: $total \r\n";
|
|
$fichaReserva .= "Nombre: $nombre \r\n";
|
|
$fichaReserva .= "Correo electrónico: $email \r\n";
|
|
$fichaReserva .= "Teléfono: $telefono \r\n";
|
|
$fichaReserva .= "DNI: $dni \r\n";
|
|
|
|
|
|
// Enviar correo electrónico con los detalles de la reserva
|
|
$destinatario = "appasin04@gmail.com";
|
|
$asunto = "Informacion de reserva - $idReserva";
|
|
$headers = 'Reply-To: appasin04@gmail.com' . "\r\n";
|
|
$headers .= 'Bcc: ' . $email . "\r\n";
|
|
if (!mail($destinatario, $asunto, $fichaReserva, $headers)) {
|
|
$exito = false;
|
|
$msgError[] = "Error al enviar el correo electrónico.";
|
|
}
|
|
|
|
|
|
// Guardar la ficha de reserva en un archivo
|
|
$dir_reservas = "alquileres/";
|
|
!is_dir($dir_reservas) ? !mkdir($dir_reservas, 0777, true) : null;
|
|
$nombre_archivo = $dir_reservas . "alquiler_" . $idReserva . ".txt";
|
|
if (file_put_contents($nombre_archivo, $fichaReserva) === false) {
|
|
$exito = false;
|
|
$msgError[] = "Error al guardar la ficha de reserva.";
|
|
} else {
|
|
$msgExito[] = "<h2>Reserva de alquiler confirmada!!!!</h2> " .
|
|
"<p>". nl2br($info) . "</p>" .
|
|
"<h4> Esta es la informacion detallada de tu reserva ! </h4>" . "<p>" . nl2br($fichaReserva) . "</p>";
|
|
}
|
|
|
|
// Añadir al listado_reservas.txt una línea de reserva
|
|
$registroReserva = implode('/_/', [$idReserva, $nombre, $email, $telefono, $fecha_inicio, $fechaReserva, $dias, $tipo]) . "\r\n";
|
|
$nombre_archivo = "listado_reservas.txt";
|
|
if (file_put_contents($nombre_archivo, $registroReserva, FILE_APPEND | LOCK_EX) === false) {
|
|
$exito = false;
|
|
$msgError[] = "Error al guardar la lista de reservas.";
|
|
}
|
|
|
|
if (isset($_FILES['dni_archivo']) && $_FILES['dni_archivo']['error'] === UPLOAD_ERR_OK) {
|
|
$exitoUpload = true;
|
|
$dir_uploads = "dni_clientes/";
|
|
!is_dir($dir_uploads) ? !mkdir($dir_uploads, 0777, true) : null;
|
|
$nombreArchivo = implode("_", [$dni, $idReserva, $_FILES['dni_archivo']['name']]);
|
|
$extension_archivo = strtolower(pathinfo($_FILES["dni_archivo"]["name"], PATHINFO_EXTENSION));
|
|
$tamano_maximo = 1 * 1024 * 1024;
|
|
$extensiones_permitidas = array("jpg", "jpeg", "pdf");
|
|
|
|
if (!in_array($extension_archivo, $extensiones_permitidas)) {
|
|
$exitoUpload = false;
|
|
$exito = false;
|
|
$msgError[] = "El fichero no tiene una extension valida.";
|
|
}
|
|
|
|
if ($_FILES["dni_archivo"]["size"] > $tamano_maximo) {
|
|
$exitoUpload = false;
|
|
$exito = false;
|
|
$msgError[] = "El fichero excede el tamaño maximo (1Mb).";
|
|
}
|
|
|
|
if ($exitoUpload) {
|
|
if (move_uploaded_file($_FILES["dni_archivo"]["tmp_name"], $dir_uploads . $nombreArchivo)) {
|
|
$msgExito[] = "<h4>El fichero adjunto se almaceno correctamente</h4>";
|
|
} else {
|
|
$exito = false;
|
|
$msgError[] = "Ocurrio un error al almacenar el fichero adjunto";
|
|
}
|
|
}
|
|
}
|
|
|
|
$resultado = [
|
|
'exito' => $exito,
|
|
'msgExito' => $msgExito,
|
|
'msgError' => $msgError
|
|
];
|
|
return $resultado;
|
|
}
|
|
|
|
function mostrarResultado($resultado)
|
|
{
|
|
echo "<div>";
|
|
foreach ($resultado['msgExito'] as $key => $value) {
|
|
echo "<p> $value </p>";
|
|
}
|
|
if (!$resultado['exito']) {
|
|
echo "<h4>Se han encontrado los siguientes problemas al procesar su solicitud:</h4>";
|
|
echo "<ul>";
|
|
foreach ($resultado['msgError'] as $key => $value) {
|
|
echo "<li> $value </li>";
|
|
}
|
|
echo "</ul>";
|
|
}
|
|
echo "</div>";
|
|
}
|
|
|
|
?>
|
|
|
|
</body>
|
|
|
|
</html>
|