From 10363fc17d12efd16f2f500cbf7e024a22a3c8a8 Mon Sep 17 00:00:00 2001 From: "o.moresis" Date: Wed, 22 May 2024 16:21:46 +0300 Subject: [PATCH] initial --- .docker/caddy/Dockerfile | 8 ++++ .docker/config/Caddyfile | 9 +++++ .docker/php-fpm/Dockerfile | 55 +++++++++++++++++++++++++ .docker/php-fpm/Dockerfileold | 41 +++++++++++++++++++ .gitignore | 1 + composer.lock | 17 ++++---- docker-compose.yml | 65 ++++++++++++++++++++++++++++++ gulpfile.js | 9 ----- public/index.old | 3 ++ src/Config.php | 37 +++++++++-------- src/Controllers/AuthController.php | 8 ++-- src/Services/PgSql.php | 11 ++--- src/Services/PoseidonAPI.php | 4 +- src/Services/WfdAPI.php | 10 +++-- 14 files changed, 228 insertions(+), 50 deletions(-) create mode 100644 .docker/caddy/Dockerfile create mode 100644 .docker/config/Caddyfile create mode 100644 .docker/php-fpm/Dockerfile create mode 100644 .docker/php-fpm/Dockerfileold create mode 100644 docker-compose.yml delete mode 100644 gulpfile.js create mode 100644 public/index.old diff --git a/.docker/caddy/Dockerfile b/.docker/caddy/Dockerfile new file mode 100644 index 0000000..4e1229c --- /dev/null +++ b/.docker/caddy/Dockerfile @@ -0,0 +1,8 @@ +FROM caddy:latest + +# Ensure the www-data user and group match the host system +# RUN groupadd -g 1000 www-data && useradd -u 1000 -g www-data -s /bin/bash www-data + +# Set permissions for /var/www/html +# RUN chown -R www-data:www-data /var/www/html + diff --git a/.docker/config/Caddyfile b/.docker/config/Caddyfile new file mode 100644 index 0000000..f51488a --- /dev/null +++ b/.docker/config/Caddyfile @@ -0,0 +1,9 @@ +localhost { + root * /var/www/html/public + file_server + php_fastcgi php_backend:9000 + + log { + output file /var/log/caddy.log + } +} diff --git a/.docker/php-fpm/Dockerfile b/.docker/php-fpm/Dockerfile new file mode 100644 index 0000000..33f21c8 --- /dev/null +++ b/.docker/php-fpm/Dockerfile @@ -0,0 +1,55 @@ +FROM php:8.2-fpm-alpine + +# Install system dependencies and PostgreSQL libraries +RUN apk add --no-cache \ + libpng-dev \ + libjpeg-turbo-dev \ + freetype-dev \ + postgresql-dev \ + libpq \ + unzip \ + git + +# Configure and install PHP extensions +RUN docker-php-ext-configure gd --with-freetype --with-jpeg \ + && docker-php-ext-install -j$(nproc) gd pdo pdo_pgsql pgsql + +ENV REPO_URL= +ENV GEOSERVER_URL= +ENV GEOSERVER_USERNAME= +ENV GEOSERVER_PASSWORD= +ENV GEONETWORK_URL= +ENV GEONETWORK_API_KEY= +ENV GEONETWORK_API_VERSION= +ENV PG_USER= +ENV PG_PASS= +ENV PG_NAME= +ENV PG_HOST= + +# Install Composer +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer + +# Ensure the www-data user and group match the host system +# RUN addgroup -g 1000 www-data && adduser -u 1000 -G www-data -s /bin/sh -D www-data +# RUN addgroup -g 1000 nobody && adduser -u 1000 -G nobody -s /bin/sh -D nobody + +# Set permissions for /var/www/html +# RUN mkdir -p /var/www/html && chown -R www-data:www-data /var/www/html +RUN chown -R nobody:nobody /var/www/html /run + +# Copy project files +COPY --chown=nobody . /var/www/html/ + +# Switch to the www-data user +# USER www-data +USER nobody + +# Set the working directory +WORKDIR /var/www/html + +EXPOSE 9000 + +# Run Composer install +CMD sh -c "composer install --no-dev --optimize-autoloader && php-fpm" + +USER root diff --git a/.docker/php-fpm/Dockerfileold b/.docker/php-fpm/Dockerfileold new file mode 100644 index 0000000..ff39404 --- /dev/null +++ b/.docker/php-fpm/Dockerfileold @@ -0,0 +1,41 @@ +FROM php:8.2-fpm + +# EXPOSE 9000 +# Install any PHP extensions or packages you need +RUN apt-get update && apt-get install -y \ + git \ + curl \ + libpng-dev \ + libpq-dev \ + libjpeg-dev \ + libfreetype6-dev \ + zip \ + unzip \ + && docker-php-ext-configure gd \ + && docker-php-ext-install -j$(nproc) gd \ + && docker-php-ext-install pdo pdo_pgsql pgsql mbstring exif bcmath pcntl + +# Ensure the www-data user and group match the host system +# RUN groupadd -g 1000 www-data && useradd -u 1000 -g www-data -s /bin/bash +# RUN useradd -u 1000 -g www-data -s /bin/bash + +# Copy composer from the official image +COPY --from=composer /usr/bin/composer /usr/bin/composer + + +WORKDIR /var/www/html + +# ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/ +# Install PHP extensions +RUN chmod +x /usr/local/bin/install-php-extensions; \ + +# Set permissions for /var/www/html +COPY --chown=www-data:www-data . /var/www/html + +# Copy project files + +USER www-data + +# Run Composer install +CMD bash -c "composer install --no-dev --optimize-autoloader" + diff --git a/.gitignore b/.gitignore index 7a73857..76ac779 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .htaccess .env +.docker/data/* node_modules/ package-lock.json composer.phar diff --git a/composer.lock b/composer.lock index c3d13c5..b62eee7 100644 --- a/composer.lock +++ b/composer.lock @@ -68,16 +68,16 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.27.0", + "version": "v1.29.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "5bbc823adecdae860bb64756d639ecfec17b050a" + "reference": "ef4d7e442ca910c4764bce785146269b30cb5fc4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/5bbc823adecdae860bb64756d639ecfec17b050a", - "reference": "5bbc823adecdae860bb64756d639ecfec17b050a", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/ef4d7e442ca910c4764bce785146269b30cb5fc4", + "reference": "ef4d7e442ca910c4764bce785146269b30cb5fc4", "shasum": "" }, "require": { @@ -91,9 +91,6 @@ }, "type": "library", "extra": { - "branch-alias": { - "dev-main": "1.27-dev" - }, "thanks": { "name": "symfony/polyfill", "url": "https://github.com/symfony/polyfill" @@ -130,7 +127,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.27.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.29.0" }, "funding": [ { @@ -146,7 +143,7 @@ "type": "tidelift" } ], - "time": "2022-11-03T14:55:06+00:00" + "time": "2024-01-29T20:11:03+00:00" }, { "name": "vlucas/phpdotenv", @@ -233,5 +230,5 @@ "prefer-lowest": false, "platform": [], "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..401cbef --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,65 @@ +version: '3' + +services: + caddy: + container_name: spa_caddy + build: .docker/caddy + ports: + - 80:80 + - 443:443 + volumes: + - ./:/var/www/html + - .docker/config/Caddyfile:/etc/caddy/Caddyfile + - .docker/data/logs/caddy:/var/log/caddy:delegated + depends_on: + - php-fpm + networks: + - internal + + php-fpm: + container_name: php_backend + build: + context: .docker/php-fpm + volumes: + - ./:/var/www/html:delegated + - .docker/data/logs/php:/var/log/php:delegated + env_file: + - .env + environment: + - REPO_URL= + - GEOSERVER_URL=geoserver_service + - GEOSERVER_USERNAME=admin + - GEOSERVER_PASSWORD=myawesomepw123 + - GEONETWORK_URL= + - GEONETWORK_API_KEY= + - GEONETWORK_API_VERSION= + - PG_USER= + - PG_PASS= + - PG_NAME= + - PG_HOST= + restart: unless-stopped + expose: + - 9000 + networks: + - internal + + geoserver: + container_name: geoserver_service + image: kartoza/geoserver + environment: + - GEOSERVER_ADMIN_USER=admin + - GEOSERVER_ADMIN_PASSWORD=myawesomepw123 + volumes: + - geoserver_data:/opt/geoserver/data_dir + expose: + - 8081 + ports: + - "8081:8081" + networks: + - internal + +volumes: + geoserver_data: +networks: + internal: + diff --git a/gulpfile.js b/gulpfile.js deleted file mode 100644 index 87c8e54..0000000 --- a/gulpfile.js +++ /dev/null @@ -1,9 +0,0 @@ -gulp.task('modules', function() { - sources = [ - './node_modules/prismjs/prism.js', - './node_modules/prismjs/themes/prism-dark.css', - ] - gulp.src( sources ).pipe(gulp.dest('./public/modules/')); -}); - -gulp.task('copy-modules', ['modules']); diff --git a/public/index.old b/public/index.old new file mode 100644 index 0000000..cf60860 --- /dev/null +++ b/public/index.old @@ -0,0 +1,3 @@ + diff --git a/src/Config.php b/src/Config.php index cd94f26..6fd87cc 100644 --- a/src/Config.php +++ b/src/Config.php @@ -14,15 +14,22 @@ require_once __DIR__.'/../vendor/autoload.php'; /** * Init DotEnv. */ -$dotEnv = new \Dotenv\Dotenv(BASE_PATH); -$dotEnv->load(); - +// $dotEnv = new \Dotenv\Dotenv(BASE_PATH); +// $dotEnv->load(); +if (file_exists(BASE_PATH . '/.env')) { + /** + * Init DotEnv. + */ + $dotEnv = new \Dotenv\Dotenv(BASE_PATH); + $dotEnv->load(); +} class Config { const LINE_READ_LIMIT = 500; const BYTE_READ_LIMIT = 450000000; const MODE = 1; - const API_BASE_URL = "https://geoserver.hcmr.gr/geoserver/rest/"; + // const API_BASE_URL = "https://geoserver.hcmr.gr/geoserver/rest/"; + // const API_BASE_URL = getenv("https://geoserver.hcmr.gr/geoserver/rest/"); const API_CREDENTIALS = ["portal", "@dm!n322"]; /* const REPO_API = "http://msfd-repo.ath.hcmr.gr"; */ const API_URL = array( @@ -41,13 +48,6 @@ class Config { "WFD" => "" ); - const DB_CREDS = array( - "HOST" => "192.168.11.45", - "NAME" => "newschema", - "USER" => "vaslak", - "PASS" => "postgre2020" - ); - // Header translations const HEADER_TRANSLATIONS = [ 'Cruise' => 'cruise', @@ -94,15 +94,20 @@ class Config { public static function getDatabaseCreds($mode = self::MODE) { - return array(self::DB_CREDS['HOST'], self::DB_CREDS['NAME'], self::DB_CREDS['USER'], self::DB_CREDS['PASS']); + $host = getenv("DB_HOST") ?: "192.168.11.45"; + $name = getenv("DB_NAME") ?: "newschema"; + $user = getenv("DB_USER") ?: "vaslak"; + $pass = getenv("DB_PASS") ?: "postgre2020"; + + return array($host, $name, $user, $pass); //self::DB_CREDS['USER'], self::DB_CREDS['PASS']); } // GEOSERVER API CONSTANTS - public static function getAPIBaseURL($api_name) { - return self::API_URL[$api_name]; - } - + // public static function getAPIBaseURL($api_name) { + // return self::API_URL[$api_name]; + // } + // public static function getAPICredentials($api_name) { diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php index 59b5e14..5ae3003 100644 --- a/src/Controllers/AuthController.php +++ b/src/Controllers/AuthController.php @@ -30,10 +30,10 @@ class AuthController { // $res = $api->getUser($user_id); $sql_groups = "SELECT group_id FROM webapp.users u JOIN webapp.users__groups ug ON u.geo_id = ug.user_id - WHERE geo_id = '${user_id}'"; + WHERE geo_id = '{$user_id}'"; $user_groups = Pgsql::getColValues($sql_groups); $sql_name = "SELECT fname FROM webapp.users u - WHERE geo_id = '${user_id}'"; + WHERE geo_id = '{$user_id}'"; $user_name = PgSql::getCol($sql_name); $_SESSION['user_id'] = $user_id; @@ -110,7 +110,7 @@ class AuthController { private function insertGuestUser($email) { $sql = "INSERT INTO webapp.users (email, origin, password) - VALUES ('${email}', 'GUEST', '') + VALUES ('{$email}', 'GUEST', '') ON CONFLICT (email) DO UPDATE SET last_login = now()"; @@ -120,7 +120,7 @@ class AuthController { private function insertGuestGroup($id) { $sql = "INSERT INTO webapp.users__groups (user_id, group_id) - VALUES ('${id}', 1) ON CONFLICT DO NOTHING"; + VALUES ('{$id}', 1) ON CONFLICT DO NOTHING"; PgSql::insert($sql, 'NULL'); } diff --git a/src/Services/PgSql.php b/src/Services/PgSql.php index 90aaadf..624655a 100644 --- a/src/Services/PgSql.php +++ b/src/Services/PgSql.php @@ -12,9 +12,12 @@ class PgSql public static $error; - public function close() + public static function close() { - pg_close(self::$db); + if (self::$db) { + pg_close(self::$db); + self::$db = null; + } } @@ -23,7 +26,6 @@ class PgSql self::$db = pg_connect("host={$dbhost} dbname={$dbname} user={$dbuser} password={$dbpass}"); } - // For SELECT // Returns one row as object public static function getRow($sql) @@ -34,7 +36,6 @@ class PgSql return $row; } - // For SELECT // Returns an array of row objects // Gets number of rows @@ -108,7 +109,7 @@ class PgSql $excluded_columns = implode(',', $exc); if ($ret != 'NULL') { $sql = rtrim($sql, ';'); - $sql .= "\nON CONFLICT (${conf}) DO UPDATE + $sql .= "\nON CONFLICT ({$conf}) DO UPDATE SET "; $sql .= ' RETURNING '; $sql .= implode(',', $ret); diff --git a/src/Services/PoseidonAPI.php b/src/Services/PoseidonAPI.php index 9c28267..ed02a4a 100644 --- a/src/Services/PoseidonAPI.php +++ b/src/Services/PoseidonAPI.php @@ -19,8 +19,8 @@ class PoseidonAPI extends API { $url = Config::getAPIbaseURL($this->apiName); parent::__construct($url); - $this->username = "y3wvvYKquktkZZALk49utvYeJPikNQFRWzE7A4Wx"; - $this->password = "OXR6RfTbp1UuFHwGAyBeolIYzoVfGP69UBd3KXmVzO1B3gW6Ck9yt2jCipAJT5DCWnhBMcB5sx0EUPEcfy8dcGZjinUyRphLSCIQ72NrdkmiY2lhIv2utjUSq1t3B0pD"; + $this->username = Config::getAPIusername($this->apiName); + $this->password = Config::getAPIpassword($this->apiName); } // POST request to get access token diff --git a/src/Services/WfdAPI.php b/src/Services/WfdAPI.php index a586ff3..1de931b 100644 --- a/src/Services/WfdAPI.php +++ b/src/Services/WfdAPI.php @@ -17,8 +17,10 @@ class WfdAPI extends API { $url = Config::getAPIbaseURL($this->apiName); parent::__construct($url); - $this->username = "ct_api_user"; - $this->password = "@p1u$3rct"; + $this->username = Config::getAPIusername($this->apiName); + $this->password = Config::getAPIpassword($this->apiName); + $this->client_id = Config::getAPIclientID($this->apiName); + $this->client_secret = Config::getAPIclientSecret($this->apiName); } // POST request to get access token @@ -26,8 +28,8 @@ class WfdAPI extends API { $endpoint = "token"; /* $authStr = base64_encode($this->username.":".$this->password); */ $data[] = 'grant_type=password'; - $data[] = 'client_id=watermonitoring'; - $data[] = 'client_secret=8067649c-c5dc-4bf3-922a-d10f44413d68'; + $data[] = "client_id=i{$this->client_id}"; + $data[] = "client_secret={$this->client_secret}"; $data[] = "username={$this->username}"; $data[] = "password={$this->password}"; /* $this->headers["Accept"] = "x-www-form-urlencoded"; */