Alpha 7 #22
@ -239,19 +239,19 @@ class Feed {
|
||||
$start = strtolower(strlen($doc['content']) >= 9 ? substr($doc['content'], 0, 9) : $doc['content']);
|
||||
if ($start == '<!doctype' || str_starts_with($start, '<html')) {
|
||||
$derivedURL = self::deriveFeedFromHTML($doc['content']);
|
||||
if (array_key_exists('error', $derivedURL)) return ['error' => $derivedURL['error']];
|
||||
if (key_exists('error', $derivedURL)) return ['error' => $derivedURL['error']];
|
||||
$feedURL = $derivedURL['ok'];
|
||||
if (!str_starts_with($feedURL, 'http')) {
|
||||
// Relative URL; feed should be retrieved in the context of the original URL
|
||||
$original = parse_url($url);
|
||||
$port = array_key_exists('port', $original) ? ":{$original['port']}" : '';
|
||||
$port = key_exists('port', $original) ? ":{$original['port']}" : '';
|
||||
$feedURL = $original['scheme'] . '://' . $original['host'] . $port . $feedURL;
|
||||
}
|
||||
$doc = self::retrieveDocument($feedURL);
|
||||
}
|
||||
|
||||
$parsed = self::parseFeed($doc['content']);
|
||||
if (array_key_exists('error', $parsed)) return ['error' => $parsed['error']];
|
||||
if (key_exists('error', $parsed)) return ['error' => $parsed['error']];
|
||||
|
||||
$extract = $parsed['ok']->getElementsByTagNameNS(self::ATOM_NS, 'feed')->length > 0
|
||||
? self::fromAtom(...) : self::fromRSS(...);
|
||||
@ -388,7 +388,7 @@ class Feed {
|
||||
*/
|
||||
public static function refreshFeed(int $feedId, string $url, SQLite3 $db): array {
|
||||
$feedRetrieval = self::retrieveFeed($url);
|
||||
if (array_key_exists('error', $feedRetrieval)) return $feedRetrieval;
|
||||
if (key_exists('error', $feedRetrieval)) return $feedRetrieval;
|
||||
$feed = $feedRetrieval['ok'];
|
||||
|
||||
$lastCheckedQuery = $db->prepare('SELECT checked_on FROM feed where id = :id');
|
||||
@ -399,7 +399,7 @@ class Feed {
|
||||
}
|
||||
|
||||
$itemUpdate = self::updateItems($feedId, $feed, $lastChecked, $db);
|
||||
if (array_key_exists('error', $itemUpdate)) return $itemUpdate;
|
||||
if (key_exists('error', $itemUpdate)) return $itemUpdate;
|
||||
|
||||
$urlUpdate = $url == $feed->url ? '' : ', url = :url';
|
||||
$feedUpdate = $db->prepare(<<<SQL
|
||||
@ -428,7 +428,7 @@ class Feed {
|
||||
*/
|
||||
public static function add(string $url, SQLite3 $db): array {
|
||||
$feedExtract = self::retrieveFeed($url);
|
||||
if (array_key_exists('error', $feedExtract)) return $feedExtract;
|
||||
if (key_exists('error', $feedExtract)) return $feedExtract;
|
||||
|
||||
$feed = $feedExtract['ok'];
|
||||
|
||||
@ -454,7 +454,7 @@ class Feed {
|
||||
|
||||
$feedId = $db->lastInsertRowID();
|
||||
$result = self::updateItems($feedId, $feed, date_create_immutable(WWW_EPOCH), $db);
|
||||
if (array_key_exists('error', $result)) return $result;
|
||||
if (key_exists('error', $result)) return $result;
|
||||
|
||||
return ['ok' => $feedId];
|
||||
}
|
||||
@ -504,12 +504,12 @@ class Feed {
|
||||
*/
|
||||
public static function refreshAll(SQLite3 $db): array {
|
||||
$feeds = self::retrieveAll($db, $_SESSION[Key::USER_ID]);
|
||||
if (array_key_exists('error', $feeds)) return $feeds;
|
||||
if (key_exists('error', $feeds)) return $feeds;
|
||||
|
||||
$errors = [];
|
||||
array_walk($feeds, function ($feed) use ($db, &$errors) {
|
||||
$result = self::refreshFeed($feed['id'], $feed['url'], $db);
|
||||
if (array_key_exists('error', $result)) $errors[] = $result['error'];
|
||||
if (key_exists('error', $result)) $errors[] = $result['error'];
|
||||
});
|
||||
|
||||
return sizeof($errors) == 0 ? ['ok' => true] : ['error' => implode("\n", $errors)];
|
||||
|
172
src/lib/ItemList.php
Normal file
172
src/lib/ItemList.php
Normal file
@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* A list of items to be displayed
|
||||
*
|
||||
* This is a wrapper for retrieval and display of arbitrary lists of items based on a SQLite result.
|
||||
*/
|
||||
class ItemList {
|
||||
|
||||
/** @var SQLite3Result The list of items to be displayed */
|
||||
private SQLite3Result $items;
|
||||
|
||||
/** @var string The error message generated by executing a query */
|
||||
public string $error = '';
|
||||
|
||||
/**
|
||||
* Is there an error condition associated with this list?
|
||||
*
|
||||
* @return bool True if there is an error condition associated with this list, false if not
|
||||
*/
|
||||
public function isError(): bool {
|
||||
return $this->error != '';
|
||||
}
|
||||
|
||||
/** @var bool Whether to render a link to the feed to which the item belongs */
|
||||
public bool $linkFeed = false;
|
||||
|
||||
/** @var bool Whether to display the feed to which the item belongs */
|
||||
public bool $displayFeed = false;
|
||||
|
||||
/** @var bool Whether to show read / bookmarked indicators on posts */
|
||||
public bool $showIndicators = false;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param SQLite3 $db The database connection (used to retrieve error information if the query fails)
|
||||
* @param SQLite3Stmt $query The query to retrieve the items for this list
|
||||
* @param string $itemType The type of item being displayed (unread, bookmark, etc.)
|
||||
* @param string $returnURL The URL to which the item page should return once the item has been viewed
|
||||
*/
|
||||
private function __construct(SQLite3 $db, SQLite3Stmt $query, public string $itemType, public string $returnURL = '') {
|
||||
$result = $query->execute();
|
||||
if (!$result) {
|
||||
$this->error = Data::error($db)['error'];
|
||||
} else {
|
||||
$this->items = $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an item list query
|
||||
*
|
||||
* @param SQLite3 $db The database connection to use to obtain items
|
||||
* @param array $criteria One or more SQL WHERE conditions (will be combined with AND)
|
||||
* @param array $parameters Parameters to be added to the query (key index 0, value index 1; optional)
|
||||
* @return SQLite3Stmt The query, ready to be executed
|
||||
*/
|
||||
private static function makeQuery(SQLite3 $db, array $criteria, array $parameters = []): SQLite3Stmt {
|
||||
$where = empty($criteria) ? '' : 'AND ' . implode(' AND ', $criteria);
|
||||
$sql = <<<SQL
|
||||
SELECT item.id, item.feed_id, item.title AS item_title, coalesce(item.updated_on, item.published_on) AS as_of,
|
||||
item.is_read, item.is_bookmarked, feed.title AS feed_title
|
||||
FROM item INNER JOIN feed ON feed.id = item.feed_id
|
||||
WHERE feed.user_id = :userId $where
|
||||
ORDER BY coalesce(item.updated_on, item.published_on) DESC
|
||||
SQL;
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(':userId', $_SESSION[Key::USER_ID]);
|
||||
foreach ($parameters as $param) $query->bindValue($param[0], $param[1]);
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an item list with all the current user's bookmarked items
|
||||
*
|
||||
* @param SQLite3 $db The database connection to use to obtain items
|
||||
* @return static An item list with all bookmarked items
|
||||
*/
|
||||
public static function allBookmarked(SQLite3 $db): static {
|
||||
$list = new static($db, self::makeQuery($db, ['item.is_bookmarked = 1']), 'Bookmarked', '/?bookmarked');
|
||||
$list->linkFeed = true;
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an item list with all the current user's unread items
|
||||
*
|
||||
* @param SQLite3 $db The database connection to use to obtain items
|
||||
* @return static An item list with all unread items
|
||||
*/
|
||||
public static function allUnread(SQLite3 $db): static {
|
||||
$list = new static($db, self::makeQuery($db, ['item.is_read = 0']), 'Unread');
|
||||
$list->linkFeed = true;
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an item list with all items for the given feed
|
||||
*
|
||||
* @param int $feedId The ID of the feed for which items should be retrieved
|
||||
* @param SQLite3 $db The database connection to use to obtain items
|
||||
* @return static An item list with all items for the given feed
|
||||
*/
|
||||
public static function allForFeed(int $feedId, SQLite3 $db): static {
|
||||
$list = new static($db, self::makeQuery($db, ['feed.id = :feed'], [[':feed', $feedId]]), '',
|
||||
"/feed/items?id=$feedId");
|
||||
$list->showIndicators = true;
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an item list with unread items for the given feed
|
||||
*
|
||||
* @param int $feedId The ID of the feed for which items should be retrieved
|
||||
* @param SQLite3 $db The database connection to use to obtain items
|
||||
* @return static An item list with unread items for the given feed
|
||||
*/
|
||||
public static function unreadForFeed(int $feedId, SQLite3 $db): static {
|
||||
return new static($db, self::makeQuery($db, ['feed.id = :feed', 'item.is_read = 0'], [[':feed', $feedId]]),
|
||||
'Unread', "/feed/items?id=$feedId&unread");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an item list with bookmarked items for the given feed
|
||||
*
|
||||
* @param int $feedId The ID of the feed for which items should be retrieved
|
||||
* @param SQLite3 $db The database connection to use to obtain items
|
||||
* @return static An item list with bookmarked items for the given feed
|
||||
*/
|
||||
public static function bookmarkedForFeed(int $feedId, SQLite3 $db): static {
|
||||
return new static($db,
|
||||
self::makeQuery($db, ['feed.id = :feed', 'item.is_bookmarked = 1'], [[':feed', $feedId]]), 'Bookmarked',
|
||||
"/feed/items?id=$feedId&bookmarked");
|
||||
}
|
||||
|
||||
/**
|
||||
* Render this item list
|
||||
*/
|
||||
public function render(): void {
|
||||
if ($this->isError()) { ?>
|
||||
<p>Error retrieving list:<br><?=$this->error?><?php
|
||||
return;
|
||||
}
|
||||
$item = $this->items->fetchArray(SQLITE3_ASSOC);
|
||||
$return = $this->returnURL == '' ? '' : '&from=' . urlencode($this->returnURL);
|
||||
echo '<article>';
|
||||
if ($item) {
|
||||
while ($item) { ?>
|
||||
<p><?=hx_get("/item?id={$item['id']}$return", strip_tags($item['item_title']))?><br>
|
||||
<small><?php
|
||||
if ($this->showIndicators) {
|
||||
if (!$item['is_read']) echo '<strong>Unread</strong> ';
|
||||
if ($item['is_bookmarked']) echo '<strong>Bookmarked</strong> ';
|
||||
}
|
||||
echo '<em>' . date_time($item['as_of']) . '</em>';
|
||||
if ($this->linkFeed) {
|
||||
echo ' • ' .
|
||||
hx_get("/feed/items?id={$item['feed_id']}&" . strtolower($this->itemType),
|
||||
htmlentities($item['feed_title']));
|
||||
} elseif ($this->displayFeed) {
|
||||
echo ' • ' . htmlentities($item['feed_title']);
|
||||
} ?>
|
||||
</small><?php
|
||||
$item = $this->items->fetchArray(SQLITE3_ASSOC);
|
||||
}
|
||||
} else { ?>
|
||||
<p><em>There are no <?=strtolower($this->itemType)?> items</em><?php
|
||||
}
|
||||
echo '</article>';
|
||||
}
|
||||
}
|
@ -131,7 +131,7 @@ class Security {
|
||||
* @param bool $redirectIfAnonymous Whether to redirect the request if there is no user logged on
|
||||
*/
|
||||
public static function verifyUser(SQLite3 $db, bool $redirectIfAnonymous = true): void {
|
||||
if (array_key_exists(Key::USER_ID, $_SESSION)) return;
|
||||
if (key_exists(Key::USER_ID, $_SESSION)) return;
|
||||
|
||||
if (SECURITY_MODEL == self::SINGLE_USER) self::logOnSingleUser($db);
|
||||
|
||||
|
@ -35,7 +35,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
||||
$toEdit = Feed::retrieveById($_POST['id'], $db);
|
||||
$result = $toEdit ? Feed::update($toEdit, $_POST['url'], $db) : ['error' => "Feed {$_POST['id']} not found"];
|
||||
}
|
||||
if (array_key_exists('ok', $result)) {
|
||||
if (key_exists('ok', $result)) {
|
||||
add_info('Feed saved successfully');
|
||||
frc_redirect('/feeds');
|
||||
}
|
||||
|
@ -12,69 +12,19 @@ Security::verifyUser($db);
|
||||
|
||||
if (!($feed = Feed::retrieveById($_GET['id'], $db))) not_found();
|
||||
|
||||
/** Display a list of unread items for this feed */
|
||||
const TYPE_UNREAD = 0;
|
||||
|
||||
/** Display a list of bookmarked items for this feed */
|
||||
const TYPE_BOOKMARKED = 1;
|
||||
|
||||
/** Display all items for this feed */
|
||||
const TYPE_ALL = 2;
|
||||
|
||||
$type = match (true) {
|
||||
array_key_exists('unread', $_GET) => TYPE_UNREAD,
|
||||
array_key_exists('bookmarked', $_GET) => TYPE_BOOKMARKED,
|
||||
default => TYPE_ALL
|
||||
$list = match (true) {
|
||||
key_exists('unread', $_GET) => ItemList::unreadForFeed($feed['id'], $db),
|
||||
key_exists('bookmarked', $_GET) => ItemList::bookmarkedForFeed($feed['id'], $db),
|
||||
default => ItemList::allForFeed($feed['id'], $db)
|
||||
};
|
||||
|
||||
$extraSQL = match ($type) {
|
||||
TYPE_UNREAD => ' AND is_read = 0',
|
||||
TYPE_BOOKMARKED => ' AND is_bookmarked = 1',
|
||||
default => ''
|
||||
};
|
||||
$itemQuery = $db->prepare(<<<SQL
|
||||
SELECT id, title, coalesce(updated_on, published_on) AS as_of, is_read, is_bookmarked
|
||||
FROM item
|
||||
WHERE feed_id = :feed$extraSQL
|
||||
ORDER BY date(coalesce(updated_on, published_on)) DESC
|
||||
SQL);
|
||||
$itemQuery->bindValue(':feed', $feed['id']);
|
||||
if (!($itemResult = $itemQuery->execute())) add_error(Data::error($db)['error']);
|
||||
$item = $itemResult ? $itemResult->fetchArray(SQLITE3_ASSOC) : false;
|
||||
|
||||
$queryParam = match ($type) {
|
||||
TYPE_UNREAD => '&unread',
|
||||
TYPE_BOOKMARKED => '&bookmarked',
|
||||
default => ''
|
||||
};
|
||||
$thisURL = urlencode("/feed/items?id={$feed['id']}$queryParam");
|
||||
|
||||
$listType = match ($type) {
|
||||
TYPE_UNREAD => 'Unread',
|
||||
TYPE_BOOKMARKED => 'Bookmarked',
|
||||
default => ''
|
||||
};
|
||||
|
||||
page_head(($type != TYPE_ALL ? "$listType Items | " : '') . strip_tags($feed['title']));
|
||||
if ($type == TYPE_ALL) { ?>
|
||||
<h1><?=htmlentities($feed['title'])?></h1><?php
|
||||
} else { ?>
|
||||
<h1 class=item_heading><?=htmlentities($feed['title'])?></h1>
|
||||
<div class=item_published><?=$listType?> Items</div><?php
|
||||
} ?>
|
||||
<article><?php
|
||||
if ($item) {
|
||||
while ($item) { ?>
|
||||
<p><?=hx_get("/item?id={$item['id']}&from=$thisURL", strip_tags($item['title']))?><br>
|
||||
<small><?=$item['is_read'] == 0 ? '<strong>New</strong> ' : ''?>
|
||||
<?=$item['is_bookmarked'] ? '<strong>Bookmarked</strong> ' : ''?>
|
||||
<em><?=date_time($item['as_of'])?></em></small><?php
|
||||
$item = $itemResult->fetchArray(SQLITE3_ASSOC);
|
||||
}
|
||||
} else { ?>
|
||||
<p><em>There are no <?=strtolower($listType)?> items</em><?php
|
||||
} ?>
|
||||
</article>
|
||||
<?php
|
||||
page_head(($list->itemType != '' ? "$list->itemType Items | " : '') . strip_tags($feed['title']));
|
||||
if ($list->itemType == '') {
|
||||
echo '<h1>' . htmlentities($feed['title']) . '</h1>';
|
||||
} else {
|
||||
echo '<h1 class=item_heading>' . htmlentities($feed['title']) . '</h1>';
|
||||
echo "<div class=item_published>$list->itemType Items</div>";
|
||||
}
|
||||
$list->render();
|
||||
page_foot();
|
||||
$db->close();
|
||||
|
@ -10,60 +10,28 @@ include '../start.php';
|
||||
$db = Data::getConnection();
|
||||
Security::verifyUser($db);
|
||||
|
||||
if (array_key_exists('refresh', $_GET)) {
|
||||
if (key_exists('refresh', $_GET)) {
|
||||
$refreshResult = Feed::refreshAll($db);
|
||||
if (array_key_exists('ok', $refreshResult)) {
|
||||
if (key_exists('ok', $refreshResult)) {
|
||||
add_info('All feeds refreshed successfully');
|
||||
} else {
|
||||
add_error(nl2br($refreshResult['error']));
|
||||
}
|
||||
}
|
||||
|
||||
if (key_exists('bookmarked', $_GET)) {
|
||||
$itemCriteria = 'item.is_bookmarked = 1';
|
||||
$returnURL = '&from=' . urlencode('/?bookmarked');
|
||||
$type = 'Bookmarked';
|
||||
} else {
|
||||
$itemCriteria = 'item.is_read = 0';
|
||||
$returnURL = '';
|
||||
$type = 'Unread';
|
||||
$list = match (true) {
|
||||
key_exists('bookmarked', $_GET) => ItemList::allBookmarked($db),
|
||||
default => ItemList::allUnread($db)
|
||||
};
|
||||
$title = "Your $list->itemType Items";
|
||||
|
||||
page_head($title);
|
||||
echo "<h1>$title";
|
||||
if ($list->itemType == 'Unread') {
|
||||
echo ' ' . hx_get('/?refresh', '(Refresh All Feeds)', 'class=refresh hx-indicator="closest h1"')
|
||||
. '<span class=loading>Refreshing…</span>';
|
||||
}
|
||||
$title = "Your $type Items";
|
||||
|
||||
$query = $db->prepare(<<<SQL
|
||||
SELECT item.id, item.feed_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 feed.user_id = :userId
|
||||
AND $itemCriteria
|
||||
ORDER BY coalesce(item.updated_on, item.published_on) DESC
|
||||
SQL);
|
||||
$query->bindValue(':userId', $_SESSION[Key::USER_ID]);
|
||||
$result = $query->execute();
|
||||
$item = $result ? $result->fetchArray(SQLITE3_ASSOC) : false;
|
||||
|
||||
page_head($title); ?>
|
||||
<h1>
|
||||
<?=$title?><?php
|
||||
if ($type == 'Unread'): ?>
|
||||
<?=hx_get('/?refresh', '(Refresh All Feeds)', 'class=refresh hx-indicator="closest h1"')?>
|
||||
<span class=loading>Refreshing…</span><?php
|
||||
endif; ?>
|
||||
</h1>
|
||||
<article><?php
|
||||
if ($item) {
|
||||
while ($item) { ?>
|
||||
<p><?=hx_get("/item?id={$item['id']}$returnURL", strip_tags($item['item_title']))?><br>
|
||||
<small><?=date_time($item['as_of'])?> •
|
||||
<?=hx_get("/feed/items?id={$item['feed_id']}&" . strtolower($type), htmlentities($item['feed_title']))?>
|
||||
</small><?php
|
||||
$item = $result->fetchArray(SQLITE3_ASSOC);
|
||||
}
|
||||
} else { ?>
|
||||
<p>There are no <?=strtolower($type)?> items<?php
|
||||
} ?>
|
||||
</article><?php
|
||||
|
||||
echo '</h1>';
|
||||
$list->render();
|
||||
page_foot();
|
||||
$db->close();
|
||||
|
@ -5,6 +5,6 @@
|
||||
|
||||
include '../../start.php';
|
||||
|
||||
if (array_key_exists(Key::USER_ID, $_SESSION)) session_destroy();
|
||||
if (key_exists(Key::USER_ID, $_SESSION)) session_destroy();
|
||||
|
||||
frc_redirect('/');
|
||||
|
@ -5,7 +5,7 @@ $db = Data::getConnection();
|
||||
Security::verifyUser($db, redirectIfAnonymous: false);
|
||||
|
||||
// Users already logged on have no need of this page
|
||||
if (array_key_exists(Key::USER_ID, $_SESSION)) frc_redirect('/');
|
||||
if (key_exists(Key::USER_ID, $_SESSION)) frc_redirect('/');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
||||
Security::logOnUser($_POST['email'] ?? '', $_POST['password'], $_POST['returnTo'] ?? null, $db);
|
||||
|
@ -16,7 +16,7 @@ session_start([
|
||||
* @param string $message The message itself
|
||||
*/
|
||||
function add_message(string $level, string $message): void {
|
||||
if (!array_key_exists(Key::USER_MSG, $_SESSION)) $_SESSION[Key::USER_MSG] = array();
|
||||
if (!key_exists(Key::USER_MSG, $_SESSION)) $_SESSION[Key::USER_MSG] = array();
|
||||
$_SESSION[Key::USER_MSG][] = ['level' => $level, 'message' => $message];
|
||||
}
|
||||
|
||||
@ -39,8 +39,7 @@ function add_info(string $message): void {
|
||||
}
|
||||
|
||||
/** @var bool $is_htmx True if this request was initiated by htmx, false if not */
|
||||
$is_htmx = array_key_exists('HTTP_HX_REQUEST', $_SERVER)
|
||||
&& !array_key_exists('HTTP_HX_HISTORY_RESTORE_REQUEST', $_SERVER);
|
||||
$is_htmx = key_exists('HTTP_HX_REQUEST', $_SERVER) && !key_exists('HTTP_HX_HISTORY_RESTORE_REQUEST', $_SERVER);
|
||||
|
||||
/**
|
||||
* Render the title bar for the page
|
||||
@ -54,7 +53,7 @@ function title_bar(): void {
|
||||
<header hx-target=#main hx-push-url=true>
|
||||
<div><a href=/ class=title>Feed Reader Central</a><span class=version>v<?=$version?></span></div>
|
||||
<div><?php
|
||||
if (array_key_exists(Key::USER_ID, $_SESSION)) {
|
||||
if (key_exists(Key::USER_ID, $_SESSION)) {
|
||||
$db = Data::getConnection();
|
||||
try {
|
||||
$bookQuery = $db->prepare(<<<'SQL'
|
||||
|
Loading…
Reference in New Issue
Block a user