use ORM when subscribing feeds

master
Andrew Dolgov 4 years ago
parent dae0476159
commit 7cf12233d7

@ -986,17 +986,18 @@ class Feeds extends Handler_Protected {
* to get all possible feeds. * to get all possible feeds.
* 5 - Couldn't download the URL content. * 5 - Couldn't download the URL content.
* 6 - Content is an invalid XML. * 6 - Content is an invalid XML.
* 7 - Error while creating feed database entry.
*/ */
static function _subscribe($url, $cat_id = 0, static function _subscribe($url, $cat_id = 0,
$auth_login = '', $auth_pass = '') { $auth_login = '', $auth_pass = '') : array {
$pdo = Db::pdo(); $pdo = Db::pdo();
$url = UrlHelper::validate($url); $url = UrlHelper::validate($url);
if (!$url) return array("code" => 2); if (!$url) return ["code" => 2];
$contents = @UrlHelper::fetch($url, false, $auth_login, $auth_pass); $contents = UrlHelper::fetch($url, false, $auth_login, $auth_pass);
PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_SUBSCRIBE_FEED, PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_SUBSCRIBE_FEED,
function ($result) use (&$contents) { function ($result) use (&$contents) {
@ -1024,35 +1025,33 @@ class Feeds extends Handler_Protected {
$url = key($feedUrls); $url = key($feedUrls);
} }
if (!$cat_id) $cat_id = null; $feed = ORM::for_table('ttrss_feeds')
->where('feed_url', $url)
$sth = $pdo->prepare("SELECT id FROM ttrss_feeds ->where('owner_uid', $_SESSION['uid'])
WHERE feed_url = ? AND owner_uid = ?"); ->find_one();
$sth->execute([$url, $_SESSION['uid']]);
if ($row = $sth->fetch()) { if ($feed) {
return array("code" => 0, "feed_id" => (int) $row["id"]); return ["code" => 0, "feed_id" => $feed->id];
} else { } else {
$sth = $pdo->prepare( $feed = ORM::for_table('ttrss_feeds')->create();
"INSERT INTO ttrss_feeds
(owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method,auth_pass_encrypted) $feed->set([
VALUES (?, ?, ?, ?, ?, ?, 0, false)"); 'owner_uid' => $_SESSION['uid'],
'feed_url' => $url,
$sth->execute([$_SESSION['uid'], $url, "[Unknown]", $cat_id, (string)$auth_login, (string)$auth_pass]); 'title' => "[Unknown]",
'cat_id' => $cat_id ? $cat_id : null,
$sth = $pdo->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ? 'auth_login' => (string)$auth_login,
AND owner_uid = ?"); 'auth_pass' => (string)$auth_pass,
$sth->execute([$url, $_SESSION['uid']]); 'update_method' => 0,
$row = $sth->fetch(); 'auth_pass_encrypted' => false,
]);
$feed_id = $row["id"];
if ($feed->save()) {
if ($feed_id) { RSSUtils::set_basic_feed_info($feed->id);
RSSUtils::set_basic_feed_info($feed_id); return ["code" => 1, "feed_id" => (int) $feed->id];
} }
return array("code" => 1, "feed_id" => (int) $feed_id); return ["code" => 7];
} }
} }

@ -154,7 +154,7 @@ class Bookmarklets extends Plugin {
<a href='index.php'><?= __("Return to Tiny Tiny RSS") ?></a> <a href='index.php'><?= __("Return to Tiny Tiny RSS") ?></a>
</form> </form>
<?php <?php
} else { } else if (!$feed_urls) {
?> ?>
<a href='index.php'><?= __("Return to Tiny Tiny RSS") ?></a> <a href='index.php'><?= __("Return to Tiny Tiny RSS") ?></a>
<?php <?php

Loading…
Cancel
Save