Skip to content

Debugging and updates #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,11 @@ services:
- php-mvc-network

php-mvc-db:
image: mysql
image: mysql:8
container_name: php-mvc-db
command: --plugin-load-add=mysql_native_password.so
volumes:
- dbdata:/var/lib/mysql
- ./docker/mysql/my.cnf:/etc/mysql/my.cnf
- ./docker/mysql/init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=mvc_db
Expand Down
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ RUN apt-get update && apt-get install -y --no-install-recommends \

RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install mysqli pdo pdo_mysql gd sockets opcache
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug
RUN pecl install -o -f memcached \
&& docker-php-ext-enable memcached
RUN pecl install grpc \
&& docker-php-ext-enable grpc
RUN pecl install protobuf \
&& docker-php-ext-enable protobuf
RUN pecl install xdebug-3.4.0beta1 \
&& docker-php-ext-enable xdebug

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

Expand Down
4 changes: 0 additions & 4 deletions docker/mysql/init.sql

This file was deleted.

1 change: 0 additions & 1 deletion docker/mysql/my.cnf
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[mysqld]
plugin-load-add=mysql_native_password.so
sql_mode = STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
general_log = 1
general_log_file = /var/lib/mysql/general.log
Expand Down
3 changes: 2 additions & 1 deletion docker/php/opcache.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[opcache]
opcache.enable=1
# Enable it in production
opcache.enable=0
opcache.revalidate_freq=0
opcache.validate_timestamps=0
opcache.max_accelerated_files=10000
Expand Down
4 changes: 2 additions & 2 deletions src/App/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private static function WEBauthentication(): mixed
Database::query("SELECT * FROM users WHERE email = :email");
Database::bind(':email', $email);

if (!is_null(Database::fetch()['id'])) return Database::fetch()['id'];
if (!empty(Database::fetch()['id'])) return Database::fetch()['id'];
}
return null;
}
Expand All @@ -88,7 +88,7 @@ private static function APIauthentication(): mixed
Database::query("SELECT * FROM users WHERE secret = :secret");
Database::bind(':secret', $matches[1]);

if (!is_null(Database::fetch()['id'])) {
if (!empty(Database::fetch()['id'])) {
setcookie('loggedin', base64_encode(Database::fetch()['email']), time() + (86400 * COOKIE_DAYS));
return Database::fetch()['id'];
}
Expand Down
12 changes: 6 additions & 6 deletions src/Models/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public static function verify(object $request): bool
Database::bind(':email', $request->email);

if (
!is_null(Database::fetch())
&& !is_null(Database::fetch()['user_token'])
!empty(Database::fetch())
&& !empty(Database::fetch()['user_token'])
&& $request->user_token == Database::fetch()['user_token']
) {
Database::query("UPDATE users SET verified = :verified WHERE email = :email");
Expand Down Expand Up @@ -78,7 +78,7 @@ public static function getSecret(object $request): mixed
Database::query("SELECT * FROM users WHERE email = :email");
Database::bind(':email', $request->email);

if (!is_null(Database::fetch()) && !is_null(Database::fetch()['secret'])) return Database::fetch()['secret'];
if (!empty(Database::fetch()) && !empty(Database::fetch()['secret'])) return Database::fetch()['secret'];
return false;
}

Expand All @@ -93,7 +93,7 @@ public static function checkEmail(string $email): bool
Database::query("SELECT * FROM users WHERE email = :email");
Database::bind(':email', $email);

if (!is_null(Database::fetch())) return true;
if (!empty(Database::fetch())) return true;
return false;
}

Expand All @@ -109,7 +109,7 @@ public static function checkPassword(object $request): bool
Database::bind(':email', $request->email);

