@ -30,30 +30,8 @@ Minz_Session::init('FreshRSS');
// ================================================================================================
// ================================================================================================
class FeverAPI_EntryDAO extends FreshRSS_EntryDAO
class FeverDAO extends Minz_ModelPdo
{
{
/**
* @return array
*/
public function countFever()
{
$values = array(
'total' => 0,
'min' => 0,
'max' => 0,
);
$sql = 'SELECT COUNT(id) as `total`, MIN(id) as `min`, MAX(id) as `max` FROM `' . $this->prefix . 'entry`';
$stm = $this->bd->prepare($sql);
$stm->execute();
$result = $stm->fetchAll(PDO::FETCH_ASSOC);
if (!empty($result[0])) {
$values = $result[0];
}
return $values;
}
/**
/**
* @param string $prefix
* @param string $prefix
* @param array $values
* @param array $values
@ -81,14 +59,15 @@ class FeverAPI_EntryDAO extends FreshRSS_EntryDAO
{
{
$values = array();
$values = array();
$order = '';
$order = '';
$entryDAO = FreshRSS_Factory::createEntryDao();
$sql = 'SELECT id, guid, title, author, '
$sql = 'SELECT id, guid, title, author, '
. ($this ->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
. ($entryDAO ->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
. ', link, date, is_read, is_favorite, id_feed, tags '
. ', link, date, is_read, is_favorite, id_feed, tags '
. 'FROM `' . $this->prefix . 'entry` WHERE';
. 'FROM `' . $this->prefix . 'entry` WHERE';
if (!empty($entry_ids)) {
if (!empty($entry_ids)) {
$bindEntryIds = $this->bindParamArray("id" , $entry_ids, $values);
$bindEntryIds = $this->bindParamArray('id' , $entry_ids, $values);
$sql .= " id IN($bindEntryIds)";
$sql .= " id IN($bindEntryIds)";
} else if (!empty($max_id)) {
} else if (!empty($max_id)) {
$sql .= ' id < :id ' ;
$sql .= ' id < :id ' ;
@ -101,7 +80,7 @@ class FeverAPI_EntryDAO extends FreshRSS_EntryDAO
}
}
if (!empty($feed_ids)) {
if (!empty($feed_ids)) {
$bindFeedIds = $this->bindParamArray("feed" , $feed_ids, $values);
$bindFeedIds = $this->bindParamArray('feed' , $feed_ids, $values);
$sql .= " AND id_feed IN($bindFeedIds)";
$sql .= " AND id_feed IN($bindFeedIds)";
}
}
@ -114,7 +93,7 @@ class FeverAPI_EntryDAO extends FreshRSS_EntryDAO
$entries = array();
$entries = array();
foreach ($result as $dao) {
foreach ($result as $dao) {
$entries[] = self ::daoToEntry($dao);
$entries[] = FreshRSS_EntryDAO ::daoToEntry($dao);
}
}
return $entries;
return $entries;
@ -130,6 +109,9 @@ class FeverAPI
const STATUS_OK = 1;
const STATUS_OK = 1;
const STATUS_ERR = 0;
const STATUS_ERR = 0;
private $entryDAO = null;
private $feedDAO = null;
/**
/**
* Authenticate the user
* Authenticate the user
*
*
@ -150,6 +132,8 @@ class FeverAPI
$user_conf = get_user_configuration($username);
$user_conf = get_user_configuration($username);
if ($user_conf != null & & $feverKey === $user_conf->feverKey) {
if ($user_conf != null & & $feverKey === $user_conf->feverKey) {
FreshRSS_Context::$user_conf = $user_conf;
FreshRSS_Context::$user_conf = $user_conf;
$this->entryDAO = FreshRSS_Factory::createEntryDao();
$this->feedDAO = FreshRSS_Factory::createFeedDao();
return true;
return true;
}
}
Minz_Log::error('Fever API: Reset API password for user: ' . $username, API_LOG);
Minz_Log::error('Fever API: Reset API password for user: ' . $username, API_LOG);
@ -175,30 +159,6 @@ class FeverAPI
return false;
return false;
}
}
/**
* @return FreshRSS_FeedDAO
*/
protected function getDaoForFeeds()
{
return new FreshRSS_FeedDAO();
}
/**
* @return FreshRSS_CategoryDAO
*/
protected function getDaoForCategories()
{
return new FreshRSS_CategoryDAO();
}
/**
* @return FeverAPI_EntryDAO
*/
protected function getDaoForEntries()
{
return new FeverAPI_EntryDAO();
}
/**
/**
* This does all the processing, since the fever api does not have a specific variable that specifies the operation
* This does all the processing, since the fever api does not have a specific variable that specifies the operation
*
*
@ -213,65 +173,65 @@ class FeverAPI
throw new Exception('No user given or user is not allowed to access API');
throw new Exception('No user given or user is not allowed to access API');
}
}
if (isset($_REQUEST["groups" ])) {
if (isset($_REQUEST['groups' ])) {
$response_arr["groups" ] = $this->getGroups();
$response_arr['groups' ] = $this->getGroups();
$response_arr["feeds_groups" ] = $this->getFeedsGroup();
$response_arr['feeds_groups' ] = $this->getFeedsGroup();
}
}
if (isset($_REQUEST["feeds" ])) {
if (isset($_REQUEST['feeds' ])) {
$response_arr["feeds" ] = $this->getFeeds();
$response_arr['feeds' ] = $this->getFeeds();
$response_arr["feeds_groups" ] = $this->getFeedsGroup();
$response_arr['feeds_groups' ] = $this->getFeedsGroup();
}
}
if (isset($_REQUEST["favicons" ])) {
if (isset($_REQUEST['favicons' ])) {
$response_arr["favicons" ] = $this->getFavicons();
$response_arr['favicons' ] = $this->getFavicons();
}
}
if (isset($_REQUEST["items" ])) {
if (isset($_REQUEST['items' ])) {
$response_arr["total_items" ] = $this->getTotalItems();
$response_arr['total_items' ] = $this->getTotalItems();
$response_arr["items" ] = $this->getItems();
$response_arr['items' ] = $this->getItems();
}
}
if (isset($_REQUEST["links" ])) {
if (isset($_REQUEST['links' ])) {
$response_arr["links" ] = $this->getLinks();
$response_arr['links' ] = $this->getLinks();
}
}
if (isset($_REQUEST["unread_item_ids" ])) {
if (isset($_REQUEST['unread_item_ids' ])) {
$response_arr["unread_item_ids" ] = $this->getUnreadItemIds();
$response_arr['unread_item_ids' ] = $this->getUnreadItemIds();
}
}
if (isset($_REQUEST["saved_item_ids" ])) {
if (isset($_REQUEST['saved_item_ids' ])) {
$response_arr["saved_item_ids" ] = $this->getSavedItemIds();
$response_arr['saved_item_ids' ] = $this->getSavedItemIds();
}
}
if (isset($_REQUEST["mark"], $_REQUEST["as"], $_REQUEST["id"]) & & is_numeric($_REQUEST["id" ])) {
if (isset($_REQUEST['mark'], $_REQUEST['as'], $_REQUEST['id']) & & is_numeric($_REQUEST['id' ])) {
$method_name = "set" . ucfirst($_REQUEST["mark"]) . "As" . ucfirst($_REQUEST["as" ]);
$method_name = 'set' . ucfirst($_REQUEST['mark']) . 'As' . ucfirst($_REQUEST['as' ]);
$allowedMethods = array(
$allowedMethods = array(
'setFeedAsRead', 'setGroupAsRead', 'setItemAsRead',
'setFeedAsRead', 'setGroupAsRead', 'setItemAsRead',
'setItemAsSaved', 'setItemAsUnread', 'setItemAsUnsaved'
'setItemAsSaved', 'setItemAsUnread', 'setItemAsUnsaved'
);
);
if (in_array($method_name, $allowedMethods)) {
if (in_array($method_name, $allowedMethods)) {
$id = intval($_REQUEST["id" ]);
$id = intval($_REQUEST['id' ]);
switch (strtolower($_REQUEST["mark" ])) {
switch (strtolower($_REQUEST['mark' ])) {
case 'item':
case 'item':
$this->{$method_name}($id);
$this->{$method_name}($id);
break;
break;
case 'feed':
case 'feed':
case 'group':
case 'group':
$before = (isset($_REQUEST["before"])) ? $_REQUEST["before" ] : null;
$before = isset($_REQUEST['before']) ? $_REQUEST['before' ] : null;
$this->{$method_name}($id, $before);
$this->{$method_name}($id, $before);
break;
break;
}
}
switch ($_REQUEST["as" ]) {
switch ($_REQUEST['as' ]) {
case "read" :
case 'read' :
case "unread" :
case 'unread' :
$response_arr["unread_item_ids" ] = $this->getUnreadItemIds();
$response_arr['unread_item_ids' ] = $this->getUnreadItemIds();
break;
break;
case 'saved':
case 'saved':
case 'unsaved':
case 'unsaved':
$response_arr["saved_item_ids" ] = $this->getSavedItemIds();
$response_arr['saved_item_ids' ] = $this->getSavedItemIds();
break;
break;
}
}
}
}
@ -308,8 +268,7 @@ class FeverAPI
{
{
$lastUpdate = 0;
$lastUpdate = 0;
$dao = $this->getDaoForFeeds();
$entries = $this->feedDAO->listFeedsOrderUpdate(-1, 1);
$entries = $dao->listFeedsOrderUpdate(-1, 1);
$feed = current($entries);
$feed = current($entries);
if (!empty($feed)) {
if (!empty($feed)) {
@ -325,20 +284,18 @@ class FeverAPI
protected function getFeeds()
protected function getFeeds()
{
{
$feeds = array();
$feeds = array();
$myFeeds = $this->feedDAO->listFeeds();
$dao = $this->getDaoForFeeds();
$myFeeds = $dao->listFeeds();
/** @var FreshRSS_Feed $feed */
/** @var FreshRSS_Feed $feed */
foreach ($myFeeds as $feed) {
foreach ($myFeeds as $feed) {
$feeds[] = array(
$feeds[] = array(
"id" => $feed->id(),
'id' => $feed->id(),
"favicon_id" => $feed->id(),
'favicon_id' => $feed->id(),
"title" => $feed->name(),
'title' => $feed->name(),
"url" => $feed->url(),
'url' => $feed->url(),
"site_url" => $feed->website(),
'site_url' => $feed->website(),
"is_spark" => 0, // unsupported
'is_spark' => 0, // unsupported
"last_updated_on_time" => $feed->lastUpdate()
'last_updated_on_time' => $feed->lastUpdate(),
);
);
}
}
@ -352,14 +309,14 @@ class FeverAPI
{
{
$groups = array();
$groups = array();
$dao = $this->getDaoForCategories ();
$categoryDAO = new FreshRSS_CategoryDAO ();
$categories = $dao ->listCategories(false, false);
$categories = $categoryDAO ->listCategories(false, false);
/** @var FreshRSS_Category $category */
/** @var FreshRSS_Category $category */
foreach ($categories as $category) {
foreach ($categories as $category) {
$groups[] = array(
$groups[] = array(
'id' => $category->id(),
'id' => $category->id(),
'title' => $category->name()
'title' => $category->name(),
);
);
}
}
@ -372,11 +329,8 @@ class FeverAPI
protected function getFavicons()
protected function getFavicons()
{
{
$favicons = array();
$favicons = array();
$dao = $this->getDaoForFeeds();
$myFeeds = $dao->listFeeds();
$salt = FreshRSS_Context::$system_conf->salt;
$salt = FreshRSS_Context::$system_conf->salt;
$myFeeds = $this->feedDAO->listFeeds();
/** @var FreshRSS_Feed $feed */
/** @var FreshRSS_Feed $feed */
foreach ($myFeeds as $feed) {
foreach ($myFeeds as $feed) {
@ -388,8 +342,8 @@ class FeverAPI
}
}
$favicons[] = array(
$favicons[] = array(
"id" => $feed->id(),
'id' => $feed->id(),
"data" => image_type_to_mime_type(exif_imagetype($filename)) . ";base64," . base64_encode(file_get_contents($filename))
'data' => image_type_to_mime_type(exif_imagetype($filename)) . ';base64,' . base64_encode(file_get_contents($filename))
);
);
}
}
@ -401,16 +355,7 @@ class FeverAPI
*/
*/
protected function getTotalItems()
protected function getTotalItems()
{
{
$total_items = 0;
return $this->entryDAO->count();
$dao = $this->getDaoForEntries();
$result = $dao->countFever();
if (!empty($result)) {
$total_items = $result['total'];
}
return $total_items;
}
}
/**
/**
@ -420,9 +365,7 @@ class FeverAPI
{
{
$groups = array();
$groups = array();
$ids = array();
$ids = array();
$myFeeds = $this->feedDAO->listFeeds();
$dao = $this->getDaoForFeeds();
$myFeeds = $dao->listFeeds();
/** @var FreshRSS_Feed $feed */
/** @var FreshRSS_Feed $feed */
foreach ($myFeeds as $feed) {
foreach ($myFeeds as $feed) {
@ -462,8 +405,7 @@ class FeverAPI
*/
*/
protected function getUnreadItemIds()
protected function getUnreadItemIds()
{
{
$dao = $this->getDaoForEntries();
$entries = $this->entryDAO->listIdsWhere('a', '', FreshRSS_Entry::STATE_NOT_READ, 'ASC', 0);
$entries = $dao->listIdsWhere('a', '', FreshRSS_Entry::STATE_NOT_READ, 'ASC', 0);
return $this->entriesToIdList($entries);
return $this->entriesToIdList($entries);
}
}
@ -472,33 +414,28 @@ class FeverAPI
*/
*/
protected function getSavedItemIds()
protected function getSavedItemIds()
{
{
$dao = $this->getDaoForEntries();
$entries = $this->entryDAO->listIdsWhere('a', '', FreshRSS_Entry::STATE_FAVORITE, 'ASC', 0);
$entries = $dao->listIdsWhere('a', '', FreshRSS_Entry::STATE_FAVORITE, 'ASC', 0);
return $this->entriesToIdList($entries);
return $this->entriesToIdList($entries);
}
}
protected function setItemAsRead($id)
protected function setItemAsRead($id)
{
{
$dao = $this->getDaoForEntries();
return $this->entryDAO->markRead($id, true);
$dao->markRead($id, true);
}
}
protected function setItemAsUnread($id)
protected function setItemAsUnread($id)
{
{
$dao = $this->getDaoForEntries();
return $this->entryDAO->markRead($id, false);
$dao->markRead($id, false);
}
}
protected function setItemAsSaved($id)
protected function setItemAsSaved($id)
{
{
$dao = $this->getDaoForEntries();
return $this->entryDAO->markFavorite($id, true);
$dao->markFavorite($id, true);
}
}
protected function setItemAsUnsaved($id)
protected function setItemAsUnsaved($id)
{
{
$dao = $this->getDaoForEntries();
return $this->entryDAO->markFavorite($id, false);
$dao->markFavorite($id, false);
}
}
/**
/**
@ -511,17 +448,17 @@ class FeverAPI
$max_id = null;
$max_id = null;
$since_id = null;
$since_id = null;
if (isset($_REQUEST["feed_ids"]) || isset($_REQUEST["group_ids" ])) {
if (isset($_REQUEST['feed_ids']) || isset($_REQUEST['group_ids' ])) {
if (isset($_REQUEST["feed_ids" ])) {
if (isset($_REQUEST['feed_ids' ])) {
$feed_ids = explode(",", $_REQUEST["feed_ids" ]);
$feed_ids = explode(',', $_REQUEST['feed_ids' ]);
}
}
$dao = $this->getDaoForCategories();
if (isset($_REQUEST['group_ids'])) {
if (isset($_REQUEST["group_ids"])) {
$categoryDAO = new FreshRSS_CategoryDAO();
$group_ids = explode(",", $_REQUEST["group_ids" ]);
$group_ids = explode(',', $_REQUEST['group_ids' ]);
foreach ($group_ids as $id) {
foreach ($group_ids as $id) {
/** @var FreshRSS_Category $category */
/** @var FreshRSS_Category $category */
$category = $dao->searchById($id);
$category = $categoryDAO->searchById($id); //TODO: Transform to SQL query without loop! Consider FreshRSS_CategoryDAO::listCategories(true)
/** @var FreshRSS_Feed $feed */
/** @var FreshRSS_Feed $feed */
foreach ($category->feeds() as $feed) {
foreach ($category->feeds() as $feed) {
$feeds[] = $feed->id();
$feeds[] = $feed->id();
@ -532,25 +469,25 @@ class FeverAPI
}
}
}
}
if (isset($_REQUEST["max_id" ])) {
if (isset($_REQUEST['max_id' ])) {
// use the max_id argument to request the previous $item_limit items
// use the max_id argument to request the previous $item_limit items
if (is_numeric($_REQUEST["max_id" ])) {
if (is_numeric($_REQUEST['max_id' ])) {
$max = ($_REQUEST["max_id"] > 0) ? intval($_REQUEST["max_id" ]) : 0;
$max = $_REQUEST['max_id'] > 0 ? intval($_REQUEST['max_id' ]) : 0;
if ($max) {
if ($max) {
$max_id = $max;
$max_id = $max;
}
}
}
}
} else if (isset($_REQUEST["with_ids" ])) {
} else if (isset($_REQUEST['with_ids' ])) {
$entry_ids = explode(",", $_REQUEST["with_ids" ]);
$entry_ids = explode(',', $_REQUEST['with_ids' ]);
} else {
} else {
// use the since_id argument to request the next $item_limit items
// use the since_id argument to request the next $item_limit items
$since_id = isset($_REQUEST["since_id"]) & & is_numeric($_REQUEST["since_id"]) ? intval($_REQUEST["since_id" ]) : 0;
$since_id = isset($_REQUEST['since_id']) & & is_numeric($_REQUEST['since_id']) ? intval($_REQUEST['since_id' ]) : 0;
}
}
$items = array();
$items = array();
$dao = $this->getDaoForEntries ();
$feverDAO = new FeverDAO ();
$entries = $dao ->findEntries($feed_ids, $entry_ids, $max_id, $since_id);
$entries = $feverDAO ->findEntries($feed_ids, $entry_ids, $max_id, $since_id);
// Load list of extensions and enable the "system" ones.
// Load list of extensions and enable the "system" ones.
Minz_ExtensionManager::init();
Minz_ExtensionManager::init();
@ -562,15 +499,15 @@ class FeverAPI
continue;
continue;
}
}
$items[] = array(
$items[] = array(
"id" => $entry->id(),
'id' => $entry->id(),
"feed_id" => $entry->feed(false),
'feed_id' => $entry->feed(false),
"title" => $entry->title(),
'title' => $entry->title(),
"author" => $entry->author(),
'author' => $entry->author(),
"html" => $entry->content(),
'html' => $entry->content(),
"url" => $entry->link(),
'url' => $entry->link(),
"is_saved" => $entry->isFavorite() ? 1 : 0,
'is_saved' => $entry->isFavorite() ? 1 : 0,
"is_read" => $entry->isRead() ? 1 : 0,
'is_read' => $entry->isRead() ? 1 : 0,
"created_on_time" => $entry->date(true)
'created_on_time' => $entry->date(true),
);
);
}
}
@ -585,43 +522,31 @@ class FeverAPI
*/
*/
protected function convertBeforeToId($beforeTimestamp)
protected function convertBeforeToId($beforeTimestamp)
{
{
// if before is zero, set it to now so feeds all items are read from before this point in time
return $beforeTimestamp == 0 ? 0 : $beforeTimestamp . '000000';
if ($beforeTimestamp == 0) {
$before = time();
}
$before = PHP_INT_MAX;
return $before;
}
}
protected function setFeedAsRead($id, $before)
protected function setFeedAsRead($id, $before)
{
{
$before = $this->convertBeforeToId($before);
$before = $this->convertBeforeToId($before);
$dao = $this->getDaoForEntries();
return $this->entryDAO->markReadFeed($id, $before);
return $dao->markReadFeed($id, $before);
}
}
protected function setGroupAsRead($id, $before)
protected function setGroupAsRead($id, $before)
{
{
$dao = $this->getDaoForEntries( );
$before = $this->convertBeforeToId($before );
// special case to mark all items as read
// special case to mark all items as read
if ($id === 0) {
if ($id == 0) {
$result = $dao->countFever();
return $this->entryDAO->markReadEntries($before);
if (!empty($result)) {
return $dao->markReadEntries($result['max']);
}
}
}
$before = $this->convertBeforeToId($before);
return $this->entryDAO->markReadCat($id, $before);
return $dao->markReadCat($id, $before);
}
}
}
}
// ================================================================================================
// ================================================================================================
// refresh is not allowed yet, probably we find a way to support it later
// refresh is not allowed yet, probably we find a way to support it later
if (isset($_REQUEST["refresh" ])) {
if (isset($_REQUEST['refresh' ])) {
Minz_Log::warning('Fever API: Refresh items - notImplemented()', API_LOG);
Minz_Log::warning('Fever API: Refresh items - notImplemented()', API_LOG);
header('HTTP/1.1 501 Not Implemented');
header('HTTP/1.1 501 Not Implemented');
header('Content-Type: text/plain; charset=UTF-8');
header('Content-Type: text/plain; charset=UTF-8');
@ -631,7 +556,7 @@ if (isset($_REQUEST["refresh"])) {
// Start the Fever API handling
// Start the Fever API handling
$handler = new FeverAPI();
$handler = new FeverAPI();
header("Content-Type: application/json; charset=UTF-8" );
header('Content-Type: application/json; charset=UTF-8' );
if (!$handler->isAuthenticatedApiUser()) {
if (!$handler->isAuthenticatedApiUser()) {
echo $handler->wrap(FeverAPI::STATUS_ERR, array());
echo $handler->wrap(FeverAPI::STATUS_ERR, array());