From ca52d3b24824a55f57d09dc73b24a85d5caff806 Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Fri, 31 Jan 2025 09:25:18 +0100 Subject: [PATCH] Update API endpoints, add wash certificate handling Replaced legacy 'nnks.truckwash.dk' endpoint with 'api.truckwash.dk' across the codebase for consistency. Implemented a feature to handle certificate downloads, including file serving and validation. Updated PHP configuration to support additional dependencies like PHPMailer, and introduced necessary route and configuration changes for wash certificate processing. --- services/backup/backup.sh | 3 ++- services/nginx/app/classes/redis.php | 5 ++-- .../app/classes/wash_certificate_store.php | 15 ++++++++++++ services/nginx/app/file_server.php | 23 +++++++++++++++++++ services/nginx/app/index.php | 6 +++++ .../nginx/app/routes/washCertificateRoute.php | 23 +++++++++++++++++++ services/nginx/nginx.conf | 22 ++++++++++++++---- services/php/Dockerfile | 13 +++++++++-- 8 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 services/nginx/app/file_server.php create mode 100644 services/nginx/app/routes/washCertificateRoute.php diff --git a/services/backup/backup.sh b/services/backup/backup.sh index 9210f6ef..32b86845 100644 --- a/services/backup/backup.sh +++ b/services/backup/backup.sh @@ -2,6 +2,7 @@ #echo "Backup script started at $(date)" #echo "Auth key: $auth_key" # Curl the backup script -curl -s "https://nnks.truckwash.dk/?internalCronCall=true&script=run&action=cron&auth_key=$auth_key" +# shellcheck disable=SC2154 +curl -s "https://api.truckwash.dk/?internalCronCall=true&script=run&action=cron&auth_key=$auth_key" # Show the script has finished #echo "Backup script finished at $(date)" \ No newline at end of file diff --git a/services/nginx/app/classes/redis.php b/services/nginx/app/classes/redis.php index 9635663b..25128d7d 100644 --- a/services/nginx/app/classes/redis.php +++ b/services/nginx/app/classes/redis.php @@ -105,12 +105,13 @@ class redis implements redis_i public function get_department_name(int $department_id): string|null { // Get the department name - $tmp_department = (object)$this->get('department_' . $department_id) ?? null; + $tmp_department = $this->get('department_' . $department_id) ?? null; if ($tmp_department === 'IS_EMPTY_OR_NULL' || $tmp_department === null) { return null; } + $tmp_department = json_decode($tmp_department); // Return the department name - return $tmp_department->name; + return $tmp_department->name ?: $tmp_department['name'] ?? null; } /** diff --git a/services/nginx/app/classes/wash_certificate_store.php b/services/nginx/app/classes/wash_certificate_store.php index c4f8c95e..e18c2349 100644 --- a/services/nginx/app/classes/wash_certificate_store.php +++ b/services/nginx/app/classes/wash_certificate_store.php @@ -31,4 +31,19 @@ class wash_certificate_store implements minio_wash_certificates_i { return self::getPresignedUrl('wash_certificate_' . $id . '.pdf'); } + + /** + * @param string $file + * @return string The path to the downloaded file + */ + public function download(string $file): string + { + $path = '/tmp/' . $file; + $result = self::getS3Client()->getObject([ + 'Bucket' => self::getBucket(), + 'Key' => $file, + 'SaveAs' => $path + ]); + return $path; + } } \ No newline at end of file diff --git a/services/nginx/app/file_server.php b/services/nginx/app/file_server.php new file mode 100644 index 00000000..9121fb04 --- /dev/null +++ b/services/nginx/app/file_server.php @@ -0,0 +1,23 @@ +washCertificateExists($file)) { + header('HTTP/1.1 404 Not Found'); + exit; +} + +// Download the certificate from the store to /tmp +$certificate_path = $wash_certificate_store->download($file); + +// Send the certificate to the client +header('Content-Type: application/pdf'); +header('Content-Disposition: inline; filename="' . $file . '"'); +header('Content-Length: ' . filesize($certificate_path)); +readfile($certificate_path); +exit; \ No newline at end of file diff --git a/services/nginx/app/index.php b/services/nginx/app/index.php index cc44cf2c..9d4e3410 100644 --- a/services/nginx/app/index.php +++ b/services/nginx/app/index.php @@ -102,6 +102,12 @@ if (php_sapi_name() === 'cli' || isset($_GET['internalCronCall'])) { exit; } +// If the route ends with .php, then require the file_server.php +if (str_contains($_SERVER['REQUEST_URI'], '.pdf')) { + require_once 'file_server.php'; + exit; +} + // Autoload all the routes $router->auto_load_routes(WD . '/routes'); diff --git a/services/nginx/app/routes/washCertificateRoute.php b/services/nginx/app/routes/washCertificateRoute.php new file mode 100644 index 00000000..43fcbcc4 --- /dev/null +++ b/services/nginx/app/routes/washCertificateRoute.php @@ -0,0 +1,23 @@ +get('/modules/washcertificates', function () { + // Set the working directory to /modules/washcertificates, + // so the output directory is created in the correct location + chdir(WD . '/modules/washcertificates'); + // Run the index.php file + require_once 'index.php'; + exit(); + }); + } +} \ No newline at end of file diff --git a/services/nginx/nginx.conf b/services/nginx/nginx.conf index 483dbc0a..7cde20ea 100644 --- a/services/nginx/nginx.conf +++ b/services/nginx/nginx.conf @@ -22,22 +22,24 @@ http { # Include server block configurations include /etc/nginx/conf.d/*.conf; + # Redirect HTTP to HTTPS server { listen 80; - server_name nnks.truckwash.dk; + server_name api.truckwash.dk; return 301 https://$host$request_uri; } server { listen 443 ssl; - server_name nnks.truckwash.dk; + server_name api.truckwash.dk; + # SSL certificates - ssl_certificate /etc/letsencrypt/live/nnks.truckwash.dk/fullchain.pem; - ssl_certificate_key /etc/letsencrypt/live/nnks.truckwash.dk/privkey.pem; + ssl_certificate /etc/letsencrypt/live/api.truckwash.dk/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/api.truckwash.dk/privkey.pem; # Enable SSL options ssl_protocols TLSv1.2 TLSv1.3; @@ -55,8 +57,18 @@ http { # Location block for PHP files - location ^~ / { + add_header Access-Control-Allow-Methods "GET, POST, OPTIONS"; + add_header Access-Control-Allow-Headers "Authorization, Content-Type, X-Requested-With"; + add_header Access-Control-Allow-Credentials true; + + if ($request_method = 'OPTIONS') { + add_header Access-Control-Allow-Origin *; + add_header Access-Control-Allow-Methods "GET, POST, OPTIONS"; + add_header Access-Control-Allow-Headers "Authorization, Content-Type, X-Requested-With"; + return 204; + } + include fastcgi_params; fastcgi_pass php:9000; fastcgi_index index.php; diff --git a/services/php/Dockerfile b/services/php/Dockerfile index a5e338da..3944b515 100644 --- a/services/php/Dockerfile +++ b/services/php/Dockerfile @@ -14,10 +14,10 @@ RUN apt-get update && apt-get install -y \ libonig-dev \ libxml2-dev \ zip \ - unzip \ git \ curl \ - && docker-php-ext-install mbstring exif pcntl bcmath gd pdo_mysql mysqli + libzip-dev \ + && docker-php-ext-install mbstring exif pcntl bcmath gd pdo_mysql mysqli zip # Copy application files into the container COPY /nginx/app /var/www/html @@ -26,9 +26,18 @@ COPY /nginx/app /var/www/html RUN chown -R www-data:www-data /var/www/html \ && chmod -R 755 /var/www/html +# Require the phpmailer/phpmailer package +RUN composer require phpmailer/phpmailer + # Install using Composer RUN composer install --no-dev +# Install the /var/www/html/modules/washcertificates directory +WORKDIR /var/www/html/modules/washcertificates +RUN composer install --no-dev + +WORKDIR /var/www/html + # Expose port 9000 EXPOSE 9000