]> git.wh0rd.org - tt-rss.git/blobdiff - classes/handler/public.php
Merge branch 'sort_title_date' of ggrandou/tt-rss into master
[tt-rss.git] / classes / handler / public.php
index 30506fdadf2c11658cd08f36ce02b6f9d9a36570..d3c854ae47a143402e285229dff198e700319032 100644 (file)
@@ -2,7 +2,7 @@
 class Handler_Public extends Handler {
 
        private function generate_syndicated_feed($owner_uid, $feed, $is_cat,
-               $limit, $offset, $search, $search_mode,
+               $limit, $offset, $search,
                $view_mode = false, $format = 'atom', $order = false, $orig_guid = false, $start_ts = false) {
 
                require_once "lib/MiniTemplator.class.php";
@@ -27,7 +27,7 @@ class Handler_Public extends Handler {
 
                switch ($order) {
                case "title":
-                       $date_sort_field = "ttrss_entries.title";
+                       $date_sort_field = "ttrss_entries.title, date_entered, updated";
                        break;
                case "date_reverse":
                        $date_sort_field = "date_entered, updated";
@@ -37,13 +37,21 @@ class Handler_Public extends Handler {
                        break;
                }
 
-               //function queryFeedHeadlines($feed, $limit, $view_mode, $cat_view, $search, $search_mode, $override_order = false, $offset = 0, $owner_uid = 0, $filter = false, $since_id = 0, $include_children = false, $ignore_vfeed_group = false, $override_strategy = false, $override_vfeed = false, $start_ts = false) {
-
-
-               $qfh_ret = queryFeedHeadlines($feed,
-                       1, $view_mode, $is_cat, $search, $search_mode,
-                       $date_sort_field, $offset, $owner_uid,
-                       false, 0, false, true, false, false, $start_ts);
+               $params = array(
+                       "owner_uid" => $owner_uid,
+                       "feed" => $feed,
+                       "limit" => 1,
+                       "view_mode" => $view_mode,
+                       "cat_view" => $is_cat,
+                       "search" => $search,
+                       "override_order" => $date_sort_field,
+                       "include_children" => true,
+                       "ignore_vfeed_group" => true,
+                       "offset" => $offset,
+                       "start_ts" => $start_ts
+               );
+
+               $qfh_ret = Feeds::queryFeedHeadlines($params);
 
                $result = $qfh_ret[0];
 
@@ -61,11 +69,21 @@ class Handler_Public extends Handler {
                        header("Last-Modified: $last_modified", true);
                }
 
-               $qfh_ret = queryFeedHeadlines($feed,
-                       $limit, $view_mode, $is_cat, $search, $search_mode,
-                       $date_sort_field, $offset, $owner_uid,
-                       false, 0, false, true, false, false, $start_ts);
-
+               $params = array(
+                       "owner_uid" => $owner_uid,
+                       "feed" => $feed,
+                       "limit" => $limit,
+                       "view_mode" => $view_mode,
+                       "cat_view" => $is_cat,
+                       "search" => $search,
+                       "override_order" => $date_sort_field,
+                       "include_children" => true,
+                       "ignore_vfeed_group" => true,
+                       "offset" => $offset,
+                       "start_ts" => $start_ts
+               );
+
+               $qfh_ret = Feeds::queryFeedHeadlines($params);
 
                $result = $qfh_ret[0];
                $feed_title = htmlspecialchars($qfh_ret[1]);
@@ -87,28 +105,28 @@ class Handler_Public extends Handler {
                        $tpl->setVariable('VERSION', VERSION, true);
                        $tpl->setVariable('FEED_URL', htmlspecialchars($feed_self_url), true);
 
-                       if (PUBSUBHUBBUB_HUB && $feed == -2) {
-                               $tpl->setVariable('HUB_URL', htmlspecialchars(PUBSUBHUBBUB_HUB), true);
-                               $tpl->addBlock('feed_hub');
-                       }
-
                        $tpl->setVariable('SELF_URL', htmlspecialchars(get_self_url_prefix()), true);
                        while ($line = $this->dbh->fetch_assoc($result)) {
-                               $line["content_preview"] = truncate_string(strip_tags($line["content"]), 100, '...');
+
+                               $line["content_preview"] = sanitize(truncate_string(strip_tags($line["content"]), 100, '...'));
 
                                foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
                                        $line = $p->hook_query_headlines($line);
                                }
 
+                               foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_EXPORT_FEED) as $p) {
+                                       $line = $p->hook_article_export_feed($line, $feed, $is_cat);
+                               }
+
                                $tpl->setVariable('ARTICLE_ID',
                                        htmlspecialchars($orig_guid ? $line['link'] :
-                                               get_self_url_prefix() .
-                                                       "/public.php?url=" . urlencode($line['link'])), true);
+                                                       $this->make_article_tag_uri($line['id'], $line['date_entered'])), true);
                                $tpl->setVariable('ARTICLE_LINK', htmlspecialchars($line['link']), true);
                                $tpl->setVariable('ARTICLE_TITLE', htmlspecialchars($line['title']), true);
                                $tpl->setVariable('ARTICLE_EXCERPT', $line["content_preview"], true);
 
-                               $content = sanitize($line["content"], false, $owner_uid);
+                               $content = sanitize($line["content"], false, $owner_uid,
+                                       $feed_site_url, false, $line["id"]);
 
                                if ($line['note']) {
                                        $content = "<div style=\"$note_style\">Article note: " . $line['note'] . "</div>" .
@@ -125,22 +143,22 @@ class Handler_Public extends Handler {
 
                                $tpl->setVariable('ARTICLE_AUTHOR', htmlspecialchars($line['author']), true);
 
-                               $tpl->setVariable('ARTICLE_SOURCE_LINK', htmlspecialchars($line['site_url']), true);
+                               $tpl->setVariable('ARTICLE_SOURCE_LINK', htmlspecialchars($line['site_url'] ? $line["site_url"] : get_self_url_prefix()), true);
                                $tpl->setVariable('ARTICLE_SOURCE_TITLE', htmlspecialchars($line['feed_title'] ? $line['feed_title'] : $feed_title), true);
 
-                               $tags = get_article_tags($line["id"], $owner_uid);
+                               $tags = Article::get_article_tags($line["id"], $owner_uid);
 
                                foreach ($tags as $tag) {
                                        $tpl->setVariable('ARTICLE_CATEGORY', htmlspecialchars($tag), true);
                                        $tpl->addBlock('category');
                                }
 
-                               $enclosures = get_article_enclosures($line["id"]);
+                               $enclosures = Article::get_article_enclosures($line["id"]);
 
                                foreach ($enclosures as $e) {
                                        $type = htmlspecialchars($e['content_type']);
                                        $url = htmlspecialchars($e['content_url']);
-                                       $length = $e['duration'];
+                                       $length = $e['duration'] ? $e['duration'] : 1;
 
                                        $tpl->setVariable('ARTICLE_ENCLOSURE_URL', $url, true);
                                        $tpl->setVariable('ARTICLE_ENCLOSURE_TYPE', $type, true);
@@ -172,32 +190,35 @@ class Handler_Public extends Handler {
                        $feed['version'] = VERSION;
                        $feed['feed_url'] = $feed_self_url;
 
-                       if (PUBSUBHUBBUB_HUB && $feed == -2) {
-                               $feed['hub_url'] = PUBSUBHUBBUB_HUB;
-                       }
-
                        $feed['self_url'] = get_self_url_prefix();
 
                        $feed['articles'] = array();
 
                        while ($line = $this->dbh->fetch_assoc($result)) {
-                               $line["content_preview"] = truncate_string(strip_tags($line["content_preview"]), 100, '...');
+
+                               $line["content_preview"] = sanitize(truncate_string(strip_tags($line["content_preview"]), 100, '...'));
+
                                foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
                                        $line = $p->hook_query_headlines($line, 100);
                                }
+
+                               foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_EXPORT_FEED) as $p) {
+                                       $line = $p->hook_article_export_feed($line, $feed, $is_cat);
+                               }
+
                                $article = array();
 
                                $article['id'] = $line['link'];
                                $article['link']        = $line['link'];
                                $article['title'] = $line['title'];
                                $article['excerpt'] = $line["content_preview"];
-                               $article['content'] = sanitize($line["content"], false, $owner_uid);
+                               $article['content'] = sanitize($line["content"], false, $owner_uid, $feed_site_url, false, $line["id"]);
                                $article['updated'] = date('c', strtotime($line["updated"]));
 
                                if ($line['note']) $article['note'] = $line['note'];
                                if ($article['author']) $article['author'] = $line['author'];
 
-                               $tags = get_article_tags($line["id"], $owner_uid);
+                               $tags = Article::get_article_tags($line["id"], $owner_uid);
 
                                if (count($tags) > 0) {
                                        $article['tags'] = array();
@@ -207,7 +228,7 @@ class Handler_Public extends Handler {
                                        }
                                }
 
-                               $enclosures = get_article_enclosures($line["id"]);
+                               $enclosures = Article::get_article_enclosures($line["id"]);
 
                                if (count($enclosures) > 0) {
                                        $article['enclosures'] = array();
@@ -242,11 +263,11 @@ class Handler_Public extends Handler {
                if ($this->dbh->num_rows($result) == 1) {
                        $uid = $this->dbh->fetch_result($result, 0, "id");
 
-                       print getGlobalUnread($uid);
+                       print Feeds::getGlobalUnread($uid);
 
                        if ($fresh) {
                                print ";";
-                               print getFeedArticles(-3, false, true, $uid);
+                               print Feeds::getFeedArticles(-3, false, true, $uid);
                        }
 
                } else {
@@ -275,71 +296,6 @@ class Handler_Public extends Handler {
                print "</select>";
        }
 
-       function pubsub() {
-               $mode = $this->dbh->escape_string($_REQUEST['hub_mode']);
-               if (!$mode) $mode = $this->dbh->escape_string($_REQUEST['hub.mode']);
-
-               $feed_id = (int) $this->dbh->escape_string($_REQUEST['id']);
-               $feed_url = $this->dbh->escape_string($_REQUEST['hub_topic']);
-
-               if (!$feed_url) $feed_url = $this->dbh->escape_string($_REQUEST['hub.topic']);
-
-               if (!PUBSUBHUBBUB_ENABLED) {
-                       header('HTTP/1.0 404 Not Found');
-                       echo "404 Not found (Disabled by server)";
-                       return;
-               }
-
-               // TODO: implement hub_verifytoken checking
-               // TODO: store requested rel=self or whatever for verification
-               // (may be different from stored feed url) e.g. http://url/ or http://url
-
-               $result = $this->dbh->query("SELECT feed_url FROM ttrss_feeds
-                       WHERE id = '$feed_id'");
-
-               if ($this->dbh->num_rows($result) != 0) {
-
-                       $check_feed_url = $this->dbh->fetch_result($result, 0, "feed_url");
-
-                       // ignore url checking for the time being
-                       if ($check_feed_url && (true || $check_feed_url == $feed_url || !$feed_url)) {
-                               if ($mode == "subscribe") {
-
-                                       $this->dbh->query("UPDATE ttrss_feeds SET pubsub_state = 2
-                                               WHERE id = '$feed_id'");
-
-                                       print $_REQUEST['hub_challenge'];
-                                       return;
-
-                               } else if ($mode == "unsubscribe") {
-
-                                       $this->dbh->query("UPDATE ttrss_feeds SET pubsub_state = 0
-                                               WHERE id = '$feed_id'");
-
-                                       print $_REQUEST['hub_challenge'];
-                                       return;
-
-                               } else if (!$mode) {
-
-                                       // Received update ping, schedule feed update.
-                                       //update_rss_feed($feed_id, true, true);
-
-                                       $this->dbh->query("UPDATE ttrss_feeds SET
-                                               last_update_started = '1970-01-01',
-                                               last_updated = '1970-01-01' WHERE id = '$feed_id'");
-
-                               }
-                       } else {
-                               header('HTTP/1.0 404 Not Found');
-                               echo "404 Not found (URL check failed)";
-                       }
-               } else {
-                       header('HTTP/1.0 404 Not Found');
-                       echo "404 Not found (Feed not found)";
-               }
-
-       }
-
        function logout() {
                logout_user();
                header("Location: index.php");
@@ -357,7 +313,7 @@ class Handler_Public extends Handler {
                        $id = $this->dbh->fetch_result($result, 0, "ref_id");
                        $owner_uid = $this->dbh->fetch_result($result, 0, "owner_uid");
 
-                       $article = format_article($id, false, true, $owner_uid);
+                       $article = Article::format_article($id, false, true, $owner_uid);
 
                        print_r($article['content']);
 
@@ -375,7 +331,6 @@ class Handler_Public extends Handler {
                $offset = (int)$this->dbh->escape_string($_REQUEST["offset"]);
 
                $search = $this->dbh->escape_string($_REQUEST["q"]);
-               $search_mode = $this->dbh->escape_string($_REQUEST["smode"]);
                $view_mode = $this->dbh->escape_string($_REQUEST["view-mode"]);
                $order = $this->dbh->escape_string($_REQUEST["order"]);
                $start_ts = $this->dbh->escape_string($_REQUEST["ts"]);
@@ -401,7 +356,7 @@ class Handler_Public extends Handler {
 
                if ($owner_id) {
                        $this->generate_syndicated_feed($owner_id, $feed, $is_cat, $limit,
-                               $offset, $search, $search_mode, $view_mode, $format, $order, $orig_guid, $start_ts);
+                               $offset, $search, $view_mode, $format, $order, $orig_guid, $start_ts);
                } else {
                        header('HTTP/1.1 403 Forbidden');
                }
@@ -500,7 +455,7 @@ class Handler_Public extends Handler {
                                        </div>
                                        <button type="submit"><?php echo __('Share') ?></button>
                                        <button onclick="return window.close()"><?php echo __('Cancel') ?></button>
-                                       </div>
+                                       </td>
 
                                </form>
                                </td></tr></table>
@@ -586,7 +541,7 @@ class Handler_Public extends Handler {
                        if ($_REQUEST['return']) {
                                header("Location: " . $_REQUEST['return']);
                        } else {
-                               header("Location: " . SELF_URL_PATH);
+                               header("Location: " . get_self_url_prefix());
                        }
                }
        }
@@ -627,7 +582,7 @@ class Handler_Public extends Handler {
                                        alt=\"Tiny Tiny RSS\"/>
                                        <h1>".__("Subscribe to feed...")."</h1><div class='content'>";
 
-                       $rc = subscribe_to_feed($feed_url);
+                       $rc = Feeds::subscribe_to_feed($feed_url);
 
                        switch ($rc['code']) {
                        case 0:
@@ -707,7 +662,7 @@ class Handler_Public extends Handler {
 
        function index() {
                header("Content-Type: text/plain");
-               print json_encode(array("error" => array("code" => 7)));
+               print error_json(13);
        }
 
        function forgotpass() {
@@ -936,7 +891,7 @@ class Handler_Public extends Handler {
                                                for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
                                                        print "<li>Performing update up to version $i...";
 
-                                                       $result = $updater->performUpdateTo($i);
+                                                       $result = $updater->performUpdateTo($i, true);
 
                                                        if (!$result) {
                                                                print "<span class='err'>FAILED!</span></li></ul>";
@@ -946,7 +901,7 @@ class Handler_Public extends Handler {
                                                                <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
                                                                </form>";
 
-                                                               break;
+                                                               return;
                                                        } else {
                                                                print "<span class='ok'>OK!</span></li>";
                                                        }
@@ -972,10 +927,10 @@ class Handler_Public extends Handler {
 
                                                print "<h2>Database update required</h2>";
 
-                                               print "<h3>";
-                                               printf("Your Tiny Tiny RSS database needs update to the latest version: %d to %d.",
-                                                       $updater->getSchemaVersion(), SCHEMA_VERSION);
-                                               print "</h3>";
+                                               print_notice("<h4>".
+                                               sprintf("Your Tiny Tiny RSS database needs update to the latest version: %d to %d.",
+                                                       $updater->getSchemaVersion(), SCHEMA_VERSION).
+                                               "</h4>");
 
                                                print_warning("Please backup your database before proceeding.");
 
@@ -1002,5 +957,65 @@ class Handler_Public extends Handler {
                <?php
        }
 
+       function cached_url() {
+               @$hash = basename($_GET['hash']);
+
+               // we don't need an extension to find the file, hash is a complete URL
+               $hash = preg_replace("/\.[^\.]*$/", "", $hash);
+
+               if ($hash) {
+
+                       $filename = CACHE_DIR . '/images/' . $hash;
+
+                       if (file_exists($filename)) {
+                               header("Content-Disposition: inline; filename=\"$hash\"");
+
+                               send_local_file($filename);
+
+                       } else {
+                               header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
+                               echo "File not found.";
+                       }
+               }
+       }
+
+       private function make_article_tag_uri($id, $timestamp) {
+
+               $timestamp = date("Y-m-d", strtotime($timestamp));
+
+               return "tag:" . parse_url(get_self_url_prefix(), PHP_URL_HOST) . ",$timestamp:/$id";
+       }
+
+       // this should be used very carefully because this endpoint is exposed to unauthenticated users
+       // plugin data is not loaded because there's no user context and owner_uid/session may or may not be available
+       // in general, don't do anything user-related in here and do not modify $_SESSION
+       public function pluginhandler() {
+               $host = new PluginHost();
+
+               $plugin = basename($_REQUEST["plugin"]);
+               $method = $_REQUEST["pmethod"];
+
+               $host->load($plugin, PluginHost::KIND_USER, 0);
+               $host->load_data();
+
+               $pclass = $host->get_plugin($plugin);
+
+               if ($pclass) {
+                       if (method_exists($pclass, $method)) {
+                               if ($pclass->is_public_method($method)) {
+                                       $pclass->$method();
+                               } else {
+                                       header("Content-Type: text/json");
+                                       print error_json(6);
+                               }
+                       } else {
+                               header("Content-Type: text/json");
+                               print error_json(13);
+                       }
+               } else {
+                       header("Content-Type: text/json");
+                       print error_json(14);
+               }
+       }
 }
-?>
+?>
\ No newline at end of file