diff --git a/src/composer.lock b/src/composer.lock index 8767b95..fccb767 100644 --- a/src/composer.lock +++ b/src/composer.lock @@ -12,10 +12,10 @@ "source": { "type": "git", "url": "https://git.bitbadger.solutions/bit-badger/documents-common", - "reference": "60bf3a7d97f06d49db3cacb9a6a84b129a83daa6" + "reference": "30d3ad0621485d0797f2483424a6199ab0021c97" }, "type": "library", - "time": "2024-05-31T16:06:59+00:00" + "time": "2024-06-01T02:26:15+00:00" }, { "name": "bit-badger/documents-sqlite", @@ -23,14 +23,14 @@ "source": { "type": "git", "url": "https://git.bitbadger.solutions/bit-badger/documents-sqlite", - "reference": "9378a62e7ac190ef4bbffdd4330bf83bbe39def0" + "reference": "009ea77b7510fd13936ec4927e2390bccd5d5e70" }, "require": { "bit-badger/documents-common": "dev-conversion", "ext-sqlite3": "*" }, "type": "library", - "time": "2024-05-31T16:07:51+00:00" + "time": "2024-06-01T02:28:46+00:00" } ], "packages-dev": [], diff --git a/src/lib/Data.php b/src/lib/Data.php index d8ee22d..f3c54c7 100644 --- a/src/lib/Data.php +++ b/src/lib/Data.php @@ -9,7 +9,6 @@ use BitBadger\Documents\StringMapper; use DateTimeImmutable; use DateTimeInterface; use Exception; -use FeedReaderCentral\Domain\Table; use SQLite3; /** diff --git a/src/lib/Domain/User.php b/src/lib/Domain/User.php deleted file mode 100644 index 962f712..0000000 --- a/src/lib/Domain/User.php +++ /dev/null @@ -1,17 +0,0 @@ -qualifier = Table::ITEM; + $userField = Field::EQ('user_id', $_SESSION[Key::USER_ID], '@user'); + $userField->qualifier = Table::FEED; + $fields = [$idField, $userField]; + + $where = Query::whereByFields($fields); + $item = Table::ITEM; + $feed = Table::FEED; + return Custom::single( + "SELECT $item.data FROM $item INNER JOIN $feed ON $item.data->>'feed_id' = $feed.data->>'id' WHERE $where", + Parameters::addFields($fields, []), new JsonMapper(Item::class)); + } } diff --git a/src/lib/ItemAndFeed.php b/src/lib/ItemAndFeed.php index e5807e8..38dbb6a 100644 --- a/src/lib/ItemAndFeed.php +++ b/src/lib/ItemAndFeed.php @@ -5,8 +5,6 @@ namespace FeedReaderCentral; use BitBadger\Documents\JsonMapper; use BitBadger\Documents\Mapper; use FeedReaderCentral\Domain\Feed; -use FeedReaderCentral\Domain\Item; -use FeedReaderCentral\Domain\Table; /** * A combined item and feed (used for lists) diff --git a/src/lib/Security.php b/src/lib/Security.php index d75778a..7ce5829 100644 --- a/src/lib/Security.php +++ b/src/lib/Security.php @@ -1,12 +1,9 @@ prepare('SELECT * FROM frc_user WHERE email = :email'); -// $query->bindValue(':email', $email); -// $result = $query->execute(); -// return $result ? $result->fetchArray(SQLITE3_ASSOC) : false; - } - - /** - * Add a user - * - * @param string $email The e-mail address for the user - * @param string $password The user's password - * @param SQLite3 $db The data connection to use to add the user - */ - public static function addUser(string $email, string $password, SQLite3 $db): void { - $user = new User(); - $user->email = $email; - $user->password = $password; - Document::insert(Table::USER, $user, $db); -// $query = $db->prepare('INSERT INTO frc_user (email, password) VALUES (:email, :password)'); -// $query->bindValue(':email', $email); -// $query->bindValue(':password', password_hash($password, self::PW_ALGORITHM)); -// $query->execute(); - } - /** * Verify a user's password * @@ -72,15 +36,13 @@ class Security { * @param string $password The password provided by the user * @param string|null $returnTo The URL to which the user should be redirected * @param SQLite3 $db The database connection to use to verify the user's credentials + * @throws DocumentException if any is encountered */ - private static function verifyPassword(User $user, string $password, ?string $returnTo, SQLite3 $db): void { + private static function verifyPassword(User $user, string $password, ?string $returnTo, SQLite3 $db): void + { if (password_verify($password, $user->password)) { if (password_needs_rehash($user->password, self::PW_ALGORITHM)) { Patch::byId(Table::USER, $user->id, ['password' => password_hash($password, self::PW_ALGORITHM)], $db); -// $rehash = $db->prepare('UPDATE frc_user SET password = :hash WHERE id = :id'); -// $rehash->bindValue(':hash', password_hash($password, self::PW_ALGORITHM)); -// $rehash->bindValue(':id', $user['id']); -// $rehash->execute(); } $_SESSION[Key::USER_ID] = $user['id']; $_SESSION[Key::USER_EMAIL] = $user['email']; @@ -95,6 +57,7 @@ class Security { * @param string $password The password provided by the user * @param string|null $returnTo The URL to which the user should be redirected * @param SQLite3 $db The database connection to use to verify the user's credentials + * @throws DocumentException If any is encountered */ public static function logOnUser(string $email, string $password, ?string $returnTo, SQLite3 $db): void { if (SECURITY_MODEL == self::SINGLE_USER_WITH_PASSWORD) { @@ -106,7 +69,7 @@ class Security { } $dbEmail = $email; } - $user = self::findUserByEmail($dbEmail, $db); + $user = User::findByEmail($dbEmail); if ($user) self::verifyPassword($user, $password, $returnTo, $db); add_error('Invalid credentials; log on unsuccessful'); } @@ -117,26 +80,24 @@ class Security { * @param string $email The e-mail address of the user whose password should be updated * @param string $password The new password for this user * @param SQLite3 $db The database connection to use in updating the password + * @throws DocumentException If any is encountered */ public static function updatePassword(string $email, string $password, SQLite3 $db): void { - Patch::byField(Table::USER, Field::EQ('email', $email), + Patch::byFields(Table::USER, [Field::EQ('email', $email)], ['password' => password_hash($password, self::PW_ALGORITHM)], $db); -// $query = $db->prepare('UPDATE frc_user SET password = :password WHERE email = :email'); -// $query->bindValue(':password', password_hash($password, self::PW_ALGORITHM)); -// $query->bindValue(':email', $email); -// $query->execute(); } /** * Log on the single user * * @param SQLite3 $db The data connection to use to retrieve the user + * @throws DocumentException If any is encountered */ private static function logOnSingleUser(SQLite3 $db): void { - $user = self::findUserByEmail(self::SINGLE_USER_EMAIL, $db); + $user = User::findByEmail(self::SINGLE_USER_EMAIL); if (!$user) { - self::addUser(self::SINGLE_USER_EMAIL, self::SINGLE_USER_PASSWORD, $db); - $user = self::findUserByEmail(self::SINGLE_USER_EMAIL, $db); + User::add(self::SINGLE_USER_EMAIL, self::SINGLE_USER_PASSWORD, $db); + $user = User::findByEmail(self::SINGLE_USER_EMAIL); } self::verifyPassword($user, self::SINGLE_USER_PASSWORD, $_GET['returnTo'], $db); } @@ -146,6 +107,7 @@ class Security { * * @param SQLite3 $db The data connection to use if required * @param bool $redirectIfAnonymous Whether to redirect the request if there is no user logged on + * @throws DocumentException If any is encountered */ public static function verifyUser(SQLite3 $db, bool $redirectIfAnonymous = true): void { if (key_exists(Key::USER_ID, $_SESSION)) return; diff --git a/src/lib/Domain/Table.php b/src/lib/Table.php similarity index 89% rename from src/lib/Domain/Table.php rename to src/lib/Table.php index b9c5a95..c4e2b75 100644 --- a/src/lib/Domain/Table.php +++ b/src/lib/Table.php @@ -1,5 +1,5 @@ email = $email; + $user->password = $password; + Document::insert(Table::USER, $user, $db); + } + +} diff --git a/src/public/bookmark.php b/src/public/bookmark.php index b0ec25f..5100f12 100644 --- a/src/public/bookmark.php +++ b/src/public/bookmark.php @@ -10,10 +10,10 @@ use BitBadger\Documents\DocumentException; use BitBadger\Documents\SQLite\Find; use BitBadger\Documents\SQLite\Patch; use FeedReaderCentral\Data; -use FeedReaderCentral\Domain\Item; -use FeedReaderCentral\Domain\Table; +use FeedReaderCentral\Item; use FeedReaderCentral\Key; use FeedReaderCentral\Security; +use FeedReaderCentral\Table; include '../start.php'; @@ -33,11 +33,11 @@ $exists = $existsResult ? $existsResult->fetchArray(SQLITE3_ASSOC) : false; if (!$exists) not_found(); if (key_exists('action', $_GET)) { - if ($_GET['action'] == 'add') { - $flag = 1; - } elseif ($_GET['action'] == 'remove') { - $flag = 0; - } + $flag = match ($_GET['action']) { + 'add' => 1, + 'remove' => 0, + default => null + }; if (isset($flag)) { try { Patch::byId(Table::ITEM, $id, ['is_bookmarked' => $flag], $db); diff --git a/src/public/feed/index.php b/src/public/feed/index.php index 00e40fe..a442cdf 100644 --- a/src/public/feed/index.php +++ b/src/public/feed/index.php @@ -9,9 +9,9 @@ use BitBadger\Documents\DocumentException; use BitBadger\Documents\Field; use BitBadger\Documents\SQLite\Delete; use FeedReaderCentral\Data; -use FeedReaderCentral\Domain\Table; use FeedReaderCentral\Feed; use FeedReaderCentral\Security; +use FeedReaderCentral\Table; include '../../start.php'; @@ -57,12 +57,15 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { } if ($feedId == 'new') { - $title = 'Add RSS Feed'; - $feed = ['id' => $_GET['id'], 'url' => '']; + $title = 'Add RSS Feed'; + $feed = new Feed(); + $feed->id = $_GET['id']; } else { $title = 'Edit RSS Feed'; if ($feedId == 'error') { - $feed = ['id' => $_POST['id'] ?? '', 'url' => $_POST['url'] ?? '']; + $feed = new Feed(); + $feed->id = $_POST['id'] ?? ''; + $feed->url = $_POST['url'] ?? ''; } elseif (!($feed = Feed::retrieveById((int)$feedId))) not_found(); } @@ -70,10 +73,10 @@ page_head($title); ?>