From 9e027ca51ebf1c99dc7b5a98e939d8e45ea8b32e Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Thu, 4 Apr 2024 19:15:58 -0400 Subject: [PATCH 01/18] Add initial data tables (#2) --- .gitignore | 2 ++ src/data/.gitkeep | 0 src/lib/Data.php | 62 +++++++++++++++++++++++++++++++++++++++++++++++ src/start.php | 20 +++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 .gitignore create mode 100644 src/data/.gitkeep create mode 100644 src/lib/Data.php create mode 100644 src/start.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..73afc4d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +src/data/*.db diff --git a/src/data/.gitkeep b/src/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/lib/Data.php b/src/lib/Data.php new file mode 100644 index 0000000..d134744 --- /dev/null +++ b/src/lib/Data.php @@ -0,0 +1,62 @@ +exec('PRAGMA foreign_keys = ON;'); + return $db; + } + + public static function ensureDb(): void { + $db = self::getConnection(); + $tables = $db->query("SELECT name FROM sqlite_master WHERE type = 'table'")->fetchArray(SQLITE3_NUM); + if (!array_search('frc_user', $tables)) { + $query = <<exec($query); + $db->exec('CREATE INDEX idx_user_email ON frc_user (email)'); + } + if (!array_search('feed', $tables)) { + $query = <<exec($query); + } + if (!array_search('item', $tables)) { + $query = <<exec($query); + } + $db->close(); + } +} + +Data::ensureDb(); diff --git a/src/start.php b/src/start.php new file mode 100644 index 0000000..50517c1 --- /dev/null +++ b/src/start.php @@ -0,0 +1,20 @@ + Date: Thu, 4 Apr 2024 20:49:25 -0400 Subject: [PATCH 02/18] Fix table creation (#2) - Add Caddyfile - Add start of vanilla layout --- src/Caddyfile | 9 +++++++++ src/lib/Data.php | 12 +++++++----- src/public/index.php | 8 ++++++++ src/start.php | 20 ++++++++++++++++++++ 4 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/Caddyfile create mode 100644 src/public/index.php diff --git a/src/Caddyfile b/src/Caddyfile new file mode 100644 index 0000000..ea7cbbf --- /dev/null +++ b/src/Caddyfile @@ -0,0 +1,9 @@ +{ + frankenphp + order php_server before file_server +} +http://localhost:8205 { + root ./public + try_files {uri} {uri}.php + php_server +} diff --git a/src/lib/Data.php b/src/lib/Data.php index d134744..39dea42 100644 --- a/src/lib/Data.php +++ b/src/lib/Data.php @@ -9,15 +9,17 @@ class Data { * @return SQLite3 A new connection to the database */ private static function getConnection(): SQLite3 { - $db = new SQLite3("../data/{${DATABASE_NAME}}"); + $db = new SQLite3('../data/' . DATABASE_NAME); $db->exec('PRAGMA foreign_keys = ON;'); return $db; } public static function ensureDb(): void { $db = self::getConnection(); - $tables = $db->query("SELECT name FROM sqlite_master WHERE type = 'table'")->fetchArray(SQLITE3_NUM); - if (!array_search('frc_user', $tables)) { + $tables = array(); + $tableQuery = $db->query("SELECT name FROM sqlite_master WHERE type = 'table'"); + while ($table = $tableQuery->fetchArray(SQLITE3_NUM)) $tables[] = $table[0]; + if (!in_array('frc_user', $tables)) { $query = <<exec($query); $db->exec('CREATE INDEX idx_user_email ON frc_user (email)'); } - if (!array_search('feed', $tables)) { + if (!in_array('feed', $tables)) { $query = <<exec($query); } - if (!array_search('item', $tables)) { + if (!in_array('item', $tables)) { $query = << +

Startup worked

+ + + + <?=$title?> | Feed Reader Central + + Date: Fri, 5 Apr 2024 22:14:24 -0400 Subject: [PATCH 03/18] Add single user security mode (#3) - Tweaks to SQL column definitions - Implement class autoloading - Split user config into its own file --- src/lib/Data.php | 47 ++++++++++++++++++++++++++++++++++------ src/lib/Security.php | 51 ++++++++++++++++++++++++++++++++++++++++++++ src/public/index.php | 4 +++- src/start.php | 27 +++++++++-------------- src/user-config.php | 23 ++++++++++++++++++++ 5 files changed, 127 insertions(+), 25 deletions(-) create mode 100644 src/lib/Security.php create mode 100644 src/user-config.php diff --git a/src/lib/Data.php b/src/lib/Data.php index 39dea42..ee9062b 100644 --- a/src/lib/Data.php +++ b/src/lib/Data.php @@ -14,6 +14,9 @@ class Data { return $db; } + /** + * Make sure the expected tables exist + */ public static function ensureDb(): void { $db = self::getConnection(); $tables = array(); @@ -24,8 +27,7 @@ class Data { CREATE TABLE frc_user ( id INTEGER NOT NULL PRIMARY KEY, email TEXT NOT NULL, - password TEXT NOT NULL, - salt TEXT NOT NULL) + password TEXT NOT NULL) SQL; $db->exec($query); $db->exec('CREATE INDEX idx_user_email ON frc_user (email)'); @@ -50,15 +52,46 @@ class Data { published_on TEXT NOT NULL, updated_on TEXT, content TEXT NOT NULL, - is_encoded INTEGER NOT NULL, - is_read INTEGER NOT NULL, - is_bookmarked INTEGER NOT NULL, + is_encoded BOOLEAN NOT NULL, + is_read BOOLEAN NOT NULL, + is_bookmarked BOOLEAN NOT NULL, FOREIGN KEY (feed_id) REFERENCES feed (id)) SQL; $db->exec($query); } $db->close(); } -} -Data::ensureDb(); + /** + * Find a user by their ID + * + * @param string $email The e-mail address of the user to retrieve + * @return array|null The user information, or null if the user is not found + */ + public static function findUserByEmail(string $email): ?array { + $db = self::getConnection(); + $query = $db->prepare('SELECT * FROM frc_user WHERE email = :email'); + $query->bindValue(':email', $email); + $result = $query->execute(); + if ($result) { + $user = $result->fetchArray(SQLITE3_ASSOC); + if ($user) return $user; + return null; + } + return null; + } + + /** + * Add a user + * + * @param string $email The e-mail address for the user + * @param string $password The user's password + */ + public static function addUser(string $email, string $password): void { + $db = self::getConnection(); + $query = $db->prepare('INSERT INTO frc_user (email, password) VALUES (:email, :password)'); + $query->bindValue(':email', $email); + $query->bindValue(':password', password_hash($password, PASSWORD_DEFAULT)); + $query->execute(); + } +} diff --git a/src/lib/Security.php b/src/lib/Security.php new file mode 100644 index 0000000..9ab5d77 --- /dev/null +++ b/src/lib/Security.php @@ -0,0 +1,51 @@ + -

Startup worked

+

User ID - e-mail

Date: Sat, 6 Apr 2024 11:02:48 -0400 Subject: [PATCH 04/18] WIP on add/edit feed page (#4) --- src/Caddyfile | 2 +- src/lib/Data.php | 8 +++++--- src/public/assets/style.css | 35 +++++++++++++++++++++++++++++++++++ src/public/feed.php | 30 ++++++++++++++++++++++++++++++ src/public/index.php | 8 +++++++- src/start.php | 18 ++++++++++++++++-- 6 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 src/public/assets/style.css create mode 100644 src/public/feed.php diff --git a/src/Caddyfile b/src/Caddyfile index ea7cbbf..96e6cbc 100644 --- a/src/Caddyfile +++ b/src/Caddyfile @@ -4,6 +4,6 @@ } http://localhost:8205 { root ./public - try_files {uri} {uri}.php + try_files {path} {path}.php php_server } diff --git a/src/lib/Data.php b/src/lib/Data.php index ee9062b..2bd5e7a 100644 --- a/src/lib/Data.php +++ b/src/lib/Data.php @@ -23,7 +23,7 @@ class Data { $tableQuery = $db->query("SELECT name FROM sqlite_master WHERE type = 'table'"); while ($table = $tableQuery->fetchArray(SQLITE3_NUM)) $tables[] = $table[0]; if (!in_array('frc_user', $tables)) { - $query = <<exec('CREATE INDEX idx_user_email ON frc_user (email)'); } if (!in_array('feed', $tables)) { - $query = <<exec($query); } if (!in_array('item', $tables)) { - $query = << $_GET['id'], 'url' => '' ]; +} +?> +

Add/Edit Feed

+
+ + +
+ -

User ID - e-mail

+ <?=$title?> | Feed Reader Central + - +
+
Feed Reader Central
+
Add Feed'; + if ($_REQUEST['FRC_USER_EMAIL'] != 'solouser@example.com') { + echo " | {$_REQUEST['FRC_USER_EMAIL']}"; + } + } ?> +
+
+
+
Date: Mon, 8 Apr 2024 07:31:32 -0400 Subject: [PATCH 05/18] WIP on add/edit feed page (#4) Mostly style tweaks --- src/public/assets/style.css | 23 +++++++++++++++++++++-- src/public/feed.php | 26 ++++++++++++++++---------- src/start.php | 4 ++-- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/public/assets/style.css b/src/public/assets/style.css index 06edb2a..1706a50 100644 --- a/src/public/assets/style.css +++ b/src/public/assets/style.css @@ -5,11 +5,12 @@ html { body { margin: 0; font-size: 1rem; - background-color: #eeeeee; + background: linear-gradient(135deg, #eeeeff, #ddddff, #eeeeff, #ccccff); + /* background-color: #eeeeee; */ } header { padding: 0 1rem .5rem 1rem; - background: linear-gradient(#000064, #000048, #000032); + background: linear-gradient(#000032, #000048, #000064); border-bottom-left-radius: .5rem; border-bottom-right-radius: .5rem; color: white; @@ -33,3 +34,21 @@ header { main { padding: 0 .5rem; } +article { + max-width: 60rem; + margin: auto; +} +input[type=url], input[type=text] { + width: 50%; + font-size: 1rem; + padding: .25rem; + border-radius: .25rem; +} +button { + font-size: 1rem; + background-color: navy; + color: white; + padding: .25rem 1rem; + border-radius: .25rem; + cursor: pointer; +} \ No newline at end of file diff --git a/src/public/feed.php b/src/public/feed.php index fdfa09d..ef1d0f4 100644 --- a/src/public/feed.php +++ b/src/public/feed.php @@ -8,23 +8,29 @@ include '../start.php'; Security::verifyUser(); -page_head('Feed page'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { // TODO: get feed, add if new, reject if existing but not owned by this user, update otherwise - $feed = array(); + $feed = [ 'id' => $_POST['id'], 'url' => $_POST['url'] ]; + $title = 'TODO'; } else { // TODO: Retrieve feed by ID if not new $feed = [ 'id' => $_GET['id'], 'url' => '' ]; + $title = 'Add RSS Feed'; } +page_head($title); + ?> -

Add/Edit Feed

-
- - -
+

+
+
+ > +
+ +
+
- + <?=$title?> | Feed Reader Central
-
Feed Reader Central
+ Feed Reader Central
Add Feed'; From 5c92c8c7d6701c11c96a8a586784b9320b1a782e Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 9 Apr 2024 21:16:34 -0400 Subject: [PATCH 06/18] Retrieve feed, add to database (#4) --- src/lib/Data.php | 34 ++++++++++++++++++++++ src/lib/Feed.php | 69 +++++++++++++++++++++++++++++++++++++++++++++ src/public/feed.php | 1 + 3 files changed, 104 insertions(+) create mode 100644 src/lib/Feed.php diff --git a/src/lib/Data.php b/src/lib/Data.php index 2bd5e7a..e8e8b91 100644 --- a/src/lib/Data.php +++ b/src/lib/Data.php @@ -96,4 +96,38 @@ class Data { $query->bindValue(':password', password_hash($password, PASSWORD_DEFAULT)); $query->execute(); } + + /** + * Add an RSS feed + * @param string $url The URL for the RSS feed + * @param string $title The title of the RSS feed + * @param string $updatedOn The date/time the RSS feed was last updated (from the XML, not when we checked) + * @return int The ID of the added feed + */ + public static function addFeed(string $url, string $title, string $updatedOn): int { + $db = self::getConnection(); + if ($updatedOn) { + try { + $updated = (new DateTimeImmutable($updatedOn))->format(DateTimeInterface::ATOM); + } catch (Exception $ex) { + $updated = null; + } + } else { + $updated = null; + } + $query = $db->prepare('INSERT INTO feed (user_id, url, title, updated_on, checked_on)' + . ' VALUES (:user, :url, :title, :updated, :checked)'); + $query->bindValue(':user', $_REQUEST['FRC_USER_ID']); + $query->bindValue(':url', $url); + $query->bindValue(':title', $title); + $query->bindValue(':updated', $updated); + $query->bindValue(':checked', (new DateTimeImmutable())->format(DateTimeInterface::ATOM)); + $result = $query->execute(); + if ($result) { + $idQuery = $db->prepare('SELECT last_insert_rowid()'); + $idResult = $idQuery->execute(); + if ($idResult) return $idResult->fetchArray(SQLITE3_NUM)[0]; + } + return -1; + } } diff --git a/src/lib/Feed.php b/src/lib/Feed.php new file mode 100644 index 0000000..0a1cfd0 --- /dev/null +++ b/src/lib/Feed.php @@ -0,0 +1,69 @@ + feed ] if successful, [ 'error' => message] if not + */ + public static function parseFeed(string $content): array { + try { + return [ 'ok' => new SimpleXMLElement($content) ]; + } catch (Exception $ex) { + return [ 'error' => $ex->getMessage() ]; + } + } + + /** + * Retrieve the feed + * @param string $url + * @return array|SimpleXMLElement[]|string[] [ 'ok' => feedXml, 'url' => actualUrl ] if successful, [ 'error' => message ] if not + */ + public static function retrieveFeed(string $url): array { + $feedReq = curl_init($url); + curl_setopt($feedReq, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($feedReq, CURLOPT_RETURNTRANSFER, true); + curl_setopt($feedReq, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($feedReq, CURLOPT_TIMEOUT, 15); + + $feedContent = curl_exec($feedReq); + + $result = array(); + $error = curl_error($feedReq); + $code = curl_getinfo($feedReq, CURLINFO_RESPONSE_CODE); + if ($error) { + $result['error'] = $error; + } else if ($code == 200) { + $parsed = self::parseFeed($feedContent); + if (array_key_exists('error', $parsed)) { + $result['error'] = $parsed['error']; + } else { + $result['ok'] = $parsed['ok']; + $result['url'] = curl_getinfo($feedReq, CURLINFO_EFFECTIVE_URL); + } + } else { + $result['error'] = "HTTP Code $code: $feedContent"; + } + + curl_close($feedReq); + return $result; + } + + /** + * Add an RSS feed + * @param string $url The URL of the RSS feed to add + * @return array [ 'ok' => true ] if successful, [ 'error' => message ] if not + */ + public static function add(string $url): array { + $feed = self::retrieveFeed($url); + if (array_key_exists('error', $feed)) return $feed; + + $channel = $feed['ok']->channel; + $feedId = Data::addFeed($feed['url'], (string) $channel->title, (string) $channel->lastBuildDate); + + return [ 'ok' => true ]; + } +} diff --git a/src/public/feed.php b/src/public/feed.php index ef1d0f4..59b4014 100644 --- a/src/public/feed.php +++ b/src/public/feed.php @@ -11,6 +11,7 @@ Security::verifyUser(); if ($_SERVER['REQUEST_METHOD'] == 'POST') { // TODO: get feed, add if new, reject if existing but not owned by this user, update otherwise + $result = Feed::add($_POST['url']); $feed = [ 'id' => $_POST['id'], 'url' => $_POST['url'] ]; $title = 'TODO'; } else { From 0530ed0dc9aa6fd13b6d1e9eb505d47e8194cb53 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 9 Apr 2024 23:14:53 -0400 Subject: [PATCH 07/18] WIP on adding feed items (#4) --- src/lib/Data.php | 57 ++++++++++++++++++++++++++++++++++++++------- src/lib/Feed.php | 37 +++++++++++++++++++++++++++++ src/public/feed.php | 1 + 3 files changed, 86 insertions(+), 9 deletions(-) diff --git a/src/lib/Data.php b/src/lib/Data.php index e8e8b91..dcd599b 100644 --- a/src/lib/Data.php +++ b/src/lib/Data.php @@ -51,12 +51,14 @@ class Data { id INTEGER NOT NULL PRIMARY KEY, feed_id INTEGER NOT NULL, title TEXT NOT NULL, + item_guid TEXT NOT NULL, + item_link TEXT NOT NULL, published_on TEXT NOT NULL, updated_on TEXT, content TEXT NOT NULL, is_encoded BOOLEAN NOT NULL, - is_read BOOLEAN NOT NULL, - is_bookmarked BOOLEAN NOT NULL, + is_read BOOLEAN NOT NULL DEFAULT 0, + is_bookmarked BOOLEAN NOT NULL DEFAULT 0, FOREIGN KEY (feed_id) REFERENCES feed (id)) SQL; $db->exec($query); @@ -109,7 +111,7 @@ class Data { if ($updatedOn) { try { $updated = (new DateTimeImmutable($updatedOn))->format(DateTimeInterface::ATOM); - } catch (Exception $ex) { + } catch (Exception) { $updated = null; } } else { @@ -123,11 +125,48 @@ class Data { $query->bindValue(':updated', $updated); $query->bindValue(':checked', (new DateTimeImmutable())->format(DateTimeInterface::ATOM)); $result = $query->execute(); - if ($result) { - $idQuery = $db->prepare('SELECT last_insert_rowid()'); - $idResult = $idQuery->execute(); - if ($idResult) return $idResult->fetchArray(SQLITE3_NUM)[0]; - } - return -1; + return $result ? $db->lastInsertRowID() : -1; + } + + /** + * Does a feed item already exist? + * @param int $feedId The ID of the feed to which the item belongs + * @param string $guid The GUID from the RSS feed, uniquely identifying the item + * @return bool True if the item exists, false if not + */ + public static function itemExists(int $feedId, string $guid): bool { + $db = self::getConnection(); + $query = $db->prepare('SELECT COUNT(*) FROM item WHERE feed_id = :feed AND item_guid = :guid'); + $query->bindValue(':feed', $feedId); + $query->bindValue(':guid', $guid); + $result = $query->execute(); + return $result && $result->fetchArray(SQLITE3_NUM)[0] == 1; + } + + /** + * Add a feed item + * @param int $feedId The ID of the feed to which the item should be added + * @param string $guid The GUID from the RSS feed (uses link if `` not specified) + * @param string $link The link to this item + * @param string $title The title of the item + * @param string $published The date/time the item was published + * @param string $content The content of the item + * @param bool $isEncoded Whether the content has HTML (true) or is plaintext (false) + * @throws Exception If the published date is not valid + */ + public static function addItem(int $feedId, string $guid, string $link, string $title, string $published, + string $content, bool $isEncoded): void { + $db = self::getConnection(); + $query = $db->prepare( + 'INSERT INTO item (feed_id, item_guid, item_link, title, published_on, content, is_encoded)' + . ' VALUES (:feed, :guid, :link, :title, :published, :content, :encoded)'); + $query->bindValue(':feed', $feedId); + $query->bindValue(':guid', $guid); + $query->bindValue(':link', $link); + $query->bindValue(':title', $title); + $query->bindValue(':published', (new DateTimeImmutable($published))->format(DateTimeInterface::ATOM)); + $query->bindValue(':content', $content); + $query->bindValue(':encoded', $isEncoded); + $query->execute(); } } diff --git a/src/lib/Feed.php b/src/lib/Feed.php index 0a1cfd0..1b0e09a 100644 --- a/src/lib/Feed.php +++ b/src/lib/Feed.php @@ -52,6 +52,40 @@ class Feed { return $result; } + /** + * Update a feed's items + * @param int $feedId The ID of the feed to which these items belong + * @param SimpleXMLElement $channel The RSS feed items + * @return array [ 'ok' => true ] if successful, [ 'error' => message ] if not + */ + public static function updateItems(int $feedId, SimpleXMLElement $channel): array { + try { + for ($i = 0; $i < sizeof($channel->item); $i++) { + $item = $channel->item[$i]; + $itemGuid = (string)$item->guid ? $item->guid : $item->link; + $isNew = !Data::itemExists($feedId, $itemGuid); + if ($isNew) { + $title = (string)$item->title; + $link = (string)$item->link; + $published = (string)$item->pubDate; + // TODO: why is this getting all encoded content, and not just the one for the current item? + $encodedContent = $item->xpath('//content:encoded'); + if ($encodedContent) { + $content = (string) $encodedContent[$i]; + $isEncoded = true; + } else { + $content = $item->description; + $isEncoded = false; + } + Data::addItem($feedId, $itemGuid, $link, $title, $published, $content, $isEncoded); + } // TODO: else check updated date; may want to return that from the isNew check instead + } + } catch (Exception $ex) { + return [ 'error' => $ex->getMessage() ]; + } + return [ 'ok', true ]; + } + /** * Add an RSS feed * @param string $url The URL of the RSS feed to add @@ -64,6 +98,9 @@ class Feed { $channel = $feed['ok']->channel; $feedId = Data::addFeed($feed['url'], (string) $channel->title, (string) $channel->lastBuildDate); + $result = self::updateItems($feedId, $channel); + if (array_key_exists('error', $result)) return $result; + return [ 'ok' => true ]; } } diff --git a/src/public/feed.php b/src/public/feed.php index 59b4014..1092b7f 100644 --- a/src/public/feed.php +++ b/src/public/feed.php @@ -12,6 +12,7 @@ Security::verifyUser(); if ($_SERVER['REQUEST_METHOD'] == 'POST') { // TODO: get feed, add if new, reject if existing but not owned by this user, update otherwise $result = Feed::add($_POST['url']); + echo '
'; var_dump($result); echo '
'; $feed = [ 'id' => $_POST['id'], 'url' => $_POST['url'] ]; $title = 'TODO'; } else { From 8ca4bf2109be046ed6c22f95ad45def556180323 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Wed, 10 Apr 2024 20:50:45 -0400 Subject: [PATCH 08/18] Change from SimpleXML to DOM (#4) This API is more reliable, and should help when implementing the "load a site's HTML and look for feed links" functionality coming before the final release --- src/lib/Data.php | 78 ++++++++++++++++++++++++++----------------- src/lib/Feed.php | 87 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 115 insertions(+), 50 deletions(-) diff --git a/src/lib/Data.php b/src/lib/Data.php index dcd599b..0969f36 100644 --- a/src/lib/Data.php +++ b/src/lib/Data.php @@ -99,8 +99,23 @@ class Data { $query->execute(); } + /** + * Parse/format a date/time from a string + * + * @param ?string $value The date/time to be parsed and formatted + * @return string|null The date/time in `DateTimeInterface::ATOM` format, or `null` if the input cannot be parsed + */ + private static function formatDate(?string $value): ?string { + try { + return $value ? (new DateTimeImmutable($value))->format(DateTimeInterface::ATOM) : null; + } catch (Exception) { + return null; + } + } + /** * Add an RSS feed + * * @param string $url The URL for the RSS feed * @param string $title The title of the RSS feed * @param string $updatedOn The date/time the RSS feed was last updated (from the XML, not when we checked) @@ -108,28 +123,25 @@ class Data { */ public static function addFeed(string $url, string $title, string $updatedOn): int { $db = self::getConnection(); - if ($updatedOn) { - try { - $updated = (new DateTimeImmutable($updatedOn))->format(DateTimeInterface::ATOM); - } catch (Exception) { - $updated = null; - } - } else { - $updated = null; - } - $query = $db->prepare('INSERT INTO feed (user_id, url, title, updated_on, checked_on)' - . ' VALUES (:user, :url, :title, :updated, :checked)'); - $query->bindValue(':user', $_REQUEST['FRC_USER_ID']); - $query->bindValue(':url', $url); - $query->bindValue(':title', $title); - $query->bindValue(':updated', $updated); - $query->bindValue(':checked', (new DateTimeImmutable())->format(DateTimeInterface::ATOM)); + $query = $db->prepare(<<<'SQL' + INSERT INTO feed ( + user_id, url, title, updated_on, checked_on + ) VALUES ( + :user, :url, :title, :updated, :checked + ) + SQL); + $query->bindValue(':user', $_REQUEST['FRC_USER_ID']); + $query->bindValue(':url', $url); + $query->bindValue(':title', $title); + $query->bindValue(':updated', self::formatDate($updatedOn)); + $query->bindValue(':checked', self::formatDate('now')); $result = $query->execute(); return $result ? $db->lastInsertRowID() : -1; } /** * Does a feed item already exist? + * * @param int $feedId The ID of the feed to which the item belongs * @param string $guid The GUID from the RSS feed, uniquely identifying the item * @return bool True if the item exists, false if not @@ -145,28 +157,34 @@ class Data { /** * Add a feed item + * * @param int $feedId The ID of the feed to which the item should be added * @param string $guid The GUID from the RSS feed (uses link if `` not specified) * @param string $link The link to this item * @param string $title The title of the item - * @param string $published The date/time the item was published + * @param string $publishedOn The date/time the item was published + * @param ?string $updatedOn The date/time the item was last updated * @param string $content The content of the item * @param bool $isEncoded Whether the content has HTML (true) or is plaintext (false) - * @throws Exception If the published date is not valid */ - public static function addItem(int $feedId, string $guid, string $link, string $title, string $published, - string $content, bool $isEncoded): void { + public static function addItem(int $feedId, string $guid, string $link, string $title, string $publishedOn, + ?string $updatedOn, string $content, bool $isEncoded): void { $db = self::getConnection(); - $query = $db->prepare( - 'INSERT INTO item (feed_id, item_guid, item_link, title, published_on, content, is_encoded)' - . ' VALUES (:feed, :guid, :link, :title, :published, :content, :encoded)'); - $query->bindValue(':feed', $feedId); - $query->bindValue(':guid', $guid); - $query->bindValue(':link', $link); - $query->bindValue(':title', $title); - $query->bindValue(':published', (new DateTimeImmutable($published))->format(DateTimeInterface::ATOM)); - $query->bindValue(':content', $content); - $query->bindValue(':encoded', $isEncoded); + $query = $db->prepare(<<<'SQL' + INSERT INTO item ( + feed_id, item_guid, item_link, title, published_on, updated_on, content, is_encoded + ) VALUES ( + :feed, :guid, :link, :title, :published, :updated, :content, :encoded + ) + SQL); + $query->bindValue(':feed', $feedId); + $query->bindValue(':guid', $guid); + $query->bindValue(':link', $link); + $query->bindValue(':title', $title); + $query->bindValue(':published', self::formatDate($publishedOn)); + $query->bindValue(':updated', self::formatDate($updatedOn)); + $query->bindValue(':content', $content); + $query->bindValue(':encoded', $isEncoded); $query->execute(); } } diff --git a/src/lib/Feed.php b/src/lib/Feed.php index 1b0e09a..fc8120d 100644 --- a/src/lib/Feed.php +++ b/src/lib/Feed.php @@ -4,23 +4,52 @@ */ class Feed { + /** @var string The XML namespace for Atom feeds */ + public const ATOM_NS = 'http://www.w3.org/2005/Atom'; + + /** @var string The XML namespace for the `` tag that allows HTML content in a feed */ + public const CONTENT_NS = 'http://purl.org/rss/1.0/modules/content/'; + + /** + * When parsing XML into a DOMDocument, errors are presented as warnings; this creates an exception for them + * + * @param int $errno The error level encountered + * @param string $errstr The text of the error encountered + * @return bool False, to delegate to the next error handler in the chain + * @throws DOMException If the error is a warning + */ + private static function xmlParseError(int $errno, string $errstr): bool { + if ($errno == E_WARNING && substr_count($errstr, 'DOMDocument::loadXml()') > 0) { + throw new DOMException($errstr, $errno); + } + return false; + } + /** * Parse a feed into an XML tree + * * @param string $content The feed's RSS content - * @return array|SimpleXMLElement[]|string[] [ 'ok' => feed ] if successful, [ 'error' => message] if not + * @return array|DOMDocument[]|string[] [ 'ok' => feed ] if successful, [ 'error' => message] if not */ public static function parseFeed(string $content): array { + set_error_handler(self::xmlParseError(...)); try { - return [ 'ok' => new SimpleXMLElement($content) ]; - } catch (Exception $ex) { + $feed = new DOMDocument(); + $feed->loadXML($content); + return [ 'ok' => $feed ]; + } catch (DOMException $ex) { return [ 'error' => $ex->getMessage() ]; + } finally { + restore_error_handler(); } } /** * Retrieve the feed + * * @param string $url - * @return array|SimpleXMLElement[]|string[] [ 'ok' => feedXml, 'url' => actualUrl ] if successful, [ 'error' => message ] if not + * @return array|DOMDocument[]|string[] [ 'ok' => feedXml, 'url' => actualUrl ] if successful, + * [ 'error' => message ] if not */ public static function retrieveFeed(string $url): array { $feedReq = curl_init($url); @@ -52,32 +81,46 @@ class Feed { return $result; } + /** + * Get the value of a child element by its tag name + * + * @param DOMElement $element The parent element + * @param string $tagName The name of the tag whose value should be obtained + * @return string The value of the element (or "[element] not found" if that element does not exist) + */ + private static function eltValue(DOMElement $element, string $tagName): string { + $tags = $element->getElementsByTagName($tagName); + return $tags->length == 0 ? "$tagName not found" : $tags->item(0)->textContent; + } + /** * Update a feed's items + * * @param int $feedId The ID of the feed to which these items belong - * @param SimpleXMLElement $channel The RSS feed items + * @param DOMElement $channel The RSS feed items * @return array [ 'ok' => true ] if successful, [ 'error' => message ] if not */ - public static function updateItems(int $feedId, SimpleXMLElement $channel): array { + public static function updateItems(int $feedId, DOMElement $channel): array { try { - for ($i = 0; $i < sizeof($channel->item); $i++) { - $item = $channel->item[$i]; - $itemGuid = (string)$item->guid ? $item->guid : $item->link; + foreach ($channel->getElementsByTagName('item') as $item) { + $itemGuid = self::eltValue($item, 'guid'); + if ($itemGuid == 'guid not found') $itemGuid = self::eltValue($item, 'link'); $isNew = !Data::itemExists($feedId, $itemGuid); if ($isNew) { - $title = (string)$item->title; - $link = (string)$item->link; - $published = (string)$item->pubDate; - // TODO: why is this getting all encoded content, and not just the one for the current item? - $encodedContent = $item->xpath('//content:encoded'); - if ($encodedContent) { - $content = (string) $encodedContent[$i]; + $title = self::eltValue($item, 'title'); + $link = self::eltValue($item, 'link'); + $published = self::eltValue($item, 'pubDate'); + $updNodes = $item->getElementsByTagNameNS(self::ATOM_NS, 'updated'); + $updated = $updNodes->length > 0 ? $updNodes->item(0)->textContent : null; + $encNodes = $item->getElementsByTagNameNS(self::CONTENT_NS, 'encoded'); + if ($encNodes->length > 0) { + $content = $encNodes->item(0)->textContent; $isEncoded = true; } else { - $content = $item->description; + $content = self::eltValue($item, 'description'); $isEncoded = false; } - Data::addItem($feedId, $itemGuid, $link, $title, $published, $content, $isEncoded); + Data::addItem($feedId, $itemGuid, $link, $title, $published, $updated, $content, $isEncoded); } // TODO: else check updated date; may want to return that from the isNew check instead } } catch (Exception $ex) { @@ -88,6 +131,7 @@ class Feed { /** * Add an RSS feed + * * @param string $url The URL of the RSS feed to add * @return array [ 'ok' => true ] if successful, [ 'error' => message ] if not */ @@ -95,8 +139,11 @@ class Feed { $feed = self::retrieveFeed($url); if (array_key_exists('error', $feed)) return $feed; - $channel = $feed['ok']->channel; - $feedId = Data::addFeed($feed['url'], (string) $channel->title, (string) $channel->lastBuildDate); + $channel = $feed['ok']->getElementsByTagName('channel')->item(0); + if (!$channel instanceof DOMElement) return [ 'error' => "Channel element not found ($channel->nodeType)" ]; + + $feedId = Data::addFeed($feed['url'], self::eltValue($channel, 'title'), + self::eltValue($channel, 'lastBuildDate')); $result = self::updateItems($feedId, $channel); if (array_key_exists('error', $result)) return $result; From 7d294b9be86bf8eca226618f39d3c79ecb1754a3 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Thu, 11 Apr 2024 19:07:39 -0400 Subject: [PATCH 09/18] Add date for RSS feeds (#4) - First cut of user message add/display --- src/lib/Data.php | 4 ++-- src/lib/Feed.php | 16 +++++++++++----- src/public/feed.php | 12 +++++++----- src/start.php | 26 +++++++++++++++++++++----- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/lib/Data.php b/src/lib/Data.php index 0969f36..59b9595 100644 --- a/src/lib/Data.php +++ b/src/lib/Data.php @@ -118,10 +118,10 @@ class Data { * * @param string $url The URL for the RSS feed * @param string $title The title of the RSS feed - * @param string $updatedOn The date/time the RSS feed was last updated (from the XML, not when we checked) + * @param ?string $updatedOn The date/time the RSS feed was last updated (from the XML, not when we checked) * @return int The ID of the added feed */ - public static function addFeed(string $url, string $title, string $updatedOn): int { + public static function addFeed(string $url, string $title, ?string $updatedOn): int { $db = self::getConnection(); $query = $db->prepare(<<<'SQL' INSERT INTO feed ( diff --git a/src/lib/Feed.php b/src/lib/Feed.php index fc8120d..70574d8 100644 --- a/src/lib/Feed.php +++ b/src/lib/Feed.php @@ -5,10 +5,10 @@ class Feed { /** @var string The XML namespace for Atom feeds */ - public const ATOM_NS = 'http://www.w3.org/2005/Atom'; + public const string ATOM_NS = 'http://www.w3.org/2005/Atom'; - /** @var string The XML namespace for the `` tag that allows HTML content in a feed */ - public const CONTENT_NS = 'http://purl.org/rss/1.0/modules/content/'; + /** @var string The XML namespace for the `` tag that allows HTML content in a feed */ + public const string CONTENT_NS = 'http://purl.org/rss/1.0/modules/content/'; /** * When parsing XML into a DOMDocument, errors are presented as warnings; this creates an exception for them @@ -142,8 +142,14 @@ class Feed { $channel = $feed['ok']->getElementsByTagName('channel')->item(0); if (!$channel instanceof DOMElement) return [ 'error' => "Channel element not found ($channel->nodeType)" ]; - $feedId = Data::addFeed($feed['url'], self::eltValue($channel, 'title'), - self::eltValue($channel, 'lastBuildDate')); + // In Atom feeds, lastBuildDate contains the last time an item in the feed was updated; if that is not present, + // use the pubDate element instead + $updated = self::eltValue($channel, 'lastBuildDate'); + if ($updated == 'lastBuildDate not found') { + $updated = self::eltValue($channel, 'pubDate'); + if ($updated == 'pubDate not found') $updated = null; + } + $feedId = Data::addFeed($feed['url'], self::eltValue($channel, 'title'), $updated); $result = self::updateItems($feedId, $channel); if (array_key_exists('error', $result)) return $result; diff --git a/src/public/feed.php b/src/public/feed.php index 1092b7f..82ac36a 100644 --- a/src/public/feed.php +++ b/src/public/feed.php @@ -12,7 +12,11 @@ Security::verifyUser(); if ($_SERVER['REQUEST_METHOD'] == 'POST') { // TODO: get feed, add if new, reject if existing but not owned by this user, update otherwise $result = Feed::add($_POST['url']); - echo '
'; var_dump($result); echo '
'; + if (array_key_exists('ok', $result)) { + add_message('INFO', 'Feed added successfully'); + } else { + add_message('ERROR', $result['error']); + } $feed = [ 'id' => $_POST['id'], 'url' => $_POST['url'] ]; $title = 'TODO'; } else { @@ -20,9 +24,8 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $feed = [ 'id' => $_GET['id'], 'url' => '' ]; $title = 'Add RSS Feed'; } -page_head($title); -?> +page_head($title); ?>

@@ -33,6 +36,5 @@ page_head($title);
-
- $level, 'message' => $message]; +} + /** * Render the page title * @param string $title The title of the page being displayed @@ -29,14 +40,19 @@ function page_head(string $title): void {
Add Feed'; - if ($_REQUEST['FRC_USER_EMAIL'] != 'solouser@example.com') { - echo " | {$_REQUEST['FRC_USER_EMAIL']}"; - } + if ($_REQUEST['FRC_USER_EMAIL'] != 'solouser@example.com') echo " | {$_REQUEST['FRC_USER_EMAIL']}"; } ?>
-
- +
+ {$msg['level']}
"?> + +
Date: Thu, 11 Apr 2024 22:01:36 -0400 Subject: [PATCH 10/18] Add editing of feed URL (#4) - Move feed-specific database calls to Feed class - Detect when feed items have been updated - Add const keys for $_REQUEST values --- src/lib/Data.php | 119 ++++++++---------------- src/lib/Feed.php | 211 +++++++++++++++++++++++++++++++++++-------- src/lib/Key.php | 13 +++ src/lib/Security.php | 6 +- src/public/feed.php | 34 +++++-- src/public/index.php | 6 +- src/start.php | 30 ++++-- 7 files changed, 275 insertions(+), 144 deletions(-) create mode 100644 src/lib/Key.php diff --git a/src/lib/Data.php b/src/lib/Data.php index 59b9595..7ac179d 100644 --- a/src/lib/Data.php +++ b/src/lib/Data.php @@ -8,7 +8,7 @@ class Data { * Obtain a new connection to the database * @return SQLite3 A new connection to the database */ - private static function getConnection(): SQLite3 { + public static function getConnection(): SQLite3 { $db = new SQLite3('../data/' . DATABASE_NAME); $db->exec('PRAGMA foreign_keys = ON;'); return $db; @@ -74,15 +74,19 @@ class Data { */ public static function findUserByEmail(string $email): ?array { $db = self::getConnection(); - $query = $db->prepare('SELECT * FROM frc_user WHERE email = :email'); - $query->bindValue(':email', $email); - $result = $query->execute(); - if ($result) { - $user = $result->fetchArray(SQLITE3_ASSOC); - if ($user) return $user; + try { + $query = $db->prepare('SELECT * FROM frc_user WHERE email = :email'); + $query->bindValue(':email', $email); + $result = $query->execute(); + if ($result) { + $user = $result->fetchArray(SQLITE3_ASSOC); + if ($user) return $user; + return null; + } return null; + } finally { + $db->close(); } - return null; } /** @@ -93,10 +97,14 @@ class Data { */ public static function addUser(string $email, string $password): void { $db = self::getConnection(); - $query = $db->prepare('INSERT INTO frc_user (email, password) VALUES (:email, :password)'); - $query->bindValue(':email', $email); - $query->bindValue(':password', password_hash($password, PASSWORD_DEFAULT)); - $query->execute(); + try { + $query = $db->prepare('INSERT INTO frc_user (email, password) VALUES (:email, :password)'); + $query->bindValue(':email', $email); + $query->bindValue(':password', password_hash($password, PASSWORD_DEFAULT)); + $query->execute(); + } finally { + $db->close(); + } } /** @@ -105,7 +113,7 @@ class Data { * @param ?string $value The date/time to be parsed and formatted * @return string|null The date/time in `DateTimeInterface::ATOM` format, or `null` if the input cannot be parsed */ - private static function formatDate(?string $value): ?string { + public static function formatDate(?string $value): ?string { try { return $value ? (new DateTimeImmutable($value))->format(DateTimeInterface::ATOM) : null; } catch (Exception) { @@ -114,77 +122,22 @@ class Data { } /** - * Add an RSS feed + * Retrieve a feed by its ID for the current user * - * @param string $url The URL for the RSS feed - * @param string $title The title of the RSS feed - * @param ?string $updatedOn The date/time the RSS feed was last updated (from the XML, not when we checked) - * @return int The ID of the added feed + * @param int $feedId The ID of the feed to retrieve + * @param ?SQLite3 $dbConn A database connection to use (optional; will use standalone if not provided) + * @return array|bool The data for the feed if found, false if not found */ - public static function addFeed(string $url, string $title, ?string $updatedOn): int { - $db = self::getConnection(); - $query = $db->prepare(<<<'SQL' - INSERT INTO feed ( - user_id, url, title, updated_on, checked_on - ) VALUES ( - :user, :url, :title, :updated, :checked - ) - SQL); - $query->bindValue(':user', $_REQUEST['FRC_USER_ID']); - $query->bindValue(':url', $url); - $query->bindValue(':title', $title); - $query->bindValue(':updated', self::formatDate($updatedOn)); - $query->bindValue(':checked', self::formatDate('now')); - $result = $query->execute(); - return $result ? $db->lastInsertRowID() : -1; - } - - /** - * Does a feed item already exist? - * - * @param int $feedId The ID of the feed to which the item belongs - * @param string $guid The GUID from the RSS feed, uniquely identifying the item - * @return bool True if the item exists, false if not - */ - public static function itemExists(int $feedId, string $guid): bool { - $db = self::getConnection(); - $query = $db->prepare('SELECT COUNT(*) FROM item WHERE feed_id = :feed AND item_guid = :guid'); - $query->bindValue(':feed', $feedId); - $query->bindValue(':guid', $guid); - $result = $query->execute(); - return $result && $result->fetchArray(SQLITE3_NUM)[0] == 1; - } - - /** - * Add a feed item - * - * @param int $feedId The ID of the feed to which the item should be added - * @param string $guid The GUID from the RSS feed (uses link if `` not specified) - * @param string $link The link to this item - * @param string $title The title of the item - * @param string $publishedOn The date/time the item was published - * @param ?string $updatedOn The date/time the item was last updated - * @param string $content The content of the item - * @param bool $isEncoded Whether the content has HTML (true) or is plaintext (false) - */ - public static function addItem(int $feedId, string $guid, string $link, string $title, string $publishedOn, - ?string $updatedOn, string $content, bool $isEncoded): void { - $db = self::getConnection(); - $query = $db->prepare(<<<'SQL' - INSERT INTO item ( - feed_id, item_guid, item_link, title, published_on, updated_on, content, is_encoded - ) VALUES ( - :feed, :guid, :link, :title, :published, :updated, :content, :encoded - ) - SQL); - $query->bindValue(':feed', $feedId); - $query->bindValue(':guid', $guid); - $query->bindValue(':link', $link); - $query->bindValue(':title', $title); - $query->bindValue(':published', self::formatDate($publishedOn)); - $query->bindValue(':updated', self::formatDate($updatedOn)); - $query->bindValue(':content', $content); - $query->bindValue(':encoded', $isEncoded); - $query->execute(); + public static function retrieveFeedById(int $feedId, ?SQLite3 $dbConn = null): array|bool { + $db = $dbConn ?? self::getConnection(); + try { + $query = $db->prepare('SELECT * FROM feed WHERE id = :id AND user_id = :user'); + $query->bindValue(':id', $feedId); + $query->bindValue(':user', $_REQUEST[Key::USER_ID]); + $result = $query->execute(); + return $result ? $result->fetchArray(SQLITE3_ASSOC) : false; + } finally { + if (is_null($dbConn)) $db->close(); + } } } diff --git a/src/lib/Feed.php b/src/lib/Feed.php index 70574d8..cfbcfbd 100644 --- a/src/lib/Feed.php +++ b/src/lib/Feed.php @@ -29,16 +29,16 @@ class Feed { * Parse a feed into an XML tree * * @param string $content The feed's RSS content - * @return array|DOMDocument[]|string[] [ 'ok' => feed ] if successful, [ 'error' => message] if not + * @return array|DOMDocument[]|string[] ['ok' => feed] if successful, ['error' => message] if not */ public static function parseFeed(string $content): array { set_error_handler(self::xmlParseError(...)); try { $feed = new DOMDocument(); $feed->loadXML($content); - return [ 'ok' => $feed ]; + return ['ok' => $feed]; } catch (DOMException $ex) { - return [ 'error' => $ex->getMessage() ]; + return ['error' => $ex->getMessage()]; } finally { restore_error_handler(); } @@ -48,8 +48,8 @@ class Feed { * Retrieve the feed * * @param string $url - * @return array|DOMDocument[]|string[] [ 'ok' => feedXml, 'url' => actualUrl ] if successful, - * [ 'error' => message ] if not + * @return array|DOMDocument[]|string[] ['ok' => feedXml, 'url' => actualUrl] if successful, ['error' => message] if + * not */ public static function retrieveFeed(string $url): array { $feedReq = curl_init($url); @@ -93,53 +93,123 @@ class Feed { return $tags->length == 0 ? "$tagName not found" : $tags->item(0)->textContent; } + /** + * Extract the fields we need to keep from the feed + * + * @param DOMElement $item The item from the feed + * @return array The fields for the item as an associative array + */ + private static function itemFields(DOMElement $item): array { + $itemGuid = self::eltValue($item, 'guid'); + $updNodes = $item->getElementsByTagNameNS(self::ATOM_NS, 'updated'); + $encNodes = $item->getElementsByTagNameNS(self::CONTENT_NS, 'encoded'); + return [ + 'guid' => $itemGuid == 'guid not found' ? self::eltValue($item, 'link') : $itemGuid, + 'title' => self::eltValue($item, 'title'), + 'link' => self::eltValue($item, 'link'), + 'published' => Data::formatDate(self::eltValue($item, 'pubDate')), + 'updated' => Data::formatDate($updNodes->length > 0 ? $updNodes->item(0)->textContent : null), + 'content' => $encNodes->length > 0 ? $encNodes->item(0)->textContent + : self::eltValue($item, 'description'), + 'isEncoded' => $encNodes->length > 0 + ]; + } + + /** + * Update a feed item + * + * @param int $itemId The ID of the item to be updated + * @param array $item The fields from the updated item + * @param SQLite3 $db A database connection to use for the update + */ + private static function updateItem(int $itemId, array $item, SQLite3 $db): void { + $query = $db->prepare(<<<'SQL' + UPDATE item + SET title = :title, + published_on = :published, + updated_on = :updated, + content = :content, + is_encoded = :encoded, + is_read = 0 + WHERE id = :id + SQL); + $query->bindValue(':title', $item['title']); + $query->bindValue(':published', $item['published']); + $query->bindValue(':updated', $item['updated']); + $query->bindValue(':content', $item['content']); + $query->bindValue(':encoded', $item['isEncoded']); + $query->bindValue(':id', $itemId); + $query->execute(); + } + + /** + * Add a feed item + * + * @param int $feedId The ID of the feed to which the item should be added + * @param array $item The fields for the item + * @param SQLite3 $db A database connection to use for the addition + */ + private static function addItem(int $feedId, array $item, SQLite3 $db): void { + $query = $db->prepare(<<<'SQL' + INSERT INTO item ( + feed_id, item_guid, item_link, title, published_on, updated_on, content, is_encoded + ) VALUES ( + :feed, :guid, :link, :title, :published, :updated, :content, :encoded + ) + SQL); + $query->bindValue(':feed', $feedId); + $query->bindValue(':guid', $item['guid']); + $query->bindValue(':link', $item['link']); + $query->bindValue(':title', $item['title']); + $query->bindValue(':published', $item['published']); + $query->bindValue(':updated', $item['updated']); + $query->bindValue(':content', $item['content']); + $query->bindValue(':encoded', $item['isEncoded']); + $query->execute(); + } + /** * Update a feed's items * * @param int $feedId The ID of the feed to which these items belong * @param DOMElement $channel The RSS feed items - * @return array [ 'ok' => true ] if successful, [ 'error' => message ] if not + * @return array ['ok' => true] if successful, ['error' => message] if not */ - public static function updateItems(int $feedId, DOMElement $channel): array { + public static function updateItems(int $feedId, DOMElement $channel, SQLite3 $db): array { try { - foreach ($channel->getElementsByTagName('item') as $item) { - $itemGuid = self::eltValue($item, 'guid'); - if ($itemGuid == 'guid not found') $itemGuid = self::eltValue($item, 'link'); - $isNew = !Data::itemExists($feedId, $itemGuid); - if ($isNew) { - $title = self::eltValue($item, 'title'); - $link = self::eltValue($item, 'link'); - $published = self::eltValue($item, 'pubDate'); - $updNodes = $item->getElementsByTagNameNS(self::ATOM_NS, 'updated'); - $updated = $updNodes->length > 0 ? $updNodes->item(0)->textContent : null; - $encNodes = $item->getElementsByTagNameNS(self::CONTENT_NS, 'encoded'); - if ($encNodes->length > 0) { - $content = $encNodes->item(0)->textContent; - $isEncoded = true; - } else { - $content = self::eltValue($item, 'description'); - $isEncoded = false; + foreach ($channel->getElementsByTagName('item') as $rawItem) { + $item = self::itemFields($rawItem); + $existsQuery = $db->prepare( + 'SELECT id, published_on, updated_on FROM item WHERE feed_id = :feed AND item_guid = :guid'); + $existsQuery->bindValue(':feed', $feedId); + $existsQuery->bindValue(':guid', $item['guid']); + $exists = $existsQuery->execute(); + if ($exists) { + $existing = $exists->fetchArray(SQLITE3_ASSOC); + if ( $existing + && ( $existing['published_on'] != $item['published'] + || $existing['updated_on'] ?? '' != $item['updated'] ?? '')) { + self::updateItem($existing['id'], $item, $db); } - Data::addItem($feedId, $itemGuid, $link, $title, $published, $updated, $content, $isEncoded); - } // TODO: else check updated date; may want to return that from the isNew check instead + } else { + self::addItem($feedId, $item, $db); + } } } catch (Exception $ex) { - return [ 'error' => $ex->getMessage() ]; + return ['error' => $ex->getMessage()]; } - return [ 'ok', true ]; + return ['ok', true]; } /** - * Add an RSS feed + * Find the `` element and derive the published/last updated date from the feed * - * @param string $url The URL of the RSS feed to add - * @return array [ 'ok' => true ] if successful, [ 'error' => message ] if not + * @param DOMDocument $feed The feed from which the information should be extracted + * @return array|string[]|DOMElement[] ['channel' => channel, 'updated' => date] if successful, ['error' => message] + * if not */ - public static function add(string $url): array { - $feed = self::retrieveFeed($url); - if (array_key_exists('error', $feed)) return $feed; - - $channel = $feed['ok']->getElementsByTagName('channel')->item(0); + private static function findChannelAndDate(DOMDocument $feed): array { + $channel = $feed->getElementsByTagName('channel')->item(0); if (!$channel instanceof DOMElement) return [ 'error' => "Channel element not found ($channel->nodeType)" ]; // In Atom feeds, lastBuildDate contains the last time an item in the feed was updated; if that is not present, @@ -149,11 +219,74 @@ class Feed { $updated = self::eltValue($channel, 'pubDate'); if ($updated == 'pubDate not found') $updated = null; } - $feedId = Data::addFeed($feed['url'], self::eltValue($channel, 'title'), $updated); + return ['channel' => $channel, 'updated' => Data::formatDate($updated)]; + } - $result = self::updateItems($feedId, $channel); + /** + * Add an RSS feed + * + * @param string $url The URL of the RSS feed to add + * @return array ['ok' => feedId] if successful, ['error' => message] if not + */ + public static function add(string $url, SQLite3 $db): array { + $feed = self::retrieveFeed($url); + if (array_key_exists('error', $feed)) return $feed; + + $channelAndDate = self::findChannelAndDate($feed['ok']); + if (array_key_exists('error', $channelAndDate)) return $channelAndDate; + $channel = $channelAndDate['channel']; + + $query = $db->prepare(<<<'SQL' + INSERT INTO feed (user_id, url, title, updated_on, checked_on) + VALUES (:user, :url, :title, :updated, :checked) + SQL); + $query->bindValue(':user', $_REQUEST[Key::USER_ID]); + $query->bindValue(':url', $feed['url']); + $query->bindValue(':title', self::eltValue($channel, 'title')); + $query->bindValue(':updated', $channelAndDate['updated']); + $query->bindValue(':checked', Data::formatDate('now')); + $result = $query->execute(); + + $feedId = $result ? $db->lastInsertRowID() : -1; + if ($feedId < 0) return ['error' => $db->lastErrorMsg()]; + + $result = self::updateItems($feedId, $channel, $db); if (array_key_exists('error', $result)) return $result; - return [ 'ok' => true ]; + return ['ok' => $feedId]; + } + + /** + * Update an RSS feed + * + * @param array $existing The existing RSS feed + * @param string $url The URL with which the existing feed should be modified + * @return bool[]|string[] [ 'ok' => true ] if successful, [ 'error' => message ] if not + */ + public static function update(array $existing, string $url, SQLite3 $db): array { + $feed = self::retrieveFeed($url); + if (array_key_exists('error', $feed)) return $feed; + + $channelAndDate = self::findChannelAndDate($feed['ok']); + if (array_key_exists('error', $channelAndDate)) return $channelAndDate; + $channel = $channelAndDate['channel']; + + $query = $db->prepare(<<<'SQL' + UPDATE feed + SET url = :url, title = :title, updated_on = :updated, checked_on = :checked + WHERE id = :id AND user_id = :user + SQL); + $query->bindValue(':url', $feed['url']); + $query->bindValue(':title', self::eltValue($channel, 'title')); + $query->bindValue(':updated', $channelAndDate['updated']); + $query->bindValue(':checked', Data::formatDate('now')); + $query->bindValue(':id', $existing['id']); + $query->bindValue(':user', $_REQUEST[Key::USER_ID]); + $query->execute(); + + $result = self::updateItems($existing['id'], $channel, $db); + if (array_key_exists('error', $result)) return $result; + + return ['ok' => true]; } } diff --git a/src/lib/Key.php b/src/lib/Key.php new file mode 100644 index 0000000..39e7bfd --- /dev/null +++ b/src/lib/Key.php @@ -0,0 +1,13 @@ + "Feed {$_POST['id']} not found" ]; } - $feed = [ 'id' => $_POST['id'], 'url' => $_POST['url'] ]; - $title = 'TODO'; -} else { - // TODO: Retrieve feed by ID if not new + if (array_key_exists('ok', $result)) { + add_info('Feed saved successfully'); + $feedId = $isNew ? $result['ok'] : $_POST['id']; + } else { + add_error($result['error']); + } +} + +if ($feedId == 'new') { $feed = [ 'id' => $_GET['id'], 'url' => '' ]; $title = 'Add RSS Feed'; +} else { + $feed = Data::retrieveFeedById((int) $feedId, $db); + if (!$feed) { + http_response_code(404); + die(); + } + $title = 'Edit RSS Feed'; } page_head($title); ?> @@ -38,3 +53,4 @@ page_head($title); ?> close(); diff --git a/src/public/index.php b/src/public/index.php index f077ba3..b4a1f38 100644 --- a/src/public/index.php +++ b/src/public/index.php @@ -9,8 +9,6 @@ include '../start.php'; Security::verifyUser(); -page_head('Welcome'); -?> - - +

Unread items go here

$level, 'message' => $message]; + if (!array_key_exists(Key::USER_MSG, $_REQUEST)) $_REQUEST[Key::USER_MSG] = array(); + $_REQUEST[Key::USER_MSG][] = ['level' => $level, 'message' => $message]; +} + +/** + * Add an error message to be displayed at the top of the page + * + * @param string $message The message to be displayed + */ +function add_error(string $message): void { + add_message('ERROR', $message); +} + +/** + * Add an error message to be displayed at the top of the page + * + * @param string $message The message to be displayed + */ +function add_info(string $message): void { + add_message('INFO', $message); } /** @@ -38,15 +56,15 @@ function page_head(string $title): void {
Feed Reader Central
Add Feed'; - if ($_REQUEST['FRC_USER_EMAIL'] != 'solouser@example.com') echo " | {$_REQUEST['FRC_USER_EMAIL']}"; + if ($_REQUEST[Key::USER_EMAIL] != 'solouser@example.com') echo " | {$_REQUEST[Key::USER_EMAIL]}"; } ?>
+ if (array_key_exists(Key::USER_MSG, $_REQUEST)) { + foreach ($_REQUEST[Key::USER_MSG] as $msg) { ?>
{$msg['level']}
"?> From 480228013c7a45e9c9d39827cfc2398e58a12f73 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 12 Apr 2024 07:27:19 -0400 Subject: [PATCH 11/18] Handle 404s on feed edit page (#4) --- src/public/feed.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/public/feed.php b/src/public/feed.php index 798c00e..3a6bafa 100644 --- a/src/public/feed.php +++ b/src/public/feed.php @@ -25,19 +25,24 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $feedId = $isNew ? $result['ok'] : $_POST['id']; } else { add_error($result['error']); + $feedId = 'error'; } } if ($feedId == 'new') { - $feed = [ 'id' => $_GET['id'], 'url' => '' ]; $title = 'Add RSS Feed'; + $feed = [ 'id' => $_GET['id'], 'url' => '' ]; } else { - $feed = Data::retrieveFeedById((int) $feedId, $db); - if (!$feed) { - http_response_code(404); - die(); - } $title = 'Edit RSS Feed'; + if ($feedId == 'error') { + $feed = ['id' => $_POST['id'] ?? '', 'url' => $_POST['url'] ?? '']; + } else { + $feed = Data::retrieveFeedById((int) $feedId, $db); + if (!$feed) { + http_response_code(404); + die(); + } + } } page_head($title); ?> From d9dc3ec3613ba3e3bb91a423f883d146e711f12e Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 12 Apr 2024 18:29:25 -0400 Subject: [PATCH 12/18] Add URL to feed error message (#4) --- src/lib/Feed.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/Feed.php b/src/lib/Feed.php index cfbcfbd..f1b8d88 100644 --- a/src/lib/Feed.php +++ b/src/lib/Feed.php @@ -74,7 +74,7 @@ class Feed { $result['url'] = curl_getinfo($feedReq, CURLINFO_EFFECTIVE_URL); } } else { - $result['error'] = "HTTP Code $code: $feedContent"; + $result['error'] = "Prospective feed URL $url returned HTTP Code $code: $feedContent"; } curl_close($feedReq); From 9b2190252fadbd991ccee430d2636c8e4ce0f17f Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sat, 13 Apr 2024 12:10:07 -0400 Subject: [PATCH 13/18] List unread items on home page (#6) - Fix feed update (latent bug on #4) --- src/lib/Feed.php | 4 +++- src/public/assets/style.css | 15 +++++++++------ src/public/feed.php | 2 +- src/public/index.php | 31 ++++++++++++++++++++++++++++++- src/user-config.php | 7 +++++++ 5 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/lib/Feed.php b/src/lib/Feed.php index f1b8d88..2a45ab8 100644 --- a/src/lib/Feed.php +++ b/src/lib/Feed.php @@ -190,9 +190,11 @@ class Feed { && ( $existing['published_on'] != $item['published'] || $existing['updated_on'] ?? '' != $item['updated'] ?? '')) { self::updateItem($existing['id'], $item, $db); + } else { + self::addItem($feedId, $item, $db); } } else { - self::addItem($feedId, $item, $db); + throw new Exception($db->lastErrorMsg()); } } } catch (Exception $ex) { diff --git a/src/public/assets/style.css b/src/public/assets/style.css index 1706a50..bc42762 100644 --- a/src/public/assets/style.css +++ b/src/public/assets/style.css @@ -5,8 +5,15 @@ html { body { margin: 0; font-size: 1rem; - background: linear-gradient(135deg, #eeeeff, #ddddff, #eeeeff, #ccccff); - /* background-color: #eeeeee; */ + background: linear-gradient(135deg, #eeeeff, #ddddff, #eeeeff, #ccccff) fixed; +} +a:link, a:visited { + text-decoration: none; + font-weight: bold; + color: navy; +} +a:hover { + text-decoration: underline; } header { padding: 0 1rem .5rem 1rem; @@ -25,10 +32,6 @@ header { a:link, a:visited { color: white; - text-decoration: none; - } - a:hover { - text-decoration: underline; } } main { diff --git a/src/public/feed.php b/src/public/feed.php index 3a6bafa..85446c2 100644 --- a/src/public/feed.php +++ b/src/public/feed.php @@ -18,7 +18,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $result = Feed::add($_POST['url'], $db); } else { $toEdit = Data::retrieveFeedById($_POST['id'], $db); - $result = $toEdit ? Feed::update($toEdit, $_POST['url'], $db) : [ 'error' => "Feed {$_POST['id']} not found" ]; + $result = $toEdit ? Feed::update($toEdit, $_POST['url'], $db) : ['error' => "Feed {$_POST['id']} not found"]; } if (array_key_exists('ok', $result)) { add_info('Feed saved successfully'); diff --git a/src/public/index.php b/src/public/index.php index b4a1f38..852558e 100644 --- a/src/public/index.php +++ b/src/public/index.php @@ -9,6 +9,35 @@ include '../start.php'; Security::verifyUser(); +$db = Data::getConnection(); +$result = $db->query(<<<'SQL' + SELECT item.id, item.title AS item_title, coalesce(item.updated_on, item.published_on) AS as_of, + feed.title AS feed_title + FROM item + INNER JOIN feed ON feed.id = item.feed_id + WHERE item.is_read = 0 + ORDER BY coalesce(item.updated_on, item.published_on) DESC + SQL); +$item = $result ? $result->fetchArray(SQLITE3_ASSOC) : false; + page_head('Welcome'); ?> -

Unread items go here

Your Unread Items +
format(DATE_TIME_FORMAT); + } catch (Exception) { + $asOf = '(invalid date)'; + } ?> +

>
+
fetchArray(SQLITE3_ASSOC); + } +} else { ?> +

There are no unread items

+
close(); diff --git a/src/user-config.php b/src/user-config.php index 877d61a..55af358 100644 --- a/src/user-config.php +++ b/src/user-config.php @@ -19,5 +19,12 @@ const SECURITY_MODEL = Security::SINGLE_USER; /** The name of the database file where users and feeds should be kept */ const DATABASE_NAME = 'frc.db'; +/** + * The format for date/time outputs; see https://www.php.net/manual/en/datetime.format.php for acceptable values + * + * The default, 'F j, Y \a\t g:ia', equates to "August 17, 2023 at 4:45pm" + */ +const DATE_TIME_FORMAT = 'F j, Y \a\t g:ia'; + // END USER CONFIGURATION ITEMS // (editing below this line is not advised) From ee86eeef13d15e58e4d9da3e5c5e4b5c8bdf1297 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sat, 13 Apr 2024 13:39:49 -0400 Subject: [PATCH 14/18] Add item read page (#7) - Open items links in an external window - Include "Keep as New" button --- src/public/assets/style.css | 38 ++++++++++++++++-- src/public/index.php | 11 ++---- src/public/item.php | 79 +++++++++++++++++++++++++++++++++++++ src/start.php | 30 ++++++++++++++ 4 files changed, 147 insertions(+), 11 deletions(-) create mode 100644 src/public/item.php diff --git a/src/public/assets/style.css b/src/public/assets/style.css index bc42762..86bd961 100644 --- a/src/public/assets/style.css +++ b/src/public/assets/style.css @@ -36,10 +36,26 @@ header { } main { padding: 0 .5rem; + + .item_heading { + margin-bottom: 0; + } + + .item_published { + margin-bottom: 1rem; + line-height: 1.2; + } } article { max-width: 60rem; margin: auto; + + .item_content { + border: solid 1px navy; + border-radius: .5rem; + background-color: white; + padding: .5rem; + } } input[type=url], input[type=text] { width: 50%; @@ -47,11 +63,27 @@ input[type=url], input[type=text] { padding: .25rem; border-radius: .25rem; } -button { +button, +.action_buttons a:link, +.action_buttons a:visited { font-size: 1rem; + font-weight: normal; background-color: navy; color: white; - padding: .25rem 1rem; + padding: .5rem 1rem; border-radius: .25rem; cursor: pointer; -} \ No newline at end of file + border: none; +} +button:hover, +.action_buttons a:hover { + text-decoration: none; + cursor: pointer; + background: linear-gradient(navy, #000032); +} +.action_buttons { + margin: 1rem 0; + display: flex; + flex-flow: row nowrap; + justify-content: space-evenly; +} diff --git a/src/public/index.php b/src/public/index.php index 852558e..799d67d 100644 --- a/src/public/index.php +++ b/src/public/index.php @@ -24,14 +24,9 @@ page_head('Welcome'); ?>

Your Unread Items

format(DATE_TIME_FORMAT); - } catch (Exception) { - $asOf = '(invalid date)'; - } ?> -

>
-
+

>
+
fetchArray(SQLITE3_ASSOC); } } else { ?> diff --git a/src/public/item.php b/src/public/item.php new file mode 100644 index 0000000..59a1630 --- /dev/null +++ b/src/public/item.php @@ -0,0 +1,79 @@ +prepare(<<<'SQL' + SELECT COUNT(*) + FROM item INNER JOIN feed ON feed.id = item.feed_id + WHERE item.id = :id AND feed.user_id = :user + SQL); + $isValidQuery->bindValue(':id', $_POST['id']); + $isValidQuery->bindValue(':user', $_REQUEST[Key::USER_ID]); + $isValidResult = $isValidQuery->execute(); + if ($isValidResult && $isValidResult->fetchArray(SQLITE3_NUM)[0] == 1) { + $keepUnread = $db->prepare('UPDATE item SET is_read = 0 WHERE id = :id'); + $keepUnread->bindValue(':id', $_POST['id']); + $keepUnread->execute(); + } + $db->close(); + frc_redirect('/'); +} + +$query = $db->prepare(<<<'SQL' + SELECT item.title AS item_title, item.item_link, item.published_on, item.updated_on, item.content, item.is_encoded, + feed.title AS feed_title + FROM item INNER JOIN feed ON feed.id = item.feed_id + WHERE item.id = :id + AND feed.user_id = :user + SQL); +$query->bindValue(':id', $_GET['id']); +$query->bindValue(':user', $_REQUEST[Key::USER_ID]); +$result = $query->execute(); +$item = $result ? $result->fetchArray(SQLITE3_ASSOC) : false; + +if ($item) { + $markRead = $db->prepare('UPDATE item SET is_read = 1 WHERE id = :id'); + $markRead->bindValue(':id', $_GET['id']); + $markRead->execute(); +} + +$published = date_time($item['published_on']); +$updated = isset($item['updated_on']) ? date_time($item['updated_on']) : null; +$isEncoded = (bool) $item['is_encoded']; + +page_head(htmlentities("{$item['item_title']} | {$item['feed_title']}")); ?> +

+
+

+
+ From
+ Published +
+
+
+
+
+ > + Done + +
+
close(); diff --git a/src/start.php b/src/start.php index 2d5ce21..78624af 100644 --- a/src/start.php +++ b/src/start.php @@ -49,6 +49,7 @@ function page_head(string $title): void { ?> + <?=$title?> | Feed Reader Central @@ -79,3 +80,32 @@ function page_head(string $title): void { function page_foot(): void { ?>
format(DATE_TIME_FORMAT); + } catch (Exception) { + return '(invalid date)'; + } +} From 77d628ed695633e2045f3f7ad2ee63a474ca7a35 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sat, 13 Apr 2024 22:32:39 -0400 Subject: [PATCH 15/18] Add feed refresh link (#5) - Feed updates and refresh use the same logic - Fixed issue with logic around item add-or-update --- src/lib/Feed.php | 161 +++++++++++++++++++++++++++---------------- src/public/index.php | 14 +++- src/start.php | 7 +- 3 files changed, 117 insertions(+), 65 deletions(-) diff --git a/src/lib/Feed.php b/src/lib/Feed.php index 2a45ab8..3c5a6af 100644 --- a/src/lib/Feed.php +++ b/src/lib/Feed.php @@ -44,12 +44,25 @@ class Feed { } } + /** + * Get the value of a child element by its tag name + * + * @param DOMElement $element The parent element + * @param string $tagName The name of the tag whose value should be obtained + * @return string The value of the element (or "[element] not found" if that element does not exist) + */ + private static function eltValue(DOMElement $element, string $tagName): string { + $tags = $element->getElementsByTagName($tagName); + return $tags->length == 0 ? "$tagName not found" : $tags->item(0)->textContent; + } + /** * Retrieve the feed * * @param string $url - * @return array|DOMDocument[]|string[] ['ok' => feedXml, 'url' => actualUrl] if successful, ['error' => message] if - * not + * @return array|DOMDocument[]|string[]|DOMElement[] + * ['ok' => feedXml, 'url' => actualUrl, 'channel' => channel, 'updated' => updatedDate] if successful, + * ['error' => message] if not */ public static function retrieveFeed(string $url): array { $feedReq = curl_init($url); @@ -72,6 +85,24 @@ class Feed { } else { $result['ok'] = $parsed['ok']; $result['url'] = curl_getinfo($feedReq, CURLINFO_EFFECTIVE_URL); + + $channel = $result['ok']->getElementsByTagName('channel')->item(0); + if ($channel instanceof DOMElement) { + $result['channel'] = $channel; + } else { + return ['error' => "Channel element not found ($channel->nodeType)"]; + } + + // In Atom feeds, lastBuildDate contains the last time an item in the feed was updated; if that is not + // present, use the pubDate element instead + $updated = self::eltValue($channel, 'lastBuildDate'); + if ($updated == 'lastBuildDate not found') { + $updated = self::eltValue($channel, 'pubDate'); + if ($updated == 'pubDate not found') $updated = null; + } + $result['updated'] = Data::formatDate($updated); + return $result; + } } else { $result['error'] = "Prospective feed URL $url returned HTTP Code $code: $feedContent"; @@ -81,18 +112,6 @@ class Feed { return $result; } - /** - * Get the value of a child element by its tag name - * - * @param DOMElement $element The parent element - * @param string $tagName The name of the tag whose value should be obtained - * @return string The value of the element (or "[element] not found" if that element does not exist) - */ - private static function eltValue(DOMElement $element, string $tagName): string { - $tags = $element->getElementsByTagName($tagName); - return $tags->length == 0 ? "$tagName not found" : $tags->item(0)->textContent; - } - /** * Extract the fields we need to keep from the feed * @@ -186,10 +205,11 @@ class Feed { $exists = $existsQuery->execute(); if ($exists) { $existing = $exists->fetchArray(SQLITE3_ASSOC); - if ( $existing - && ( $existing['published_on'] != $item['published'] - || $existing['updated_on'] ?? '' != $item['updated'] ?? '')) { - self::updateItem($existing['id'], $item, $db); + if ($existing) { + if ( $existing['published_on'] != $item['published'] + || $existing['updated_on'] ?? '' != $item['updated'] ?? '') { + self::updateItem($existing['id'], $item, $db); + } } else { self::addItem($feedId, $item, $db); } @@ -204,24 +224,43 @@ class Feed { } /** - * Find the `` element and derive the published/last updated date from the feed + * Refresh a feed * - * @param DOMDocument $feed The feed from which the information should be extracted - * @return array|string[]|DOMElement[] ['channel' => channel, 'updated' => date] if successful, ['error' => message] - * if not + * @param string $url The URL of the feed to be refreshed + * @param SQLite3 $db A database connection to use to refresh the feed + * @return array|string[]|true[] ['ok' => true] if successful, ['error' => message] if not */ - private static function findChannelAndDate(DOMDocument $feed): array { - $channel = $feed->getElementsByTagName('channel')->item(0); - if (!$channel instanceof DOMElement) return [ 'error' => "Channel element not found ($channel->nodeType)" ]; + private static function refreshFeed(string $url, SQLite3 $db): array { + $feedQuery = $db->prepare('SELECT id FROM feed WHERE url = :url AND user_id = :user'); + $feedQuery->bindValue(':url', $url); + $feedQuery->bindValue(':user', $_REQUEST[Key::USER_ID]); + $feedResult = $feedQuery->execute(); + $feedId = $feedResult ? $feedResult->fetchArray(SQLITE3_NUM)[0] : -1; + if ($feedId < 0) return ['error' => "No feed for URL $url found"]; - // In Atom feeds, lastBuildDate contains the last time an item in the feed was updated; if that is not present, - // use the pubDate element instead - $updated = self::eltValue($channel, 'lastBuildDate'); - if ($updated == 'lastBuildDate not found') { - $updated = self::eltValue($channel, 'pubDate'); - if ($updated == 'pubDate not found') $updated = null; - } - return ['channel' => $channel, 'updated' => Data::formatDate($updated)]; + $feed = self::retrieveFeed($url); + if (array_key_exists('error', $feed)) return $feed; + + $itemUpdate = self::updateItems($feedId, $feed['channel'], $db); + if (array_key_exists('error', $itemUpdate)) return $itemUpdate; + + $urlUpdate = $url == $feed['url'] ? '' : ', url = :url'; + $feedUpdate = $db->prepare(<<bindValue(':title', self::eltValue($feed['channel'], 'title')); + $feedUpdate->bindValue(':updated', $feed['updated']); + $feedUpdate->bindValue(':checked', Data::formatDate('now')); + $feedUpdate->bindValue(':id', $feedId); + if ($urlUpdate != '') $feedUpdate->bindValue(':url', $feed['url']); + $feedUpdate->execute(); + + return ['ok' => true]; } /** @@ -234,25 +273,21 @@ class Feed { $feed = self::retrieveFeed($url); if (array_key_exists('error', $feed)) return $feed; - $channelAndDate = self::findChannelAndDate($feed['ok']); - if (array_key_exists('error', $channelAndDate)) return $channelAndDate; - $channel = $channelAndDate['channel']; - $query = $db->prepare(<<<'SQL' INSERT INTO feed (user_id, url, title, updated_on, checked_on) VALUES (:user, :url, :title, :updated, :checked) SQL); $query->bindValue(':user', $_REQUEST[Key::USER_ID]); $query->bindValue(':url', $feed['url']); - $query->bindValue(':title', self::eltValue($channel, 'title')); - $query->bindValue(':updated', $channelAndDate['updated']); + $query->bindValue(':title', self::eltValue($feed['channel'], 'title')); + $query->bindValue(':updated', $feed['updated']); $query->bindValue(':checked', Data::formatDate('now')); $result = $query->execute(); $feedId = $result ? $db->lastInsertRowID() : -1; if ($feedId < 0) return ['error' => $db->lastErrorMsg()]; - $result = self::updateItems($feedId, $channel, $db); + $result = self::updateItems($feedId, $feed['channel'], $db); if (array_key_exists('error', $result)) return $result; return ['ok' => $feedId]; @@ -266,29 +301,33 @@ class Feed { * @return bool[]|string[] [ 'ok' => true ] if successful, [ 'error' => message ] if not */ public static function update(array $existing, string $url, SQLite3 $db): array { - $feed = self::retrieveFeed($url); - if (array_key_exists('error', $feed)) return $feed; - - $channelAndDate = self::findChannelAndDate($feed['ok']); - if (array_key_exists('error', $channelAndDate)) return $channelAndDate; - $channel = $channelAndDate['channel']; - - $query = $db->prepare(<<<'SQL' - UPDATE feed - SET url = :url, title = :title, updated_on = :updated, checked_on = :checked - WHERE id = :id AND user_id = :user - SQL); - $query->bindValue(':url', $feed['url']); - $query->bindValue(':title', self::eltValue($channel, 'title')); - $query->bindValue(':updated', $channelAndDate['updated']); - $query->bindValue(':checked', Data::formatDate('now')); - $query->bindValue(':id', $existing['id']); - $query->bindValue(':user', $_REQUEST[Key::USER_ID]); + $query = $db->prepare('UPDATE feed SET url = :url WHERE id = :id AND user_id = :user'); + $query->bindValue(':url', $url); + $query->bindValue(':id', $existing['id']); + $query->bindValue(':user', $_REQUEST[Key::USER_ID]); $query->execute(); - $result = self::updateItems($existing['id'], $channel, $db); - if (array_key_exists('error', $result)) return $result; + return self::refreshFeed($url, $db); + } - return ['ok' => true]; + /** + * @param SQLite3 $db + * @return array|true[] ['ok => true] if successful, ['error' => message] if not (may have multiple error lines) + */ + public static function refreshAll(SQLite3 $db): array { + $query = $db->prepare('SELECT url FROM feed WHERE user_id = :user'); + $query->bindValue(':user', $_REQUEST[Key::USER_ID]); + $result = $query->execute(); + $url = $result ? $result->fetchArray(SQLITE3_NUM) : false; + if ($url) { + $errors = array(); + while ($url) { + $updateResult = self::refreshFeed($url[0], $db); + if (array_key_exists('error', $updateResult)) $errors[] = $updateResult['error']; + $url = $result->fetchArray(SQLITE3_NUM); + } + return sizeof($errors) == 0 ? ['ok' => true] : ['error' => implode("\n", $errors)]; + } + return ['error' => $db->lastErrorMsg()]; } } diff --git a/src/public/index.php b/src/public/index.php index 799d67d..b2d8f01 100644 --- a/src/public/index.php +++ b/src/public/index.php @@ -9,7 +9,17 @@ include '../start.php'; Security::verifyUser(); -$db = Data::getConnection(); +$db = Data::getConnection(); + +if (array_key_exists('refresh', $_GET)) { + $refreshResult = Feed::refreshAll($db); + if (array_key_exists('ok', $refreshResult)) { + add_info('All feeds refreshed successfully'); + } else { + add_error(nl2br($refreshResult['error'])); + } +} + $result = $db->query(<<<'SQL' SELECT item.id, item.title AS item_title, coalesce(item.updated_on, item.published_on) AS as_of, feed.title AS feed_title @@ -21,7 +31,7 @@ $result = $db->query(<<<'SQL' $item = $result ? $result->fetchArray(SQLITE3_ASSOC) : false; page_head('Welcome'); ?> -

Your Unread Items

+

Your Unread Items   (Refresh All Feeds)

diff --git a/src/start.php b/src/start.php index 78624af..48701ab 100644 --- a/src/start.php +++ b/src/start.php @@ -1,6 +1,8 @@ Date: Sat, 13 Apr 2024 23:15:48 -0400 Subject: [PATCH 16/18] Add installation dox --- INSTALLING.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 8 ++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 INSTALLING.md diff --git a/INSTALLING.md b/INSTALLING.md new file mode 100644 index 0000000..d8d4dea --- /dev/null +++ b/INSTALLING.md @@ -0,0 +1,45 @@ +# Installation + +## All Environments (FrankenPHP) + +The easiest way to get up and running quickly is by using [FrankenPHP](https://frankenphp.dev), a version of [Caddy](https://caddyserver.com) that runs PHP in its process. There is a `Caddyfile` in the `/src` directory which will configure the site to run with FrankenPHP. + +For Linux / Mac users: +- Follow [their instructions](https://frankenphp.dev/docs/#standalone-binary) for downloading a binary for your system +- Rename that binary to `frankenphp` and make it executable (`chmod +x ./frankenphp`) +- Move that binary to `/usr/local/bin` + +For Windows users, the steps are the same; however, the binary should be named `frankenphp.exe` and be placed somewhere within your system's `PATH`. + +Once those steps are complete, from the `/src` directory, run `frankenphp run`. + +_(More environments will be detailed as part of a later release; an nginx reverse proxy via FastCGI is another common way to run PHP applications)_ + +## PHP Requirements + +This is written to target PHP 8.3, and requires the `curl`, `DOM`, and `SQLite3` modules. _(FrankenPHP contains these modules as part of its build.)_ + +# Setup and Configuration + +## Site Address + +The default `Caddyfile` will run the site at `http://localhost:8205`. To change that, change that address on line 5. (Note that if `http` is not specified, Caddy will attempt to obtain and install a server certificate. This may be what you want, but that also could be a source of startup errors.) + +## Feed Reader Central Behavior + +Within the `/src` directory, there is a file named `user-config.php`. This file is the place for customizations and configuration of the instance's behavior. + +### Security Model + +There ~~are~~ will be three supported security models, designed around different ways the software may be deployed. +- `Securty::SINGLE_USER` assumes that all connections to the instance are the same person. There is no password required, and no username or e-mail address will be displayed for that user. This is a good setup for a single user on a home intranet. **DO NOT PUT AN INSTANCE WITH THIS CONFIGURATION ON THE PUBLIC INTERNET!** If you do, you deserve what you get. +- `Security::SINGLE_USER_WITH_PASSWORD` _(not yet implemented)_ will be the same as the above, but will require a password. This setup is ideal for intranets where the user does not want any other users ending up marking their feeds as read just by browsing them. +- `Security::MULTI_USER` _(not yet implemented)_ will require a known e-mail address and password be provided to establish the identity of each user. This will be the most appropriate setup for an Internet-facing instance, even if there is only one user. + +### Database Name + +Data is stored under the `/src/data` directory, and the default database name is `frc.db`. If users want to change that path or file name, the path provided should be relative to `/src/data`, not just `/src`. + +### Date/Time Format + +The default format for dates and times look like "May 28, 2023 at 3:15pm". Changing the string there will alter the display on the main page and when reading an item. Any [supported PHP date or time token](https://www.php.net/manual/en/datetime.format.php) is supported. \ No newline at end of file diff --git a/README.md b/README.md index 086cab3..1b8caaa 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ -# feed-reader-central +# Feed Reader Central -A centralized, lightweight feed reader with simple self-hosting \ No newline at end of file +Feed Reader Central is a lightweight feed reader with simple self-hosting. The self-hosted instance serves as a place where feeds can be read and referenced from different devices. + +It is written in vanilla PHP, and uses a SQLite database to keep track of items. + +See [INSTALLING.md](/INSTALLING.md) for setup and configuration instructions. From c74d9ccb74ccf82e1a47a2954876923ec3bd3d01 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sun, 14 Apr 2024 20:05:39 -0400 Subject: [PATCH 17/18] Allow HTML in all item content RSS description may contain encoded entities --- src/lib/Data.php | 1 - src/lib/Feed.php | 10 +++------- src/public/index.php | 4 ++-- src/public/item.php | 9 +-------- 4 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/lib/Data.php b/src/lib/Data.php index 7ac179d..3f380b0 100644 --- a/src/lib/Data.php +++ b/src/lib/Data.php @@ -56,7 +56,6 @@ class Data { published_on TEXT NOT NULL, updated_on TEXT, content TEXT NOT NULL, - is_encoded BOOLEAN NOT NULL, is_read BOOLEAN NOT NULL DEFAULT 0, is_bookmarked BOOLEAN NOT NULL DEFAULT 0, FOREIGN KEY (feed_id) REFERENCES feed (id)) diff --git a/src/lib/Feed.php b/src/lib/Feed.php index 3c5a6af..3f2cbbc 100644 --- a/src/lib/Feed.php +++ b/src/lib/Feed.php @@ -129,8 +129,7 @@ class Feed { 'published' => Data::formatDate(self::eltValue($item, 'pubDate')), 'updated' => Data::formatDate($updNodes->length > 0 ? $updNodes->item(0)->textContent : null), 'content' => $encNodes->length > 0 ? $encNodes->item(0)->textContent - : self::eltValue($item, 'description'), - 'isEncoded' => $encNodes->length > 0 + : self::eltValue($item, 'description') ]; } @@ -148,7 +147,6 @@ class Feed { published_on = :published, updated_on = :updated, content = :content, - is_encoded = :encoded, is_read = 0 WHERE id = :id SQL); @@ -156,7 +154,6 @@ class Feed { $query->bindValue(':published', $item['published']); $query->bindValue(':updated', $item['updated']); $query->bindValue(':content', $item['content']); - $query->bindValue(':encoded', $item['isEncoded']); $query->bindValue(':id', $itemId); $query->execute(); } @@ -171,9 +168,9 @@ class Feed { private static function addItem(int $feedId, array $item, SQLite3 $db): void { $query = $db->prepare(<<<'SQL' INSERT INTO item ( - feed_id, item_guid, item_link, title, published_on, updated_on, content, is_encoded + feed_id, item_guid, item_link, title, published_on, updated_on, content ) VALUES ( - :feed, :guid, :link, :title, :published, :updated, :content, :encoded + :feed, :guid, :link, :title, :published, :updated, :content ) SQL); $query->bindValue(':feed', $feedId); @@ -183,7 +180,6 @@ class Feed { $query->bindValue(':published', $item['published']); $query->bindValue(':updated', $item['updated']); $query->bindValue(':content', $item['content']); - $query->bindValue(':encoded', $item['isEncoded']); $query->execute(); } diff --git a/src/public/index.php b/src/public/index.php index b2d8f01..6bcb14f 100644 --- a/src/public/index.php +++ b/src/public/index.php @@ -35,8 +35,8 @@ page_head('Welcome'); ?>
-

>
-
>
+
fetchArray(SQLITE3_ASSOC); } } else { ?> diff --git a/src/public/item.php b/src/public/item.php index 59a1630..1e98bd5 100644 --- a/src/public/item.php +++ b/src/public/item.php @@ -51,7 +51,6 @@ if ($item) { $published = date_time($item['published_on']); $updated = isset($item['updated_on']) ? date_time($item['updated_on']) : null; -$isEncoded = (bool) $item['is_encoded']; page_head(htmlentities("{$item['item_title']} | {$item['feed_title']}")); ?>

@@ -62,13 +61,7 @@ page_head(htmlentities("{$item['item_title']} | {$item['feed_title']}")); ?> Published
-
-
+
> Done From f04e54b3a55c809dd4848512e9ac79d436e0a549 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Mon, 15 Apr 2024 18:21:11 -0400 Subject: [PATCH 18/18] Update host note, fix doc link --- INSTALLING.md | 4 ++-- README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/INSTALLING.md b/INSTALLING.md index d8d4dea..9497134 100644 --- a/INSTALLING.md +++ b/INSTALLING.md @@ -13,7 +13,7 @@ For Windows users, the steps are the same; however, the binary should be named ` Once those steps are complete, from the `/src` directory, run `frankenphp run`. -_(More environments will be detailed as part of a later release; an nginx reverse proxy via FastCGI is another common way to run PHP applications)_ +_(More environments will be detailed as part of a later release; an nginx reverse proxy via FastCGI is another common way to run PHP applications.)_ ## PHP Requirements @@ -23,7 +23,7 @@ This is written to target PHP 8.3, and requires the `curl`, `DOM`, and `SQLite3` ## Site Address -The default `Caddyfile` will run the site at `http://localhost:8205`. To change that, change that address on line 5. (Note that if `http` is not specified, Caddy will attempt to obtain and install a server certificate. This may be what you want, but that also could be a source of startup errors.) +The default `Caddyfile` will run the site at `http://localhost:8205`. To have the process respond to other devices on your network, you can add the server name to that to line 5 (ex. `http://localhost:8205, http://server:8205`); you can also change the port on which it listens. (Note that if `http` is not specified, Caddy will attempt to obtain and install a server certificate. This may be what you want, but that also could be a source of startup errors.) ## Feed Reader Central Behavior diff --git a/README.md b/README.md index 1b8caaa..50ffdb7 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,4 @@ Feed Reader Central is a lightweight feed reader with simple self-hosting. The s It is written in vanilla PHP, and uses a SQLite database to keep track of items. -See [INSTALLING.md](/INSTALLING.md) for setup and configuration instructions. +See [INSTALLING.md](./INSTALLING.md) for setup and configuration instructions.