From 1744754dd4bde7c2207b8fccd1be7ba99b76efc5 Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 13:47:32 +0100 Subject: [PATCH 01/13] Hide password and host to the database by updating .drone.yaml --- .drone.yml | 19 +++++++++++++- .idea/dafl_music.iml | 9 ++++++- .idea/php.xml | 6 +++++ Sources/php_script/script/config.php | 8 +++--- Sources/php_script/script/db.php | 6 ++--- Sources/php_script/script/distance.php | 34 ++++++++++++-------------- Sources/php_script/script/index.php | 5 ---- 7 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 .idea/php.xml delete mode 100644 Sources/php_script/script/index.php diff --git a/.drone.yml b/.drone.yml index 7c91da7..4d161c3 100644 --- a/.drone.yml +++ b/.drone.yml @@ -74,4 +74,21 @@ steps: CONTAINERNAME: php_script COMMAND: create OVERWRITE: true - depends_on: [ php_script ] \ No newline at end of file + depends_on: [ php_script ] + +#database container +- name: deploy-php-database + image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest + environment: + IMAGENAME: innodb + CONTAINERNAME: location_php_database + COMMAND: create + PRIVATE: true + CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST: + from_secret: db_host + CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE: + from_secret: db_database + CODEFIRST_CLIENTDRONE_ENV_INNODB_USER: + from_secret: db_user + CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD: + from_secret: db_password \ No newline at end of file diff --git a/.idea/dafl_music.iml b/.idea/dafl_music.iml index 9f5b22b..7d01c35 100644 --- a/.idea/dafl_music.iml +++ b/.idea/dafl_music.iml @@ -1,5 +1,10 @@ + + + + + @@ -7,7 +12,9 @@ - + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..97a38d7 --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/Sources/php_script/script/config.php b/Sources/php_script/script/config.php index 63484ca..68a9e2b 100644 --- a/Sources/php_script/script/config.php +++ b/Sources/php_script/script/config.php @@ -1,8 +1,8 @@ meters($lat1, $lng1, $lat2, $lng2); + $dist = meters($lat1, $lng1, $lat2, $lng2); if ($dist <= 100) { $listUser[] = ['user' => $userID, 'music' => $idMusic]; } } diff --git a/Sources/php_script/script/index.php b/Sources/php_script/script/index.php deleted file mode 100644 index 53b1ece..0000000 --- a/Sources/php_script/script/index.php +++ /dev/null @@ -1,5 +0,0 @@ - \ No newline at end of file From d47058c023d7efecf2767b7c2d4e04c5c9cc78bc Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 13:50:13 +0100 Subject: [PATCH 02/13] .drone.yaml update --- .drone.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.drone.yml b/.drone.yml index 4d161c3..c969bfb 100644 --- a/.drone.yml +++ b/.drone.yml @@ -81,7 +81,7 @@ steps: image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest environment: IMAGENAME: innodb - CONTAINERNAME: location_php_database + CONTAINERNAME: deploy-php COMMAND: create PRIVATE: true CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST: @@ -91,4 +91,5 @@ steps: CODEFIRST_CLIENTDRONE_ENV_INNODB_USER: from_secret: db_user CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD: - from_secret: db_password \ No newline at end of file + from_secret: db_password + depends_on : [ deploy-php ] \ No newline at end of file From 3dba7ab4c46484077513bf7d6d98ac466c5686a2 Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 13:54:26 +0100 Subject: [PATCH 03/13] .drone.yaml update --- .drone.yml | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/.drone.yml b/.drone.yml index c969bfb..697d39d 100644 --- a/.drone.yml +++ b/.drone.yml @@ -73,23 +73,13 @@ steps: IMAGENAME: hub.codefirst.iut.uca.fr/dorian.hodin/dafl_music:latest CONTAINERNAME: php_script COMMAND: create - OVERWRITE: true - depends_on: [ php_script ] - -#database container -- name: deploy-php-database - image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest - environment: - IMAGENAME: innodb - CONTAINERNAME: deploy-php - COMMAND: create PRIVATE: true CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST: from_secret: db_host - CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE: - from_secret: db_database - CODEFIRST_CLIENTDRONE_ENV_INNODB_USER: - from_secret: db_user - CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD: - from_secret: db_password - depends_on : [ deploy-php ] \ No newline at end of file + CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE: + from_secret: db_database + CODEFIRST_CLIENTDRONE_ENV_INNODB_USER: + from_secret: db_user + CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD: + from_secret: db_password + depends_on: [ php_script ] From 2fac1230529585777002ebc0b0cd754b17f2ed2d Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 14:19:11 +0100 Subject: [PATCH 04/13] .drone.yaml update and getting the environement variables in the pho script --- .drone.yml | 12 ++++++------ Sources/php_script/script/config.php | 14 ++++---------- Sources/php_script/script/db.php | 6 +++--- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/.drone.yml b/.drone.yml index 697d39d..c5f05ea 100644 --- a/.drone.yml +++ b/.drone.yml @@ -76,10 +76,10 @@ steps: PRIVATE: true CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST: from_secret: db_host - CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE: - from_secret: db_database - CODEFIRST_CLIENTDRONE_ENV_INNODB_USER: - from_secret: db_user - CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD: - from_secret: db_password + CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE: + from_secret: db_database + CODEFIRST_CLIENTDRONE_ENV_INNODB_USER: + from_secret: db_user + CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD: + from_secret: db_password depends_on: [ php_script ] diff --git a/Sources/php_script/script/config.php b/Sources/php_script/script/config.php index 68a9e2b..f7d853b 100644 --- a/Sources/php_script/script/config.php +++ b/Sources/php_script/script/config.php @@ -1,14 +1,8 @@ Date: Wed, 23 Nov 2022 15:44:59 +0100 Subject: [PATCH 05/13] .drone.yaml update and getting the environement variables in the pho script --- Sources/php_script/script/config.php | 2 +- Sources/php_script/script/db.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/php_script/script/config.php b/Sources/php_script/script/config.php index f7d853b..3976c03 100644 --- a/Sources/php_script/script/config.php +++ b/Sources/php_script/script/config.php @@ -1,6 +1,6 @@ Date: Wed, 23 Nov 2022 16:25:10 +0100 Subject: [PATCH 06/13] .drone.yaml update --- .drone.yml | 4 ++++ Sources/php_script/script/db.php | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index c5f05ea..064a0f8 100644 --- a/.drone.yml +++ b/.drone.yml @@ -82,4 +82,8 @@ steps: from_secret: db_user CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD: from_secret: db_password + - CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST = $$DAFL_HOST + - CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE = $$DAFL_DATABASE + - CODEFIRST_CLIENTDRONE_ENV_INNODB_USER = $$DAFL_USER + - CODEFIRST_CLIENTDRONE_ENV_INNODB_PASS = $$DAFL_PASSWORD depends_on: [ php_script ] diff --git a/Sources/php_script/script/db.php b/Sources/php_script/script/db.php index c747177..70eecc0 100644 --- a/Sources/php_script/script/db.php +++ b/Sources/php_script/script/db.php @@ -1,7 +1,14 @@ $value) +{ + echo "$key => $value \n"; +} +echo "\n";echo "\n";echo "\n"; try{ $db = new PDO ($dns, $user, $password); }catch( PDOException $e){ From 889a6a4fb98b309f9523d324968639b9b6e63d61 Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 16:25:41 +0100 Subject: [PATCH 07/13] .drone.yaml update --- .drone.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.drone.yml b/.drone.yml index 064a0f8..058637c 100644 --- a/.drone.yml +++ b/.drone.yml @@ -82,8 +82,8 @@ steps: from_secret: db_user CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD: from_secret: db_password - - CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST = $$DAFL_HOST - - CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE = $$DAFL_DATABASE - - CODEFIRST_CLIENTDRONE_ENV_INNODB_USER = $$DAFL_USER - - CODEFIRST_CLIENTDRONE_ENV_INNODB_PASS = $$DAFL_PASSWORD + CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST = $$DAFL_HOST + CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE = $$DAFL_DATABASE + CODEFIRST_CLIENTDRONE_ENV_INNODB_USER = $$DAFL_USER + CODEFIRST_CLIENTDRONE_ENV_INNODB_PASS = $$DAFL_PASSWORD depends_on: [ php_script ] From 9a71ea7cdd638b6c45640942d81aceaa9811f887 Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 16:31:32 +0100 Subject: [PATCH 08/13] .drone.yaml update --- .drone.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.drone.yml b/.drone.yml index 058637c..07f8ab1 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,6 +1,10 @@ kind: pipeline type: docker name: DAFLPipeline +- CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST = $$DAFL_HOST +- CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE = $$DAFL_DATABASE +- CODEFIRST_CLIENTDRONE_ENV_INNODB_USER = $$DAFL_USER +- CODEFIRST_CLIENTDRONE_ENV_INNODB_PASS = $$DAFL_PASSWORD trigger: event: @@ -82,8 +86,5 @@ steps: from_secret: db_user CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD: from_secret: db_password - CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST = $$DAFL_HOST - CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE = $$DAFL_DATABASE - CODEFIRST_CLIENTDRONE_ENV_INNODB_USER = $$DAFL_USER - CODEFIRST_CLIENTDRONE_ENV_INNODB_PASS = $$DAFL_PASSWORD + depends_on: [ php_script ] From 980c9f380f9a8966fd90e5efa88206f0a437a7dd Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 16:37:50 +0100 Subject: [PATCH 09/13] .drone.yaml update --- .drone.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.drone.yml b/.drone.yml index 07f8ab1..729599a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,10 +1,7 @@ kind: pipeline type: docker name: DAFLPipeline -- CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST = $$DAFL_HOST -- CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE = $$DAFL_DATABASE -- CODEFIRST_CLIENTDRONE_ENV_INNODB_USER = $$DAFL_USER -- CODEFIRST_CLIENTDRONE_ENV_INNODB_PASS = $$DAFL_PASSWORD + trigger: event: @@ -86,5 +83,10 @@ steps: from_secret: db_user CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD: from_secret: db_password + commands: + - export DAFL_HOST = CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST + - export DAFL_DATABASE = CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE + - export DAFL_USER = CODEFIRST_CLIENTDRONE_ENV_INNODB_USER + - export DAFL_PASSWORD = CODEFIRST_CLIENTDRONE_ENV_INNODB_PASS depends_on: [ php_script ] From f8a34fcf1bff6add6d65d5c5a8bd4de276de6c72 Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 16:39:57 +0100 Subject: [PATCH 10/13] .drone.yaml update --- .drone.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.drone.yml b/.drone.yml index 729599a..01b176a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -84,9 +84,9 @@ steps: CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD: from_secret: db_password commands: - - export DAFL_HOST = CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST - - export DAFL_DATABASE = CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE - - export DAFL_USER = CODEFIRST_CLIENTDRONE_ENV_INNODB_USER - - export DAFL_PASSWORD = CODEFIRST_CLIENTDRONE_ENV_INNODB_PASS + - export DAFL_HOST = $CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST + - export DAFL_DATABASE = $CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE + - export DAFL_USER = $CODEFIRST_CLIENTDRONE_ENV_INNODB_USER + - export DAFL_PASSWORD = $CODEFIRST_CLIENTDRONE_ENV_INNODB_PASS depends_on: [ php_script ] From 83d656a12efd4cc326501a278220691460d6afb9 Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 16:46:24 +0100 Subject: [PATCH 11/13] .drone.yaml update --- .drone.yml | 12 ++++++------ Sources/php_script/script/db.php | 6 +----- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/.drone.yml b/.drone.yml index 01b176a..77f8716 100644 --- a/.drone.yml +++ b/.drone.yml @@ -75,7 +75,7 @@ steps: CONTAINERNAME: php_script COMMAND: create PRIVATE: true - CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST: + CODEFIRST_CLIENTDRONE_ENV_INNODB_HOST: from_secret: db_host CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE: from_secret: db_database @@ -83,10 +83,10 @@ steps: from_secret: db_user CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD: from_secret: db_password - commands: - - export DAFL_HOST = $CODEFIRST_CLIENTDRONE_ENV_INNONDB_HOST - - export DAFL_DATABASE = $CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE - - export DAFL_USER = $CODEFIRST_CLIENTDRONE_ENV_INNODB_USER - - export DAFL_PASSWORD = $CODEFIRST_CLIENTDRONE_ENV_INNODB_PASS + #commands: + # - export DAFL_HOST = $CODEFIRST_CLIENTDRONE_ENV_INNODB_HOST + # - export DAFL_DATABASE = $CODEFIRST_CLIENTDRONE_ENV_INNODB_DATABASE + # - export DAFL_USER = $CODEFIRST_CLIENTDRONE_ENV_INNODB_USER + # - export DAFL_PASSWORD = $CODEFIRST_CLIENTDRONE_ENV_INNODB_PASS depends_on: [ php_script ] diff --git a/Sources/php_script/script/db.php b/Sources/php_script/script/db.php index 70eecc0..914136f 100644 --- a/Sources/php_script/script/db.php +++ b/Sources/php_script/script/db.php @@ -3,11 +3,7 @@ $dns = 'mysql:host='.getenv("CODEFIRST_CLIENTDRONE_ENV_INNODB_HOST").';dbname='. $user = $_ENV["CODEFIRST_CLIENTDRONE_ENV_INNODB_USER"]; $password = $_ENV["CODEFIRST_CLIENTDRONE_ENV_INNODB_PASSWORD"]; echo "\n";echo "\n";echo "\n"; -$env_array =getenv(); -foreach ($env_array as $key=>$value) -{ - echo "$key => $value \n"; -} +echo $dns; echo "\n";echo "\n";echo "\n"; try{ $db = new PDO ($dns, $user, $password); From cee41d7abdbb2f6fdb96aa47ba122afd816663e3 Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 17:29:05 +0100 Subject: [PATCH 12/13] Make the PHP Script better --- Sources/php_script/script/config.php | 13 +++++--- Sources/php_script/script/db.php | 9 ++---- Sources/php_script/script/distance.php | 4 +-- Sources/php_script/script/insert.php | 42 +++++++++++++++----------- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/Sources/php_script/script/config.php b/Sources/php_script/script/config.php index 3976c03..21d19bb 100644 --- a/Sources/php_script/script/config.php +++ b/Sources/php_script/script/config.php @@ -1,8 +1,13 @@ prepare($query); +$stm = $connect->prepare($query); $stm->execute(); $row = $stm->fetchAll(PDO::FETCH_ASSOC); $lat1=0; diff --git a/Sources/php_script/script/insert.php b/Sources/php_script/script/insert.php index dfae9ca..3ae92f5 100644 --- a/Sources/php_script/script/insert.php +++ b/Sources/php_script/script/insert.php @@ -1,20 +1,26 @@ 10;"; -$results = mysqli_query($connect, $query); -*/ -$query = "DELETE FROM gps WHERE id='$id';"; -$results = mysqli_query($connect, $query); - -$query = "INSERT INTO gps (id,latitude,longitude,idMusic,dateLog) VALUES('$id','$latitude','$longitude','$idMusic',CURRENT_TIMESTAMP);"; -$results = mysqli_query($connect, $query); +$connect=""; //Else PHP send an error, "connect don't exist", but that work anyway, this is just to remove a fake error +$res=include "config.php"; //$res get the result of the calling of "config.php" +if ($res != 1){ //Check if config.php work + print (json_encode("Failed to connect to MySQL")); //Return a json string, so the dart script can interpret the error +} +if (!empty($_POST)) { //Check if the method POST return something + $id = mysqli_real_escape_string($connect, $_POST['id']); //Get the result of POST method + $latitude = mysqli_real_escape_string($connect, $_POST['latitude']); //Get the result of POST method + $longitude = mysqli_real_escape_string($connect, $_POST['longitude']); //Get the result of POST method + $idMusic = mysqli_real_escape_string($connect, $_POST['idMusic']); //Get the result of POST method + $latitude = doubleval($latitude); //Convert a string to a double + $longitude = doubleval($longitude); //Convert a string to a double + /* + $query = "DELETE FROM gps WHERE (SELECT TIMESTAMPDIFF(MINUTE,CURRENT_TIMESTAMP,dateLog))>10;"; + $results = mysqli_query($connect, $query); + */ + $query = "DELETE FROM gps WHERE id='$id';"; //Delete the actual line and replace this line with the next lines + $results = mysqli_query($connect, $query); //Execute the SQL command + $query = "INSERT INTO gps (id,latitude,longitude,idMusic,dateLog) + VALUES('$id','$latitude','$longitude','$idMusic',CURRENT_TIMESTAMP);"; //Insert into the database the new data and new information about this user + $results = mysqli_query($connect, $query); //Execute the SQL command +}else{ //If the method POST return nothing + print (json_encode("The POST method failed")); //Return a json string, so the dart script can interpret the error +} \ No newline at end of file From a869cd66c2e251706bdfcfc3b19ec4e01d8446f7 Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 19:06:39 +0100 Subject: [PATCH 13/13] Make the PHP Script Better with error management --- Sources/php_script/script/config.php | 16 ++--- Sources/php_script/script/db.php | 10 --- Sources/php_script/script/distance.php | 90 +++++++++++++------------- Sources/php_script/script/insert.php | 2 +- 4 files changed, 55 insertions(+), 63 deletions(-) delete mode 100644 Sources/php_script/script/db.php diff --git a/Sources/php_script/script/config.php b/Sources/php_script/script/config.php index 21d19bb..7ccd56a 100644 --- a/Sources/php_script/script/config.php +++ b/Sources/php_script/script/config.php @@ -1,13 +1,13 @@ getMessage(); - echo $error; -} diff --git a/Sources/php_script/script/distance.php b/Sources/php_script/script/distance.php index a97058f..03148aa 100644 --- a/Sources/php_script/script/distance.php +++ b/Sources/php_script/script/distance.php @@ -2,54 +2,56 @@ function meters($lat1, $lng1, $lat2, $lng2): float { - // Radius of the Earth in meters : - $earth_radius = 6378137; - // Calculation of the distance between 2 GPS coordinates: - $rlo1 = deg2rad($lng1); - $rla1 = deg2rad($lat1); - $rlo2 = deg2rad($lng2); - $rla2 = deg2rad($lat2); - $dlo = ($rlo2 - $rlo1) / 2; - $dla = ($rla2 - $rla1) / 2; - $a = (sin($dla) * sin($dla)) + cos($rla1) * cos($rla2) * (sin($dlo) * sin($dlo)); - $d = 2 * atan2(sqrt($a), sqrt(1 - $a)); - // Return the distance in meters between 2 GPS points - return round($earth_radius * $d); + $earth_radius = 6378137; // Radius of the Earth in meters : + $rlo1 = deg2rad($lng1); //Transform the coordinate longitude1 from degree to radian + $rla1 = deg2rad($lat1); //Transform the coordinate latitude1 from degree to radian + $rlo2 = deg2rad($lng2); //Transform the coordinate longitude2 from degree to radian + $rla2 = deg2rad($lat2); //Transform the coordinate latitude2 from degree to radian + $dlo = ($rlo2 - $rlo1) / 2; //Stock in $dlo the result of the calcul : ($rlo2 - $rlo1) / 2 + $dla = ($rla2 - $rla1) / 2; //Stock in $dla the result of the calcul : ($rla2 - $rla1) / 2 + $a = (sin($dla) * sin($dla)) + cos($rla1) * cos($rla2) * (sin($dlo) * sin($dlo)); //Do some operations to return the distance in meters + $d = 2 * atan2(sqrt($a), sqrt(1 - $a)); //Do some operations to return the distance in meters + return round($earth_radius * $d); // Return the distance in meters between 2 GPS points } - -$connect=""; -include "config.php"; -$id = $_POST['id']; -$query = 'SELECT * FROM gps'; -$stm = $connect->prepare($query); -$stm->execute(); -$row = $stm->fetchAll(PDO::FETCH_ASSOC); -$lat1=0; -$lng1=0; -$listUser=[]; -Foreach ($row as $col) { - if (strcmp($col['id'], $id) == 0) { - $lat1 = $col['latitude']; - $lng1 = $col['longitude']; - } -} -if ($lat1==0 && $lng1==0){ - print(json_encode("ERROR No user found in the database")); - exit(1); +$connect=""; //Else PHP send an error, "connect don't exist", but that work anyway, this is just to remove a fake error +$res=include "config.php"; //$res get the result of the calling of "config.php" +if ($res != 1){ //Check if config.php work + print (json_encode("Failed to connect to MySQL")); //Return a json string, so the dart script can interpret the error } - -Foreach ($row as $col) { - if (strcmp($col['id'],$id)!=0) { - $lat2 = $col['latitude']; - $lng2 = $col['longitude']; - $userID = $col['id']; - $idMusic = $col['idMusic']; - $dist = meters($lat1, $lng1, $lat2, $lng2); - if ($dist <= 100) { - $listUser[] = ['user' => $userID, 'music' => $idMusic]; } +if (!empty($_POST)) { + $id = $_POST['id']; //Get the result of the POST method in id + $query = 'SELECT * FROM gps'; //Browse all the database + $results = mysqli_query($connect, $query); //Execute the SQL command + $lat1 = 0; //Set $lat1 to test if the user exist + $lng1 = 0; //Set $lng1 to test if the user exist + $listUser = []; //Set the listUser to an empty list + while ($row = $results->fetch_row()){ //For all the row in the database + if (strcmp($row[0], $id) == 0) { //If the user is found in the database + $lat1 = $row[1]; //Set $lat1 to the latitude of the current user + $lng1 = $row[2]; //Set $lng1 to the longitude of the current user + } + } + if ($lat1 == 0 && $lng1 == 0) { //Check if the user get found in the database + print(json_encode("ERROR No user found in the database")); //Return a json string, so the dart script can interpret the error + exit(1); //Exit the actual script + } + $results = mysqli_query($connect, $query); //Execute again the SQL command to restart the fetch_row() + while ($row = $results->fetch_row()) { //For all the row in the database + if (strcmp($row[0], $id) != 0) { //If the row is not the row of the current user + $lat2 = $row[1]; //Set $lat2 to the latitude of the user who is in the actual row + $lng2 = $row[2]; //Set $lng2 to the latitude of the user who is in the actual row + $userID = $row[0]; //Set $userID to the username of the user who is in the actual row + $idMusic = $row[3]; //Set $idMusic to the id of the actual song of the user who is in the actual row + $dist = meters($lat1, $lng1, $lat2, $lng2); //With the function meters, calcul of the distance between the current user and the user who is in the actual row + if ($dist <= 100) { //If the user in the actual row is less than 100 meters away of the current user + $listUser[] = ['user' => $userID, 'music' => $idMusic]; //Add the username and the ID of the song that user who is in the actual row is listening + } + } } + print(json_encode($listUser)); //Return a json string of the list listUser +}else{ //If the method POST return nothing + print (json_encode("The POST didn't return any values")); //Return a json string, so the dart script can interpret the error } -print(json_encode($listUser)); diff --git a/Sources/php_script/script/insert.php b/Sources/php_script/script/insert.php index 3ae92f5..c272129 100644 --- a/Sources/php_script/script/insert.php +++ b/Sources/php_script/script/insert.php @@ -22,5 +22,5 @@ if (!empty($_POST)) { VALUES('$id','$latitude','$longitude','$idMusic',CURRENT_TIMESTAMP);"; //Insert into the database the new data and new information about this user $results = mysqli_query($connect, $query); //Execute the SQL command }else{ //If the method POST return nothing - print (json_encode("The POST method failed")); //Return a json string, so the dart script can interpret the error + print (json_encode("The POST didn't return any values")); //Return a json string, so the dart script can interpret the error } \ No newline at end of file