Merge branch 'PHP_Script_on_Docker' into SQL_Script
continuous-integration/drone/push Build is failing Details

remotes/origin/mvc-implementation
dorian.hodin 2 years ago
commit 9840d8d894

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="@localhost" uuid="f334e98a-3c30-4412-8c71-35fe124ed605">
<driver-ref>mariadb</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.mariadb.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mariadb://localhost:3306</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataSource name="@localhost">
<database-model serializer="dbm" dbms="MARIADB" family-id="MARIADB" format-version="4.43">
<root id="1">
<DefaultCasing>lower/lower</DefaultCasing>
<ServerVersion>10.4.25</ServerVersion>
</root>
<schema id="2" parent="1" name="information_schema"/>
<schema id="3" parent="1" name="mysql"/>
<schema id="4" parent="1" name="performance_schema"/>
<schema id="5" parent="1" name="phpmyadmin"/>
<schema id="6" parent="1" name="positiondaflmusic"/>
<schema id="7" parent="1" name="projetphp"/>
<schema id="8" parent="1" name="test"/>
</database-model>
</dataSource>

@ -0,0 +1,2 @@
#n:information_schema
!<md> [null, 0, null, null, -2147483648, -2147483648]

@ -0,0 +1,2 @@
#n:mysql
!<md> [null, 0, null, null, -2147483648, -2147483648]

@ -0,0 +1,2 @@
#n:performance_schema
!<md> [null, 0, null, null, -2147483648, -2147483648]

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/Sources/php_script/script/insertAndMakeListUser.php" dialect="GenericSQL" />
</component>
</project>

@ -6,48 +6,52 @@ import '../main.dart';
class Location {
static Future<Map<String, dynamic>> sendCurrentLocation() async {
Uri uri = Uri.parse("https://codefirst.iut.uca.fr/containers/php_script-dorianhodin/insert.php");
Uri uri = Uri.parse("https://codefirst.iut.uca.fr/containers/php_script-dorianhodin/insertAndMakeListUser.php");
Map<String, dynamic> spot = {};
LocationPermission permission;
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.deniedForever) {
//faire l'interface gra pour gérer ça
return Future.error('Location Not Available');
}
}
String actualUser = MyApp.controller.currentUser.usernameDafl;
String actualSong = await MyApp.api.getCurrentlyPlayingTrack();
Position current = await Geolocator.getCurrentPosition();
await http.post(uri, body: {
http.Response response = await http.post(uri, body: {
"id": actualUser.toString(),
"latitude": current.latitude.toString(),
"longitude": current.longitude.toString(),
"idMusic": actualSong.toString(),
});
return getData();
}
static Future<Map<String, dynamic>> getData() async {
Map<String, dynamic> spot = {};
String actualUser = MyApp.controller.currentUser.usernameDafl;
Uri uri = Uri.parse("https://codefirst.iut.uca.fr/containers/php_script-dorianhodin/distance.php");
http.Response response = await http.post(uri, body: {
"id": actualUser,
});
var data = jsonDecode(response.body);
if (data.runtimeType==String) {
if (data=="Failed to connect to MySQL, connection timeout"){
return Future.error(data);
}else if (data=="The POST didn't return any values"){
return Future.error(data);
}else{
return Future.error(data); //ERROR No user found in the database
}
if (data == 2){
return Future.error("Failed to connect, connection timeout");
}else if (data == 3) {
return Future.error("POST method failed");
}else{
data.forEach((s) => spot.putIfAbsent(s['user'], () => s['music']));
return spot;
}
}
}

@ -1,13 +1,22 @@
<?php
function connection(): bool|int|mysqli
{
$username = $_ENV["USER"]; //Get the username
$host = $_ENV["HOST"]; //Get the address IP of the hosting machine
$password = $_ENV["PASSWORD"]; //Get the password for the user selected
$db_name = $_ENV["DATABASE"]; //Get the name of the database
try { //Try to connect to the database
$connect = mysqli_connect($host, $username, $password, $db_name); //Connecting to database
return mysqli_connect($host, $username, $password, $db_name); //Connecting to database
}catch (mysqli_sql_exception) { //If the connection failed
print(json_encode("Failed to connect to MySQL, connection timeout")); //Return a json string, so the dart script can interpret the error
exit(-1); //Send a return code as -1, so insert.php can know if the connection is successful
return -1; //Send a return code as -1, so insertAndMakeListUser.php can know if the connection is successful
}
}

@ -1,57 +1,23 @@
<?php
function meters($lat1, $lng1, $lat2, $lng2): float
function metersBetweenTwoUser($lat1, $lng1, $lat2, $lng2): float
{
$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=""; //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, connection timeout")); //Return a json string, so the dart script can interpret the error
}
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
}

@ -1,5 +0,0 @@
<?php
echo "Hello World Dafldev"
?>

@ -1,26 +0,0 @@
<?php
$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, connection timeout")); //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
}

@ -0,0 +1,68 @@
<?php
function insertUserAndReturnList(): array|int
{
$res = connection();
if ($res == -1) {
return 2;
}
if (!empty($_POST)) { //Check if the method POST return something
$id = $_POST['id']; //Get the result of POST method
$latitude = $_POST['latitude']; //Get the result of POST method
$longitude = $_POST['longitude']; //Get the result of POST method
$idMusic = $_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 = "SELECT id FROM gps WHERE id='$id'";
$results = mysqli_query($res, $query); //Execute the SQL command
if (empty($results)){
$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
}else{
$query = "UPDATE gps SET latitude='$latitude', longitude='$longitude', idMusic='$idMusic' WHERE id='$id'"; //Delete the actual line and replace this line with the next lines
}
mysqli_query($res, $query);
$query = "SELECT * FROM gps WHERE id != '$id'"; //Browse all the database
$results = mysqli_query($res, $query); //Execute the SQL command
$listUser = []; //Set the listUser to an empty list
while ($row = $results->fetch_row()) { //For all the row in the database
$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 = metersBetweenTwoUser($latitude, $longitude, $lat2, $lng2); //With the function meters, determinate 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
}
}
return (json_encode($listUser)); //Return an encode array in json, so flutter ca=n interpret it
} else { //If the method POST return nothing
return 3; //Return a code error
}
}
Loading…
Cancel
Save