Skip to content

Commit

Permalink
Added dbcontroller; added logs trace
Browse files Browse the repository at this point in the history
  • Loading branch information
grillol committed Jul 21, 2019
1 parent 3ebf63a commit 3422cf9
Show file tree
Hide file tree
Showing 15 changed files with 1,357 additions and 108 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
# iocuMap
A map based CMS where it is possibile to associate one ore more widget to a marker. The map can be an OpenStreetMap layer or a static image. The leaflet (https://leafletjs.com/) library is used to develop the interactive map.

60 changes: 56 additions & 4 deletions database/add.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,64 @@
<?php

/*
* Author: Luigi Grillo
* File: add.php
* Last update: 10/07/2019
* Todo:
* 1. Check for authorization and paramenters
*
* DESCRIPTION:
* Add new post to a marker. If the marker does not exists a new one will be saved on the database
* { "name": "widget name",
* "html": "widget definition to be loaded from form.json"
* }
*
*/

require_once("dbcontroller.php");
$db_handle = new DBController();

if(!empty($_POST["lng"])) {
// TODO check authorization - check parameters

/*
* If the POI (lat,lng) does not exist it creates a new one.
* Return the POI's id
* TODO: to implement a function around() to accept a delta difference of the coordinates
function getPOIid($lat,$lng) {
return $result;
}
*/

$lat = $_GET["lat"];
$lng = $_GET["lng"];
$data = $_GET["data"];
$description = $_GET["description"];
$widget = $_GET["widget"];

$db_handle->log("add.php","PARAMETERS GET = lat: $lat lng:$lng data:$data widget:$widget decription:$description\n");

// check the marker, or create a new one if it does not exists
$sql = "select id from poi where lat=".$lat." and lng=".$lng;
$idPOI = $db_handle->runSelectQuery($sql);
if (!$idPOI) { // The POI does not exists
$idPOI = $db_handle->insertPOI($lat,$lng,$description);
} else {

$sql = "INSERT INTO poi (lat,lng,description) VALUES ('" .$_POST["lat"] . "','" . $_POST["lng"] ."','" . $_POST["description"] . "')";
$poi = $db_handle->executeInsert($sql);
// TODO The POI exists already
}

$sql = 'select id from widgets where name="'.$widget.'"';
$idWidget = $db_handle->runSelectQuery($sql); // TODO CHECK result

$db_handle->insertPOST($idPOI,$idWidget[0],$data);

// TODO check query result


echo $poi;

}
74 changes: 69 additions & 5 deletions database/dbcontroller.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
<?php


/*
* Author: Luigi Grillo
* File: dbcontroller.php
* Last update: 10/07/2019
* Todo:
* 1. logs actions end errors
* 2. acquire USER information to log actions
* 3. Verify mysqli_free_result($result); and mysqli_close($link);
* DESCRIPTION:
*
*
*/


class DBController {
private $conn = "";
private $host = "89.46.111.49";
Expand All @@ -15,21 +31,37 @@ function __construct() {

function connectDB() {
$conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
if (mysqli_connect_errno()) {
$this->log("dbcontroller.php->connectDB","Connect failed: %s\n", mysqli_connect_error());
exit();
}

return $conn;
}

function runSelectQuery($query) {
$this->log("dbcontroller.php->runSelectQuery","runSelectQuery ".$query);
$result = mysqli_query($this->conn,$query);
while($row=mysqli_fetch_assoc($result)) {
$resultset[] = $row;
$resultset = array(); // In order to return an array even in case of no data
if ($result) {
while($row=mysqli_fetch_assoc($result)) {
$resultset[] = $row;
}
}
if(!empty($resultset))
return $resultset;

$this->log("dbcontroller.php->runSelectQuery"," \n runSelectQuery result $resultset\n");
return $resultset;

}

function executeInsert($query) {
$result = mysqli_query($this->conn,$query);
$this->log("dbcontroller.php","executeInsert ".$query);

$result = mysqli_query($this->conn,$query);

$insert_id = mysqli_insert_id($this->conn);
$this->log("dbcontroller.php","executeInsert DONE - insertID: ".$insert_id);

return $insert_id;

}
Expand All @@ -40,6 +72,7 @@ function executeUpdate($query) {
}

function executeQuery($sql) {
$this->log("dbcontroller.php","executeQuery ".$query);
$result = mysqli_query($this->conn,$sql);
return $result;

Expand All @@ -54,5 +87,36 @@ function numRows($query) {
function stringEscape($s) {
return mysqli_real_escape_string($this->conn,$s);
}

function insertPOI($lat,$lng,$description){
$this->log("insertPOI","$lat,$lng,stringEscape($description)");

$sql = 'INSERT INTO poi (id ,lat,lng,description,dataRegistration,userID)
VALUES (NULL , '.$lat.', '.$lng.',"'.$this->stringEscape($description).'", CURRENT_TIMESTAMP , NULL);';
return $this->executeInsert($sql);
}

function insertPOST($idPOI,$idWidget,$data){
$sql = 'INSERT INTO posts (id, id_poi, id_widget, data, dataRegistration, user)
VALUES (NULL, '.$idPOI.', '.$idWidget.', "'.$this->stringEscape($data).'", CURRENT_TIMESTAMP, NULL)';
$this->log("insertPOST","$idPOI,$idWidget,stringEscape($data)");
return $this->executeInsert($sql);
}

function insertWIDGET(){

}

function log($p,$l) {

// TODO implement a class log with functionalities: remote logs, logrotate, log analysis
// TODO application home director parametrization

$fd = fopen($_SERVER['DOCUMENT_ROOT']."/maps/logs/log.txt","a");
fwrite($fd,date('Y-m-d H:i:s')." - FROM: $p - $l \n");
fclose($fd);
// TO DO check for log file open error
}

}
?>
10 changes: 4 additions & 6 deletions database/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* File:
* Last update:
* Todo:
*
* check authorizaation
*
*/

Expand All @@ -15,11 +15,9 @@
$sql = "SELECT * from poi";
$poi = $db_handle->runSelectQuery($sql);

$fd = fopen("log.txt","w");
fwrite($fd,json_encode($poi));
fclose($fd);

echo json_encode($poi);
if ($poi) {
echo json_encode($poi);
}

// TODO Check for error

Expand Down
42 changes: 42 additions & 0 deletions database/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php






$lat = $_GET["lat"];
$lng = $_GET["lng"];
$data = $_GET["data"];
$description = $_GET["description"];
$widget = $_GET["widget"];


$conn = "";
$host = "89.46.111.49";
$user = "Sql1127014";
$dbname = "Sql1127014_5";
$password = "827r224040";

// Create connection
$conn = new mysqli($host, $user, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = 'INSERT INTO poi (id ,lat,lng,description,dataRegistration,userID)
VALUES (NULL , '.$lat.', '.$lng.',"'.mysqli_real_escape_string($conn,$description).'", CURRENT_TIMESTAMP , NULL);';

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();




?>
Loading

0 comments on commit 3422cf9

Please sign in to comment.