diff --git a/.drone.yml b/.drone.yml index 7c91da7..77f8716 100644 --- a/.drone.yml +++ b/.drone.yml @@ -2,6 +2,7 @@ kind: pipeline type: docker name: DAFLPipeline + trigger: event: - push @@ -73,5 +74,19 @@ steps: IMAGENAME: hub.codefirst.iut.uca.fr/dorian.hodin/dafl_music:latest CONTAINERNAME: php_script COMMAND: create - OVERWRITE: true - depends_on: [ php_script ] \ No newline at end of file + PRIVATE: true + CODEFIRST_CLIENTDRONE_ENV_INNODB_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 + #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/.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..7ccd56a 100644 --- a/Sources/php_script/script/config.php +++ b/Sources/php_script/script/config.php @@ -1,14 +1,13 @@ getMessage(); - echo $error; -} diff --git a/Sources/php_script/script/distance.php b/Sources/php_script/script/distance.php index a2155b0..03148aa 100644 --- a/Sources/php_script/script/distance.php +++ b/Sources/php_script/script/distance.php @@ -1,61 +1,57 @@ 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']; - } + $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 } -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 = (new distance)->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/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 diff --git a/Sources/php_script/script/insert.php b/Sources/php_script/script/insert.php index dfae9ca..c272129 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 didn't return any values")); //Return a json string, so the dart script can interpret the error +} \ No newline at end of file