Skip to content

Commit

Permalink
Replace mysql specific calls with PDO usage
Browse files Browse the repository at this point in the history
To prevent SQL injections, it is using PDO which supports binding parameters.
Also the `mysql_query` function has been deprecated in favour of PDO or
`mysqli`. Using PDO allows in theory to use different databases in the future.
  • Loading branch information
xZise committed Nov 26, 2015
1 parent dd4b502 commit 13ae949
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 131 deletions.
108 changes: 108 additions & 0 deletions database.php
@@ -0,0 +1,108 @@
<?php

if (!$no_config)
require_once("config.php");

define('NO_USER', -1);
define('LOCKED_USER', -2);
define('INVALID_CREDENTIALS', -3);


// Database related functions

function connect($host=true, $name=true, $user=true, $pass=true)
{
global $DBIP, $DBNAME, $DBUSER, $DBPASS;
if ($host === true)
$host = $DBIP;
if ($name === true)
$name = $DBNAME;
if ($user === true)
$user = $DBUSER;
if ($pass === true)
$pass = $DBPASS;
return new TrackMePDO("mysql:host=$host;dbname=$name",
$user, $pass);
}

function connect_save($host=true, $name=true, $user=true, $pass=true)
{
try {
return connect($host, $name, $user, $pass);
} catch (PDOException $e) {
return null;
}
}

// TODO: Use PHP's implementation of password_verify and password_hash
function password_verify($password, $hash)
{
return $hash === password_hash($password);
}

function password_hash($password)
{
$salt = "trackmeuser";
return MD5($salt.$password);
}

class TrackMePDO extends PDO {

function get_count($statement)
{
$stmt = $this->prepare("SELECT COUNT(*) FROM $statement");
$stmt->execute();
$result = $stmt->fetchAll();
return $result[0][0];
}

function exec_sql()
{
$args = func_get_args();
$statement = $args[0];
if (count($args) == 1)
$args = array();
elseif (is_array($args[1]))
$args = $args[1];
else
$args = array_slice($args, 1);
$stmt = $this->prepare($statement);
for ($i = 0; $i < count($args); $i++)
$stmt->bindParam($i + 1, $args[$i]);
if ($stmt->execute())
return $stmt;
else
return false;
}

function create_login($username, $password)
{
$this->exec_sql("INSERT INTO users (username, password) ".
"VALUES (?, ?)",
$username, password_hash($password));
return $this->valid_login($username, $password);
}

function valid_login($username, $password, $allow_disabled=false)
{
$user = $this->exec_sql("Select ID, password, Enabled " .
"FROM users WHERE username=?",
$username)->fetch();
if (is_null($user))
{
return NO_USER;
}
elseif (password_verify($password, $user['password']))
{
if (!$allow_disabled && $user['Enabled'] == 0)
return LOCKED_USER;
else
return $user['ID'];
}
else
{
return INVALID_CREDENTIALS;
}
}
}
?>
92 changes: 42 additions & 50 deletions export.php
@@ -1,6 +1,6 @@
<?php

require_once('config.php');
require_once('database.php');

header("Content-type: text/xml");

Expand All @@ -11,33 +11,26 @@
die;
}

if(!@mysql_connect("$DBIP","$DBUSER","$DBPASS"))
$db = connect_save()
if(is_null($db))
{
echo "<Result>4</Result>";
die();
}

mysql_select_db("$DBNAME");

$showbearings = 0;

$action = $_GET["a"];
$username = urldecode($_GET["u"]);
$password = urldecode($_GET["p"]);
$salt = "trackmeuser";
$password = MD5($salt.$password);
$datefrom = urldecode($_GET["df"]);
$dateto = urldecode($_GET["dt"]);
$tripname = urldecode($_GET["tn"]);
$showbearings = urldecode($_GET["sb"]);


