Skip to content

Commit 8c62d27

Browse files
authored
FIX SQL Injection Vulnerabilities following report by pm_security_report
1 parent e558563 commit 8c62d27

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

index.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ function search($s='') {
212212
foreach ($w as &$wi) {
213213
$wi = "(name LIKE '%".$c->real_escape_string($wi)."%' OR form LIKE '%".$c->real_escape_string($wi)."%' OR comments LIKE '%".$c->real_escape_string($wi)."%') ";
214214
}
215-
$q = "SELECT id, name, form FROM ".$_SESSION['dbprefix']."people WHERE ". implode(' AND ', $w) ." ORDER BY name ASC LIMIT 0, 50";
215+
$q = "SELECT `id`, `name`, `form` FROM `".$_SESSION['dbprefix']."people` WHERE ". implode(' AND ', $w) ." ORDER BY `name` ASC LIMIT 0, 50";
216216
$people = db($q, $c);
217217

218218
foreach($people as &$person) {
@@ -247,9 +247,9 @@ function get($detail) {
247247
}else{
248248
global $c;
249249
if (is_numeric($detail)) {
250-
$people = db("SELECT * FROM ".$_SESSION['dbprefix']."people WHERE id = ".$c->real_escape_string($detail)." LIMIT 1", $c);
250+
$people = db("SELECT * FROM `".$_SESSION['dbprefix']."people` WHERE `id` = '".$c->real_escape_string($detail)."' LIMIT 1;", $c);
251251
}else{
252-
$people = db("SELECT * FROM ".$_SESSION['dbprefix']."people WHERE name LIKE '%".$c->real_escape_string($detail)."%' OR form LIKE '%".$c->real_escape_string($detail)."%' ORDER BY updated DESC LIMIT 1", $c);
252+
$people = db("SELECT * FROM `".$_SESSION['dbprefix']."people` WHERE `name` LIKE '%".$c->real_escape_string($detail)."%' OR `form` LIKE '%".$c->real_escape_string($detail)."%' ORDER BY `updated` DESC LIMIT 1;", $c);
253253
}
254254
if ($people) {
255255
$people = $people[0];
@@ -297,12 +297,12 @@ function save() {
297297
}
298298
//var_dump($array);
299299
if ($_POST['id']) { // update details
300-
$q = "UPDATE ".$_SESSION['dbprefix']."people SET
301-
form = '".$c->real_escape_string(json_encode($array))."',
302-
name = '".$c->real_escape_string($_POST['name'])."',
303-
`updated` = '".time()."' WHERE id = ".($_POST['id']).";";
300+
$q = "UPDATE `".$_SESSION['dbprefix']."people` SET
301+
`form` = '".$c->real_escape_string(json_encode($array))."',
302+
`name` = '".$c->real_escape_string($_POST['name'])."',
303+
`updated` = '".time()."' WHERE `id` = '".$c->real_escape_string($_POST['id'])."';";
304304
}else{ // create new
305-
$q = "INSERT INTO ".$_SESSION['dbprefix']."people VALUES (
305+
$q = "INSERT INTO `".$_SESSION['dbprefix']."people` VALUES (
306306
NULL,
307307
'".$c->real_escape_string($_POST['name'])."',
308308
'".$c->real_escape_string(json_encode($array))."',
@@ -319,7 +319,7 @@ function save() {
319319
$response = json(array('status'=>'success','message'=>'Contact details saved'));
320320
}else{
321321
// Get the ID
322-
$q = "SELECT id from ".$_SESSION['dbprefix']."people ORDER BY id DESC LIMIT 1";
322+
$q = "SELECT `id` FROM `".$_SESSION['dbprefix']."people` ORDER BY `id` DESC LIMIT 1;";
323323
$id = db($q, $c);
324324
$response = json(array('id'=>$id[0]['id'],'status'=>'success','message'=>'New contact created'));
325325
}
@@ -348,7 +348,7 @@ function delete($id) {
348348
$response = json(array('status'=>'error','message'=>"Your user cannot delete"));
349349
}else{
350350
global $c;
351-
$deletion = db("DELETE FROM ".$_SESSION['dbprefix']."people WHERE id = ".$c->real_escape_string($id)."", $c);
351+
$deletion = db("DELETE FROM `".$_SESSION['dbprefix']."people` WHERE `id` = '".$c->real_escape_string($id)."';", $c);
352352

353353
if ($deletion) {
354354
$response = json(array('status'=>'success','message'=>'Contact deleted'));
@@ -386,7 +386,7 @@ function comment($id) {
386386
json(array('status'=>'error','message'=>$lang['writecommentfirst']));
387387
}else{
388388
global $c;
389-
$comments = db("SELECT comments FROM ".$_SESSION['dbprefix']."people WHERE id = ".$c->real_escape_string($_POST['id'])."", $c);
389+
$comments = db("SELECT `comments` FROM `".$_SESSION['dbprefix']."people` WHERE id = '".$c->real_escape_string($_POST['id'])."';", $c);
390390
$comments = json_decode($comments[0]['comments'], true);
391391
//var_dump($comments);
392392
array_unshift($comments, array(
@@ -395,7 +395,7 @@ function comment($id) {
395395
'date' => date('c', time()), // iso 8601 format
396396
'text' => $_POST['comment']
397397
));
398-
$q = "UPDATE ".$_SESSION['dbprefix']."people SET comments = '".$c->real_escape_string(json_encode($comments))."' WHERE id = ".$c->real_escape_string($_POST['id'])."";
398+
$q = "UPDATE `".$_SESSION['dbprefix']."people` SET `comments` = '".$c->real_escape_string(json_encode($comments))."' WHERE `id` = '".$c->real_escape_string($_POST['id'])."';";
399399
$result = db($q, $c);
400400

401401
if ($result) {
@@ -419,7 +419,7 @@ function commentdelete($id) {
419419
}else{
420420
global $c;
421421
// load comments from person
422-
$person = db("SELECT id,comments FROM ".$_SESSION['dbprefix']."people WHERE comments LIKE '%".$c->real_escape_string($id)."%' ORDER BY updated DESC LIMIT 1", $c);
422+
$person = db("SELECT `id`, `comments` FROM `".$_SESSION['dbprefix']."people` WHERE `comments` LIKE '%".$c->real_escape_string($id)."%' ORDER BY `updated` DESC LIMIT 1;", $c);
423423
$person[0]['comments'] = json_decode($person[0]['comments'], true);
424424
// remove from array
425425
foreach($person[0]['comments'] as $key => $comment) {
@@ -429,9 +429,9 @@ function commentdelete($id) {
429429
}
430430
}
431431
// update person
432-
$result = db("UPDATE ".$_SESSION['dbprefix']."people SET
433-
comments = '".$c->real_escape_string(json_encode($person[0]['comments']))."'
434-
WHERE id = ".($person[0]['id']).";", $c);
432+
$result = db("UPDATE `".$_SESSION['dbprefix']."people` SET
433+
`comments` = '".$c->real_escape_string(json_encode($person[0]['comments']))."'
434+
WHERE `id` = '".($person[0]['id'])."';", $c);
435435
if ($result) {
436436
$response = json(array(
437437
'status'=>'success',

0 commit comments

Comments
 (0)