Browse Source

Replaced mysql dependency with sqlite

master
jrtechs 2 years ago
parent
commit
1eeeec9bc2
10 changed files with 82 additions and 107 deletions
  1. +35
    -1
      README.md
  2. +1
    -8
      club_connect.php
  3. +0
    -16
      docker-compose.yml
  4. +5
    -17
      games/highscore.php
  5. +4
    -4
      games/insertScore.php
  6. +5
    -16
      games/userscores.php
  7. +3
    -6
      includes/header.php
  8. +4
    -1
      php_docker/Dockerfile
  9. +17
    -29
      user/profile.php
  10. +8
    -9
      user/register.php

+ 35
- 1
README.md View File

@ -14,4 +14,38 @@ A simple website with some JavaScript Games
1. Fork this repo. 1. Fork this repo.
2. Checkout your fork. 2. Checkout your fork.
3. Make changes and commit them to your fork. 3. Make changes and commit them to your fork.
4. Hit the button that says "Submit Pull Request" on your forked repo.
4. Hit the button that says "Submit Pull Request" on your forked repo.
### SQL Lite DB Initialization
```sql
sqlite3 clubpanda.sqlite
CREATE TABLE scores (
score_id INTEGER PRIMARY KEY AUTOINCREMENT,
game INTEGER NOT NULL,
user_id mediumint(9) NOT NULL,
score mediumint(9) NOT NULL
);
CREATE TABLE users (
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name varchar(20) NOT NULL,
last_name varchar(40) NOT NULL,
user_name varchar(60) NOT NULL,
pass char(40) NOT NULL,
registration_date datetime NOT NULL,
admin tinyint(1) NOT NULL
);
select score, users.user_name username from scores inner join users on users.user_id=scores.user_id where game = '1' order by score desc limit 20
.exit
```
Notes on php sqlite documentation: https://www.php.net/manual/en/sqlite3.construct.php

+ 1
- 8
club_connect.php View File

@ -1,10 +1,3 @@
<?php <?php
try
{
$dbc = mysqli_connect("clubdb", "root", 'password', "clubpanda");
} catch (Exception $ex)
{
echo 'Bad things just happened';
}
$db = new SQLite3("/app/public/clubpanda.sqlite", SQLITE3_OPEN_READWRITE);
?> ?>

+ 0
- 16
docker-compose.yml View File

@ -1,19 +1,5 @@
version: '3' version: '3'
# how to access sql when running -- used to import database into docker container
# mysql --port=3306 --host=127.0.0.1 -u root --password=password
services:
clubdb:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- "./db:/var/lib/mysql"
ports:
- "3306:3306"
# Php-fpm configuration # Php-fpm configuration
php: php:
build: ./php_docker/ build: ./php_docker/
@ -30,7 +16,5 @@ services:
- .:/app/public - .:/app/public
links: links:
- php - php
- clubdb
depends_on: depends_on:
- php - php
- clubdb

+ 5
- 17
games/highscore.php View File

@ -5,7 +5,9 @@
echo '<h1 class="w3-text-teal"><center>High Scores</center></h1>'; echo '<h1 class="w3-text-teal"><center>High Scores</center></h1>';
$q = "select * from scores where game = '$game_id' order by score desc limit 20"; $q = "select * from scores where game = '$game_id' order by score desc limit 20";
$r = mysqli_query($dbc, $q);
$q = "select score, users.user_name username from scores inner join users on users.user_id=scores.user_id where game = '$game_id' order by score desc limit 20";
$r = $db->query($q);
echo '<div class="w3-responsive w3-card-4"><table class="w3-table w3-striped echo '<div class="w3-responsive w3-card-4"><table class="w3-table w3-striped
w3-bordered"><thead>'; w3-bordered"><thead>';
echo '<tr class="w3-theme"> echo '<tr class="w3-theme">
@ -15,27 +17,13 @@ echo '
</tr></thead><tbody>'; </tr></thead><tbody>';
$rank = 0; $rank = 0;
while($row = mysqli_fetch_array($r))
while($row = $r->fetchArray())
{ {
$rank ++; $rank ++;
echo '<tr>'; echo '<tr>';
echo '<td>' . $rank . '</td>'; echo '<td>' . $rank . '</td>';
echo '<td>';
$q2 = "select user_name from users where user_id='". $row['user_id'] .
"' limit 1";
$r2 = mysqli_query($dbc, $q2);
while($row2 = mysqli_fetch_array($r2))
echo $row2['user_name'];
echo '</td>';
//score
echo '<td>' . $row['username'] . '</td>';
echo '<td>' . $row['score'] . '</td>'; echo '<td>' . $row['score'] . '</td>';
echo '</tr>'; echo '</tr>';
} }
echo '</tbody></table></div>'; echo '</tbody></table></div>';

