Skip to content

Commit

Permalink
Full PDO conversion, no more mysqli (pokepark#153)
Browse files Browse the repository at this point in the history
* Naive conversion to use the PDO flavoured my_query

* Drop useless db close

* Bulk fix fetch_row() calls to normal calls

* Stop pulling in mysqli, no longer required

* Simplify pdo executions

* One more time, pin the latest core

* Convert value binds to execute parameters
  • Loading branch information
jinnatar authored Aug 25, 2020
1 parent 3910444 commit 97ac1d7
Show file tree
Hide file tree
Showing 68 changed files with 242 additions and 223 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ARG INSTALL_CRON=1
ARG PHP_EXTENSIONS="mysqli pdo pdo_mysql opcache gd"
ARG PHP_EXTENSIONS="pdo pdo_mysql opcache gd"
FROM thecodingmachine/php:7.4-v3-apache

# Change back Apache user and group to www-data
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.236.1
2.0.237.2
21 changes: 11 additions & 10 deletions commands/addgym.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@
// Insert / update gym.
try {

global $db;
global $dbh;

// Build query to check if gym is already in database or not
$rs = my_query("
SELECT COUNT(*)
SELECT COUNT(*) AS count
FROM gyms
WHERE gym_name = '{$gym_name}'
");

$row = $rs->fetch_row();
$row = $rs->fetch();

// Gym already in database or new
if (empty($row['0'])) {
if (empty($row['count'])) {
// insert gym in table.
debug_log('Gym not found in database gym list! Inserting gym "' . $gym_name . '" now.');
$query = '
Expand Down Expand Up @@ -122,14 +122,15 @@
}

$statement = $dbh->prepare($query);
$statement->bindValue(':gym_name', $gym_name, PDO::PARAM_STR);
$statement->bindValue(':lat', $lat, PDO::PARAM_STR);
$statement->bindValue(':lon', $lon, PDO::PARAM_STR);
$statement->bindValue(':address', $address, PDO::PARAM_STR);
$statement->execute();
$statement->execute([
'gym_name' => $gym_name,
'lat' => $lat,
'lon' => $lon,
'address' => $address
]);

// Get last insert id.
if (empty($row['0'])) {
if (empty($row['count'])) {
$gym_id = $dbh->lastInsertId();
}

Expand Down
7 changes: 4 additions & 3 deletions commands/gymaddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@
} else if($gym && !empty($info)) {
debug_log('Adding address for gym with ID: ' . $id);
debug_log('Gym note: ' . $info);
my_query(
$stmt = $dbh->prepare(
"
UPDATE gyms
SET address = '{$db->real_escape_string($info)}'
WHERE id = {$id}
SET address = :info
WHERE id = :id
"
);
$stmt->execute(['info' => $info, 'id' => $id]);

// Set message.
$msg = get_gym_details($gym);
Expand Down
10 changes: 7 additions & 3 deletions commands/gymname.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@
if($gym && !empty($info) && $id > 0) {
debug_log('Changing name for gym with ID: ' . $id);
debug_log('Gym name: ' . $info);
my_query(
$stmt = $dbh->prepare(
"
UPDATE gyms
SET gym_name = '{$db->real_escape_string($info)}'
WHERE id = {$id}
SET gym_name = :info
WHERE id = :id
"
);
$stmt->execute([
'info' => $info,
'id' => $id
]);

// Set message.
$gym = get_gym($id);
Expand Down
10 changes: 7 additions & 3 deletions commands/gymnote.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@
} else if($gym && !empty($info)) {
debug_log('Adding gym note for gym with ID: ' . $id);
debug_log('Gym note: ' . $info);
my_query(
$stmt = $dbh->prepare(
"
UPDATE gyms
SET gym_note = '{$db->real_escape_string($info)}'
WHERE id = {$id}
SET gym_note = :info
WHERE id = :id
"
);
$stmt->execute([
'info' => $info,
'id' => $id
]);

// Set message.
$msg = get_gym_details($gym);
Expand Down
2 changes: 1 addition & 1 deletion commands/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
$keys = [];

// Get raids.
while ($raid = $rs->fetch_assoc()) {
while ($raid = $rs->fetch()) {
// Set text and keys.
$gym_name = $raid['gym_name'];
if(empty($gym_name)) {
Expand Down
49 changes: 31 additions & 18 deletions commands/raid.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

// Invalid data received.
if (count($data) < 4) {
send_message($update['message']['chat']['id'], 'Invalid input - Paramter mismatch', []);
send_message($update['message']['chat']['id'], 'Invalid input - Parameter mismatch', []);
exit;
}

Expand Down Expand Up @@ -73,8 +73,7 @@
LIMIT 1
';
$statement = $dbh->prepare( $query );
$statement->bindValue(':gym_name', $gym_name, PDO::PARAM_STR);
$statement->execute();
$statement->execute(['gym_name' => $gym_name]);
while ($row = $statement->fetch()) {

$gym_id = $row['id'];
Expand Down Expand Up @@ -111,7 +110,7 @@
);

// Get row.
$row_ex_raid = $rs_ex_raid->fetch_assoc();
$row_ex_raid = $rs_ex_raid->fetch();
$poke_name = $row_ex_raid['pokemon'];
debug_log('Comparing the current pokemon to pokemons from ex-raid list now...');
debug_log('Current Pokemon in database for this raid: ' . $poke_name);
Expand All @@ -122,25 +121,34 @@
// Ex-Raid! Update only team in raids table.
debug_log('Current pokemon is an ex-raid pokemon: ' . $poke_name);
debug_log('Pokemon "' .$poke_name . '" will NOT be updated to "' . $boss . '"!');
my_query(
$stmt = $dbh->prepare(
"
UPDATE raids
SET gym_team = '{$db->real_escape_string($team)}'
WHERE id = {$raid_id}
SET gym_team = :team
WHERE id = :raid_id
"
);
$stmt->execute([
'team' => $team,
'raid_id' => $raid_id
]);
} else {
// Update pokemon and team in raids table.
debug_log('Current pokemon is NOT an ex-raid pokemon: ' . $poke_name);
debug_log('Pokemon "' .$poke_name . '" will be updated to "' . $boss . '"!');
my_query(
$stmt = $dbh->prepare(
"
UPDATE raids
SET pokemon = '{$db->real_escape_string($boss)}',
gym_team = '{$db->real_escape_string($team)}'
WHERE id = {$raid_id}
SET pokemon = :boss
gym_team = :team
WHERE id = :raid_id
"
);
$stmt->execute([
'boss' => $boss,
'team' => $team,
'raid_id' => $raid_id
]);
}

// Debug log
Expand All @@ -155,21 +163,27 @@
}

// Build the query.
$rs = my_query(
$stmt = $dbh->prepare(
"
INSERT INTO raids
SET pokemon = '{$db->real_escape_string($boss)}',
user_id = {$update['message']['from']['id']},
SET pokemon = :boss,
user_id = :user_id,
first_seen = DATE_FORMAT(UTC_TIMESTAMP(), '%Y-%m-%d %H:%i:00'),
start_time = DATE_ADD(first_seen, INTERVAL {$countdown} MINUTE),
end_time = DATE_ADD(start_time, INTERVAL {$endtime} MINUTE),
gym_team = '{$db->real_escape_string($team)}',
gym_id = '{$gym_id}'
gym_team = :team,
gym_id = :gym_id
"
);
$stmt->execute([
'boss' => $boss,
'user_id' => $update['message']['from']['id'],
'team' => $team,
'gym_id' => $gym_id
]);

// Get last insert id from db.
$id = my_insert_id();
$id = $dbh->lastInsertId();

// Write to log.
debug_log('ID=' . $id);
Expand Down Expand Up @@ -245,4 +259,3 @@
}

?>

74 changes: 38 additions & 36 deletions commands/raid_from_webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ function isPointInsidePolygon($point, $polygon) {
LIMIT 1
';
$statement = $dbh->prepare( $query );
$statement->bindValue(':gym_id', $gym_id, PDO::PARAM_STR);
$statement->execute();
$statement->execute(['gym_id' => $gym_id]);
while ($row = $statement->fetch()) {

$gym_internal_id = $row['id'];
Expand Down Expand Up @@ -125,13 +124,14 @@ function isPointInsidePolygon($point, $polygon) {
gym_id LIKE :gym_id
';
$statement = $dbh->prepare( $query );
$statement->bindValue(':lat', $gym_lat, PDO::PARAM_STR);
$statement->bindValue(':lon', $gym_lon, PDO::PARAM_STR);
$statement->bindValue(':gym_name', $gym_name, PDO::PARAM_STR);
$statement->bindValue(':ex_gym', $gym_is_ex, PDO::PARAM_INT);
$statement->bindValue(':img_url', $gym_img_url, PDO::PARAM_STR);
$statement->bindValue(':gym_id', $gym_id, PDO::PARAM_STR);
$statement->execute();
$statement->execute([
'lat' => $gym_lat,
'lon' => $gym_lon,
'gym_name' => $gym_name,
'gym_id' => $gym_id,
'ex_gym' => $gym_is_ex,
'img_url' => $gym_img_url
]);
}
catch (PDOException $exception) {

Expand All @@ -151,13 +151,14 @@ function isPointInsidePolygon($point, $polygon) {
VALUES (:lat, :lon, :gym_name, :gym_id, :ex_gym, :img_url, 1)
';
$statement = $dbh->prepare( $query );
$statement->bindValue(':lat', $gym_lat, PDO::PARAM_STR);
$statement->bindValue(':lon', $gym_lon, PDO::PARAM_STR);
$statement->bindValue(':gym_name', $gym_name, PDO::PARAM_STR);
$statement->bindValue(':gym_id', $gym_id, PDO::PARAM_STR);
$statement->bindValue(':ex_gym', $gym_is_ex, PDO::PARAM_INT);
$statement->bindValue(':img_url', $gym_img_url, PDO::PARAM_STR);
$statement->execute();
$statement->execute([
'lat' => $gym_lat,
'lon' => $gym_lon,
'gym_name' => $gym_name,
'gym_id' => $gym_id,
'ex_gym' => $gym_is_ex,
'img_url' => $gym_img_url
]);
$gym_internal_id = $dbh->lastInsertId();
}
catch (PDOException $exception) {
Expand Down Expand Up @@ -230,13 +231,14 @@ function isPointInsidePolygon($point, $polygon) {
id LIKE :id
';
$statement = $dbh->prepare( $query );
$statement->bindValue(':pokemon', $pokemon, PDO::PARAM_STR);
$statement->bindValue(':gym_team', $team, PDO::PARAM_STR);
$statement->bindValue(':move1', $move_1, PDO::PARAM_STR);
$statement->bindValue(':move2', $move_2, PDO::PARAM_STR);
$statement->bindValue(':gender', $gender, PDO::PARAM_STR);
$statement->bindValue(':id', $raid_id, PDO::PARAM_INT);
$statement->execute();
$statement->execute([
'pokemon' => $pokemon,
'gym_team' => $team,
'move1' => $move_1,
'move2' => $move_2,
'gender' => $gender,
'id' => $raid_id
]);
}
catch (PDOException $exception) {
error_log($exception->getMessage());
Expand All @@ -257,8 +259,7 @@ function isPointInsidePolygon($point, $polygon) {
WHERE raid_id = :id
';
$cleanup_statement = $dbh->prepare( $cleanup_query );
$cleanup_statement->bindValue(':id', $raid_id, PDO::PARAM_STR);
$cleanup_statement->execute();
$cleanup_statement->execute(['id' => $raid_id]);
while ($row = $cleanup_statement->fetch()) {
if($config->RAID_PICTURE) {
$url = $config->RAID_PICTURE_URL."?pokemon=".$raid_info['pokemon']."&raid=".$raid_id;
Expand All @@ -280,17 +281,18 @@ function isPointInsidePolygon($point, $polygon) {
VALUES (:pokemon, :user_id, :first_seen, :start_time, :end_time, :gym_team, :gym_id, :move1, :move2, :gender)
';
$statement = $dbh->prepare( $query );
$statement->bindValue(':pokemon', $pokemon, PDO::PARAM_STR);
$statement->bindValue(':user_id', $config->WEBHOOK_CREATOR, PDO::PARAM_STR);
$statement->bindValue(':first_seen', gmdate("Y-m-d H:i:s"), PDO::PARAM_STR);
$statement->bindValue(':start_time', $start, PDO::PARAM_STR);
$statement->bindValue(':end_time', $end, PDO::PARAM_STR);
$statement->bindValue(':gym_team', $team, PDO::PARAM_STR);
$statement->bindValue(':gym_id', $gym_internal_id, PDO::PARAM_INT);
$statement->bindValue(':move1', $move_1, PDO::PARAM_STR);
$statement->bindValue(':move2', $move_2, PDO::PARAM_STR);
$statement->bindValue(':gender', $gender, PDO::PARAM_STR);
$statement->execute();
$dbh->execute([
'pokemon' => $pokemon,
'user_id' => $config->WEBHOOK_CREATOR,
'first_seen' => gmdate("Y-m-d H:i:s"),
'start_time' => $start,
'end_time' => $end,
'gym_team' => $team,
'gym_id' => $gym_internal_id,
'move1' => $move_1,
'move2' => $move_2,
'gender' => $gender
]);
$raid_id = $dbh->lastInsertId();
}
catch (PDOException $exception) {
Expand Down
2 changes: 1 addition & 1 deletion commands/start.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"
);

$info = $rs->fetch_assoc();
$info = $rs->fetch();
$creation_limit = $config->RAID_EVENT_CREATION_LIMIT - 1;

// Check raid count
Expand Down
2 changes: 1 addition & 1 deletion config/config.json.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"VERSION":"2.0.236.1",
"VERSION":"2.0.237.2",
"DB_HOST":"localhost",
"DB_NAME":"your_database_name",
"DB_USER":"your_database_user",
Expand Down
2 changes: 1 addition & 1 deletion core
Submodule core updated from 27ff2d to 30242f
4 changes: 2 additions & 2 deletions logic/active_raid_duplication_check.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function active_raid_duplication_check($gym_id)

// Get row - allow normal and ex-raid at the gym.
if($config->RAID_EXCLUDE_EXRAID_DUPLICATION) {
while ($raid = $rs->fetch_assoc()) {
while ($raid = $rs->fetch()) {
$active = $raid['active_raid'];
if ($active > 0) {
// Exclude ex-raid pokemon.
Expand All @@ -43,7 +43,7 @@ function active_raid_duplication_check($gym_id)
}
}
} else {
$raid = $rs->fetch_assoc();
$raid = $rs->fetch();
$active_counter = $raid['active_raid'];
$active_raid_id = $raid['id'];
}
Expand Down
Loading

0 comments on commit 97ac1d7

Please sign in to comment.