$result=mysql_query("Select ID FROM users WHERE username = '$username' and password='$password'");
if ( $row=mysql_fetch_array($result) )
{
$userid=$row['ID']; // Good, user and password are correct.
}
else
$userid = $db->valid_login($username, $password);
if ($userid < 0)
{
echo "<Result>1</Result>";
die();
Expand All @@ -46,34 +39,48 @@



if($action=="kml")
{
// Condition
$cond = "";
$params = array();
$cond = " WHERE A1.FK_Users_ID = ?";
if ($tripname == "<None>" )
$cond = "WHERE FK_Trips_ID is null AND A1.FK_USERS_ID='$userid' ";
{
$cond .= " AND A1.FK_Trips_ID is null";
}
else if ($tripname != "" )
$cond = "INNER JOIN trips A2 ON A1.FK_Trips_ID=A2.ID AND A2.Name='$tripname' WHERE A1.FK_USERS_ID='$userid' ";
{
$cond = " INNER JOIN trips A2 ON A1.FK_Trips_ID = A2.ID AND A2.Name = ? $cond"
$params[] = $tripname;
}
else
$cond = "LEFT JOIN trips A2 ON A1.FK_Trips_ID=A2.ID WHERE A1.FK_USERS_ID='$userid' ";
{
$cond = " LEFT JOIN trips A2 ON A1.FK_Trips_ID = A2.ID $cond";
}
$params[] = $userid;
if ( $datefrom != "" )
$cond .=" and DateOccurred>='$datefrom' ";
{
$cond .=" AND DateOccurred >= ?";
$params[] = $datefrom;
}
if ( $dateto != "" )
$cond .=" and DateOccurred<='$dateto' ";

{
$cond .=" AND DateOccurred <= ?";
$params[] = $dateto;
}
$cond .=" order by dateoccurred asc";


if($action=="kml")
{

// Generate code for custom icons
$customicons = "";
$sql = "select distinct A3.ID, A3.URL from icons A3 inner join positions A1 on A1.fk_icons_id = A3.ID ";
$sql = $sql.$cond;

$result = mysql_query($sql);
$result = $db->exec_sql("SELECT DISTINCT A3.ID, A3.URL " .
"FROM icons A3 " .
"INNER JOIN positions A1 ON A1.fk_icons_id = A3.ID" .
$cond,
$params);

while( $row = mysql_fetch_array($result) )
while ($row = $result->fetch())
{
$customicons .="<Style id='CustomIcon".$row['ID']."'>";
$customicons .="<IconStyle>";
Expand Down Expand Up @@ -236,8 +243,7 @@
$sql = $sql.$cond;


$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
$result = $db->exec_sql($sql, $params);

$header = "<?xml version='1.0' encoding='utf-8' ?>";
$header .= "<kml xmlns='http://earth.google.com/kml/2.0'>";
Expand All @@ -251,16 +257,19 @@

$count = 0;
$group = "";

$next_row = $result->fetch();

while( $row=mysql_fetch_array($result) )
while($row = $next_row)
{
$next_row = $result->fetch();
$speedMPH = number_format($row['speed']*2.2369362920544,2);
$speedKPH = number_format($row['speed']*3.6,2);
$altitudeFeet = number_format($row['altitude']*3.2808399,2);
$altitudeM = number_format($row['altitude'],2);
$angle = number_format($row['angle'],2);

if ( $count == $num_rows -1 ) // Last pushpin
if ($next_row === false) // Last pushpin
{
$output .="<LookAt>";
$output .="<longitude>".$row['longitude']."</longitude>";
Expand Down Expand Up @@ -492,22 +501,6 @@
}
else if ($action = "gpx")
{
// Condition
$cond = "";
if ($tripname == "<None>" )
$cond = "WHERE FK_Trips_ID is null AND A1.FK_USERS_ID='$userid' ";
else if ($tripname != "" )
$cond = "INNER JOIN trips A2 ON A1.FK_Trips_ID=A2.ID AND A2.Name='$tripname' WHERE A1.FK_USERS_ID='$userid' ";
else
$cond = "LEFT JOIN trips A2 ON A1.FK_Trips_ID=A2.ID WHERE A1.FK_USERS_ID='$userid' ";

if ( $datefrom != "" )
$cond .=" and DateOccurred>='$datefrom' ";
if ( $dateto != "" )
$cond .=" and DateOccurred<='$dateto' ";

$cond .=" order by dateoccurred desc";

// Main query
if ($tripname == "<None>" ) {
$sql = "select UNIX_TIMESTAMP(DateOccurred) as DateOccured,latitude, longitude,speed,altitude,fk_icons_id as customicon, null as tripname,A1.comments,A1.imageurl,A1.angle from positions A1 ";
Expand All @@ -517,8 +510,7 @@
}

$sql = $sql.$cond;
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
$result = $db->exec_sql($sql, $params);

$n=0;
$bounds_lat_min = 0;
Expand All @@ -528,7 +520,7 @@
$wptdata="";
$trkptdata="<trk>\n";
$trkptdata.="<trkseg>\n";
while( $row=mysql_fetch_array($result) )
while ($row = $result->fetch())
{
if(($row['latitude']<$bounds_lat_min && $bounds_lat_min!=0) || $bounds_lat_min==0) { $bounds_lat_min = $row['latitude']; }
if(($row['latitude']>$bounds_lat_max && $bounds_lat_max!=0) || $bounds_lat_max==0) { $bounds_lat_max = $row['latitude']; }
Expand Down

0 comments on commit 13ae949

Please sign in to comment.