]> git.wh0rd.org - tt-rss.git/blobdiff - classes/handler/public.php
move counter cache to a separate class
[tt-rss.git] / classes / handler / public.php
index dafe2a8c5cfc542bc0aa0071a7a623e52d591355..d525bc54c001405ac565233c9db842c9a126ad5f 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";
@@ -37,12 +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, true, 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];
 
@@ -60,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, true, 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]);
@@ -93,22 +112,26 @@ class Handler_Public extends Handler {
 
                        $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,
-                                       $feed_site_url);
+                                       $feed_site_url, false, $line["id"]);
 
                                if ($line['note']) {
                                        $content = "<div style=\"$note_style\">Article note: " . $line['note'] . "</div>" .
@@ -125,22 +148,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);
@@ -181,23 +204,30 @@ class Handler_Public extends Handler {
                        $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 +237,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 +272,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 {
@@ -357,7 +387,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 +405,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 +430,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 +529,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>
@@ -627,7 +656,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 +736,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 +965,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 +975,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 +1001,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,14 +1031,20 @@ class Handler_Public extends Handler {
                <?php
        }
 
-       function cached_image() {
+       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 . '.png';
+                       $filename = CACHE_DIR . '/images/' . $hash;
 
                        if (file_exists($filename)) {
+                               header("Content-Disposition: inline; filename=\"$hash\"");
+                               $mimetype = mime_content_type($filename);
+
                                /* See if we can use X-Sendfile */
                                $xsendfile = false;
                                if (function_exists('apache_get_modules') &&
@@ -1018,10 +1053,10 @@ class Handler_Public extends Handler {
 
                                if ($xsendfile) {
                                        header("X-Sendfile: $filename");
-                                       header("Content-type: application/octet-stream");
-                                       header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
+                                       header("Content-type: $mimetype");
+                                       header('Content-Disposition: inline; filename="' . basename($filename) . '"');
                                } else {
-                                       header("Content-type: image/png");
+                                       header("Content-type: $mimetype");
                                        $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
                                        header("Last-Modified: $stamp", true);
                                        readfile($filename);
@@ -1033,5 +1068,43 @@ class Handler_Public extends Handler {
                }
        }
 
+       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