+ 4
- 4
games/insertScore.php View File

@ -6,17 +6,17 @@ include_once '../club_connect.php';
if(isset($_POST['game_new_score'])) if(isset($_POST['game_new_score']))
{ {
$i_game = mysqli_real_escape_string($dbc,
$i_game = $db->escapeString(
trim($_POST['game'])); trim($_POST['game']));
$i_user_id = mysqli_real_escape_string($dbc,
$i_user_id = $db->escapeString(
trim($_POST['user_id_score'])); trim($_POST['user_id_score']));
$i_score = mysqli_real_escape_string($dbc,
$i_score = $db->escapeString(
trim($_POST['score_validate'])); trim($_POST['score_validate']));
$q = "insert into scores(game, user_id, score) $q = "insert into scores(game, user_id, score)
values('$i_game','$i_user_id','$i_score')"; values('$i_game','$i_user_id','$i_score')";
if($i_user_id > 0) if($i_user_id > 0)
$r = mysqli_query($dbc, $q);
$r = $db->query($q);
if($i_game == 1) if($i_game == 1)
header("Location: bamboofield.php"); header("Location: bamboofield.php");

+ 5
- 16
games/userscores.php View File

@ -6,10 +6,9 @@ if($loggedIn)
{ {
echo '<h1 class="w3-text-teal"><center>User\'s Personal Records echo '<h1 class="w3-text-teal"><center>User\'s Personal Records
</center></h1>'; </center></h1>';
$q = "select * from scores where user_id='" . $_SESSION['user_id']
$q = "select score, users.user_name username from scores inner join users on users.user_id=scores.user_id where scores.user_id='" . $_SESSION['user_id']
. "' and game='$game_id' order by score desc limit 20"; . "' and game='$game_id' order by score desc limit 20";
$r = mysqli_query($dbc, $q);
$r = $db->query($q);
echo '<div class="w3-responsive w3-card-4"><table echo '<div class="w3-responsive w3-card-4"><table
class="w3-table w3-striped w3-bordered"><thead>'; class="w3-table w3-striped w3-bordered"><thead>';
echo '<tr class="w3-theme"> echo '<tr class="w3-theme">
@ -17,20 +16,10 @@ if($loggedIn)
<td>Score</td> <td>Score</td>
</tr></thead><tbody>'; </tr></thead><tbody>';
while($row = mysqli_fetch_array($r))
while($row = $r->fetchArray())
{ {
echo '<tr><td>';
$q2 = "select user_name from users where user_id='"
. $row['user_id'] . "' limit 1";
$r2 = mysqli_query($dbc, $q2);
while($row2 = mysqli_fetch_array($r2))
echo $row2['user_name'];
echo '</td>';
//score
echo '<tr>';
echo '<td>' . $row['username'] . '</td>';
echo '<td>' . $row['score'] . '</td></tr>'; echo '<td>' . $row['score'] . '</td></tr>';
} }
echo '</tbody></table></div>'; echo '</tbody></table></div>';

+ 3
- 6
includes/header.php View File

@ -27,14 +27,11 @@ if((md5($_SERVER['HTTP_USERAGENT'] . 'salt')) == ($_SESSION['agent']) &&
$q = "select admin from users where user_name='" . $q = "select admin from users where user_name='" .
$_SESSION['username'] . "'"; $_SESSION['username'] . "'";
$r = mysqli_query($dbc, $q);
$r = $db->querySingle($q, true);
if(@mysqli_num_rows($r) == 1)
if($r)
{ {
while($row = mysqli_fetch_array($r))
$checka = $row['admin'];
if($checka)
if($r['admin'])
$admin = true; $admin = true;
} }
} }

+ 4
- 1
php_docker/Dockerfile View File

@ -1,6 +1,9 @@
FROM php:7.4.3-fpm-alpine3.11 FROM php:7.4.3-fpm-alpine3.11
RUN docker-php-ext-install mysqli
RUN apk update \
&& apk add sqlite \
&& apk add socat \
&& apk add php-sqlite3
# Copy the php config file # Copy the php config file
COPY ./php-fpm.conf /usr/local/etc/php-fpm.d/www.conf COPY ./php-fpm.conf /usr/local/etc/php-fpm.d/www.conf

+ 17
- 29
user/profile.php View File

@ -19,13 +19,13 @@ if(isset($_POST['log_in']))
{ {
//echo 'Login procces'; //echo 'Login procces';
if(isset($_POST['user_name'])) if(isset($_POST['user_name']))
$i_username = @mysqli_real_escape_string($dbc,
$i_username = $db->escapeString(
trim($_POST['user_name'])); trim($_POST['user_name']));
else else
$errors['User Name'] = 'You need to enter a user name!'; $errors['User Name'] = 'You need to enter a user name!';
if(isset($_POST['password'])) if(isset($_POST['password']))
$i_password = @mysqli_real_escape_string($dbc,
$i_password = $db->escapeString(
trim($_POST['password'])); trim($_POST['password']));
else else
$errors['password'] = "You need to enter a password!"; $errors['password'] = "You need to enter a password!";
@ -35,42 +35,30 @@ if(isset($_POST['log_in']))
{ {
//valid username //valid username
$q3 = "select * from users where user_name='$i_username'"; $q3 = "select * from users where user_name='$i_username'";
//echo $q3;
$r3 = mysqli_query($dbc, $q3);
$r3 = $db->querySingle($q3, true);
if(@mysqli_num_rows($r3) == 1)
if($r3) //not empty
{ {
//echo 'das good';
$firstName = "";
while($row = mysqli_fetch_array($r3))
$firstName = $row['first_name'];
$firstName = $r3['first_name'];
$q2 = "select * from users where user_name = $q2 = "select * from users where user_name =
'$i_username' and pass ='" . SHA1($i_password '$i_username' and pass ='" . SHA1($i_password
. $firstName) . "'"; . $firstName) . "'";
$r2 = $db->querySingle($q2, true);
if($r2)
{
$_SESSION['use'] = true;
$_SESSION['fname'] = $firstName;
$_SESSION['user_id'] = $r2['user_id'];
$_SESSION['username'] = $r2['user_name'];
$_SESSION['agent'] = md5($_SERVER['HTTP_USERAGENT'] . 'salt');
$r2 = mysqli_query($dbc, $q2);
if($dir == 2)
header("Location: ../index.php");
else
header("Location: index.php");
if(@mysqli_num_rows($r2) == 1)
{
while($row = mysqli_fetch_array($r2))
{
$_SESSION['use'] = true;
$_SESSION['fname'] = $firstName;
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['username'] = $row['user_name'];
$_SESSION['agent'] = md5($_SERVER['HTTP_USERAGENT']
. 'salt');
if($dir == 2)
header("Location: ../index.php");
else
header("Location: index.php");
}
} }
else else
{ {

+ 8
- 9
user/register.php View File

@ -7,17 +7,17 @@ if(!$loggedIn)
if(isset($_POST['newUser'])) if(isset($_POST['newUser']))
{ {
$i_first = mysqli_real_escape_string($dbc, trim($_POST['first']));
$i_last = mysqli_real_escape_string($dbc, trim($_POST['last']));
$i_pass = mysqli_real_escape_string($dbc, trim($_POST['pass']));
$i_user = mysqli_real_escape_string($dbc, trim($_POST['user_name']));
$i_first = $db->escapeString(trim($_POST['first']));
$i_last = $db->escapeString(trim($_POST['last']));
$i_pass = $db->escapeString(trim($_POST['pass']));
$i_user = $db->escapeString(trim($_POST['user_name']));
if($i_first && $i_last && $i_pass && $i_user) if($i_first && $i_last && $i_pass && $i_user)
{ {
$q = "select user_id from users where user_name='$i_user'"; $q = "select user_id from users where user_name='$i_user'";
$r = mysqli_query($dbc, $q);
$r = $db->query($q);
if(@mysqli_num_rows($r) == 1)
if($r->numColumns() && $r->columnType(0) != SQLITE3_NULL)
{ {
$errors['name'] = "That user name is already in use."; $errors['name'] = "That user name is already in use.";
} }
@ -44,9 +44,8 @@ if(!$loggedIn)
$q = "insert into users(first_name, last_name, user_name, pass, $q = "insert into users(first_name, last_name, user_name, pass,
registration_date, admin) values ('$i_first', '$i_last' registration_date, admin) values ('$i_first', '$i_last'
, '$i_user', '$passcom', now(), false)";
$r = mysqli_query($dbc, $q);
, '$i_user', '$passcom', date('now'), false)";
$r = $db->query($q);
header("Location: index.php"); header("Location: index.php");
} }

Loading…
Cancel
Save