if (
!is_null(Database::fetch())
!empty(Database::fetch())
&& password_verify($request->password, Database::fetch()['password'] ?? '')
) {
return true;
Expand All @@ -129,7 +129,7 @@ public static function checkVerification(string $email): bool
Database::bind(':email', $email);

if (
!is_null(Database::fetch())
!empty(Database::fetch())
&& Database::fetch()['verified']
) {
return true;
Expand Down
44 changes: 44 additions & 0 deletions src/migrate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use App\Database;

/**
* Initialize the migrations table if it doesn't exist.
*/
function initializeMigrationsTable(): void
{
$query = "CREATE TABLE IF NOT EXISTS migrations (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
migration VARCHAR(255) NOT NULL,
executed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
Database::query($query);
Database::execute();
}

/**
* Applies a migration only if it has not been applied before.
*
* @param string $migrationName
* @param callable $migrationFunction
*/
function applyMigration(string $migrationName, callable $migrationFunction): void
{
// Check if migration already exists.
$checkQuery = "SELECT COUNT(*) as count FROM migrations WHERE migration = :migration";
Database::query($checkQuery);
Database::bind(':migration', $migrationName);
$result = Database::fetch();

if ((int)$result['count'] === 0) {
// Run the migration.
$migrationFunction();

// Record this migration as executed.
Database::query(
"INSERT INTO migrations (migration, executed_at) VALUES (:migration, NOW())"
);
Database::bind(':migration', $migrationName);
Database::execute();
}
}
78 changes: 27 additions & 51 deletions src/migrations.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once __DIR__ . '/migrate.php';

use App\Database;

/**
Expand All @@ -9,24 +11,28 @@
*/
function createTables(): void
{
/**
* Tables' structure
*/
$tablesStructures = [
"CREATE TABLE IF NOT EXISTS `users` (
`id` INT UNSIGNED NOT NULL,
initializeMigrationsTable();

applyMigration('create_users_table', function () {
$query = "CREATE TABLE IF NOT EXISTS `users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`email` TINYTEXT NOT NULL,
`password` TINYTEXT NOT NULL,
`secret` TINYTEXT NOT NULL,
`user_token` TINYTEXT NOT NULL,
`verified` TINYINT UNSIGNED NOT NULL DEFAULT 0,
`tagline` TINYTEXT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
`updated_at` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
Database::query($query);
Database::execute();
});

"CREATE TABLE IF NOT EXISTS `posts` (
`id` INT UNSIGNED NOT NULL,
applyMigration('create_posts_table', function () {
$query = "CREATE TABLE IF NOT EXISTS `posts` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`category` TINYTEXT NOT NULL,
`title` TINYTEXT NOT NULL,
Expand All @@ -35,49 +41,19 @@ function createTables(): void
`body` MEDIUMTEXT NOT NULL,
`position` TINYINT UNSIGNED NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
];

/**
* Indexes
*/
$tablesIndexes = [
"ALTER TABLE `users` ADD PRIMARY KEY (`id`);",
"ALTER TABLE `posts` ADD PRIMARY KEY (`id`);"
];

/**
* Auto increments
*/
$tablesAutoIncrements = [
"ALTER TABLE `users` MODIFY `id` INT UNSIGNED NOT NULL AUTO_INCREMENT;",
"ALTER TABLE `posts` MODIFY `id` INT UNSIGNED NOT NULL AUTO_INCREMENT;"
];

/**
* Foreign keys
*/
$tablesForeignKeys = [
"ALTER TABLE `posts` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`);"
];

foreach ($tablesStructures as $tablesStructure) {
Database::query($tablesStructure);
`updated_at` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
Database::query($query);
Database::execute();
}
foreach ($tablesIndexes as $tablesIndex) {
Database::query($tablesIndex);
Database::execute();
}
foreach ($tablesAutoIncrements as $tablesAutoIncrement) {
Database::query($tablesAutoIncrement);
Database::execute();
}
foreach ($tablesForeignKeys as $tablesForeignKey) {
Database::query($tablesForeignKey);

// You might also include the foreign key creation here.
$fkQuery = "ALTER TABLE `posts` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`);";
Database::query($fkQuery);
Database::execute();
}
});

// Add your new migrations here

/**
* Prevent to create existed tables by commenting a command that call this function
Expand Down