]>
git.wh0rd.org - tt-rss.git/blob - classes/handler/public.php
2 class Handler_Public
extends Handler
{
4 private function generate_syndicated_feed($owner_uid, $feed, $is_cat,
5 $limit, $search, $search_mode, $match_on, $view_mode = false, $format = 'atom') {
7 require_once "lib/MiniTemplator.class.php";
9 $note_style = "background-color : #fff7d5;
10 border-width : 1px; ".
11 "padding : 5px; border-style : dashed; border-color : #e7d796;".
12 "margin-bottom : 1em; color : #9a8c59;";
14 if (!$limit) $limit = 100;
16 if (get_pref($this->link
, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
17 $date_sort_field = "updated";
19 $date_sort_field = "date_entered";
22 $qfh_ret = queryFeedHeadlines($this->link
, $feed,
23 $limit, $view_mode, $is_cat, $search, $search_mode,
24 $match_on, "$date_sort_field DESC", 0, $owner_uid);
26 $result = $qfh_ret[0];
27 $feed_title = htmlspecialchars($qfh_ret[1]);
28 $feed_site_url = $qfh_ret[2];
29 $last_error = $qfh_ret[3];
31 $feed_self_url = get_self_url_prefix() .
32 "/public.php?op=rss&id=-2&key=" .
33 get_feed_access_key($this->link
, -2, false, $owner_uid);
35 if (!$feed_site_url) $feed_site_url = get_self_url_prefix();
37 if ($format == 'atom') {
38 $tpl = new MiniTemplator
;
40 $tpl->readTemplateFromFile("templates/generated_feed.txt");
42 $tpl->setVariable('FEED_TITLE', $feed_title, true);
43 $tpl->setVariable('VERSION', VERSION
, true);
44 $tpl->setVariable('FEED_URL', htmlspecialchars($feed_self_url), true);
46 if (PUBSUBHUBBUB_HUB
&& $feed == -2) {
47 $tpl->setVariable('HUB_URL', htmlspecialchars(PUBSUBHUBBUB_HUB
), true);
48 $tpl->addBlock('feed_hub');
51 $tpl->setVariable('SELF_URL', htmlspecialchars(get_self_url_prefix()), true);
53 while ($line = db_fetch_assoc($result)) {
54 $tpl->setVariable('ARTICLE_ID', htmlspecialchars($line['link']), true);
55 $tpl->setVariable('ARTICLE_LINK', htmlspecialchars($line['link']), true);
56 $tpl->setVariable('ARTICLE_TITLE', htmlspecialchars($line['title']), true);
57 $tpl->setVariable('ARTICLE_EXCERPT',
58 truncate_string(strip_tags($line["content_preview"]), 100, '...'), true);
60 $content = sanitize($this->link
, $line["content_preview"], false, $owner_uid);
63 $content = "<div style=\"$note_style\">Article note: " . $line['note'] . "</div>" .
67 $tpl->setVariable('ARTICLE_CONTENT', $content, true);
69 $tpl->setVariable('ARTICLE_UPDATED_ATOM',
70 date('c', strtotime($line["updated"])), true);
71 $tpl->setVariable('ARTICLE_UPDATED_RFC822',
72 date(DATE_RFC822
, strtotime($line["updated"])), true);
74 $tpl->setVariable('ARTICLE_AUTHOR', htmlspecialchars($line['author']), true);
76 $tags = get_article_tags($this->link
, $line["id"], $owner_uid);
78 foreach ($tags as $tag) {
79 $tpl->setVariable('ARTICLE_CATEGORY', htmlspecialchars($tag), true);
80 $tpl->addBlock('category');
83 $enclosures = get_article_enclosures($this->link
, $line["id"]);
85 foreach ($enclosures as $e) {
86 $type = htmlspecialchars($e['content_type']);
87 $url = htmlspecialchars($e['content_url']);
88 $length = $e['duration'];
90 $tpl->setVariable('ARTICLE_ENCLOSURE_URL', $url, true);
91 $tpl->setVariable('ARTICLE_ENCLOSURE_TYPE', $type, true);
92 $tpl->setVariable('ARTICLE_ENCLOSURE_LENGTH', $length, true);
94 $tpl->addBlock('enclosure');
97 $tpl->addBlock('entry');
102 $tpl->addBlock('feed');
103 $tpl->generateOutputToString($tmp);
105 header("Content-Type: text/xml; charset=utf-8");
108 } else if ($format == 'json') {
112 $feed['title'] = $feed_title;
113 $feed['version'] = VERSION
;
114 $feed['feed_url'] = $feed_self_url;
116 if (PUBSUBHUBBUB_HUB
&& $feed == -2) {
117 $feed['hub_url'] = PUBSUBHUBBUB_HUB
;
120 $feed['self_url'] = get_self_url_prefix();
122 $feed['articles'] = array();
124 while ($line = db_fetch_assoc($result)) {
127 $article['id'] = $line['link'];
128 $article['link'] = $line['link'];
129 $article['title'] = $line['title'];
130 $article['excerpt'] = truncate_string(strip_tags($line["content_preview"]), 100, '...');
131 $article['content'] = sanitize($this->link
, $line["content_preview"], false, $owner_uid);
132 $article['updated'] = date('c', strtotime($line["updated"]));
134 if ($line['note']) $article['note'] = $line['note'];
135 if ($article['author']) $article['author'] = $line['author'];
137 $tags = get_article_tags($this->link
, $line["id"], $owner_uid);
139 if (count($tags) > 0) {
140 $article['tags'] = array();
142 foreach ($tags as $tag) {
143 array_push($article['tags'], $tag);
147 $enclosures = get_article_enclosures($this->link
, $line["id"]);
149 if (count($enclosures) > 0) {
150 $article['enclosures'] = array();
152 foreach ($enclosures as $e) {
153 $type = $e['content_type'];
154 $url = $e['content_url'];
155 $length = $e['duration'];
157 array_push($article['enclosures'], array("url" => $url, "type" => $type, "length" => $length));
161 array_push($feed['articles'], $article);
164 header("Content-Type: text/plain; charset=utf-8");
166 print json_encode($feed);
169 header("Content-Type: text/plain; charset=utf-8");
170 print json_encode(array("error" => array("message" => "Unknown format")));
174 function getUnread() {
175 $login = db_escape_string($_REQUEST["login"]);
176 $fresh = $_REQUEST["fresh"] == "1";
178 $result = db_query($this->link
, "SELECT id FROM ttrss_users WHERE login = '$login'");
180 if (db_num_rows($result) == 1) {
181 $uid = db_fetch_result($result, 0, "id");
183 print getGlobalUnread($this->link
, $uid);
187 print getFeedArticles($this->link
, -3, false, true, $uid);
191 print "-1;User not found";
196 function getProfiles() {
197 $login = db_escape_string($_REQUEST["login"]);
199 $result = db_query($this->link
, "SELECT * FROM ttrss_settings_profiles,ttrss_users
200 WHERE ttrss_users.id = ttrss_settings_profiles.owner_uid AND login = '$login' ORDER BY title");
202 print "<select style='width: 100%' name='profile'>";
204 print "<option value='0'>" . __("Default profile") . "</option>";
206 while ($line = db_fetch_assoc($result)) {
208 $title = $line["title"];
210 print "<option value='$id'>$title</option>";
217 $mode = db_escape_string($_REQUEST['hub_mode']);
218 $feed_id = (int) db_escape_string($_REQUEST['id']);
219 $feed_url = db_escape_string($_REQUEST['hub_topic']);
221 if (!PUBSUBHUBBUB_ENABLED
) {
222 header('HTTP/1.0 404 Not Found');
223 echo "404 Not found";
227 // TODO: implement hub_verifytoken checking
229 $result = db_query($this->link
, "SELECT feed_url FROM ttrss_feeds
230 WHERE id = '$feed_id'");
232 if (db_num_rows($result) != 0) {
234 $check_feed_url = db_fetch_result($result, 0, "feed_url");
236 if ($check_feed_url && ($check_feed_url == $feed_url ||
!$feed_url)) {
237 if ($mode == "subscribe") {
239 db_query($this->link
, "UPDATE ttrss_feeds SET pubsub_state = 2
240 WHERE id = '$feed_id'");
242 print $_REQUEST['hub_challenge'];
245 } else if ($mode == "unsubscribe") {
247 db_query($this->link
, "UPDATE ttrss_feeds SET pubsub_state = 0
248 WHERE id = '$feed_id'");
250 print $_REQUEST['hub_challenge'];
255 // Received update ping, schedule feed update.
256 //update_rss_feed($this->link, $feed_id, true, true);
258 db_query($this->link
, "UPDATE ttrss_feeds SET
259 last_update_started = '1970-01-01',
260 last_updated = '1970-01-01' WHERE id = '$feed_id'");
264 header('HTTP/1.0 404 Not Found');
265 echo "404 Not found";
268 header('HTTP/1.0 404 Not Found');
269 echo "404 Not found";
276 header("Location: index.php");
279 function fbexport() {
281 $access_key = db_escape_string($_POST["key"]);
283 // TODO: rate limit checking using last_connected
284 $result = db_query($this->link
, "SELECT id FROM ttrss_linked_instances
285 WHERE access_key = '$access_key'");
287 if (db_num_rows($result) == 1) {
289 $instance_id = db_fetch_result($result, 0, "id");
291 $result = db_query($this->link
, "SELECT feed_url, site_url, title, subscribers
292 FROM ttrss_feedbrowser_cache ORDER BY subscribers DESC LIMIT 100");
296 while ($line = db_fetch_assoc($result)) {
297 array_push($feeds, $line);
300 db_query($this->link
, "UPDATE ttrss_linked_instances SET
301 last_status_in = 1 WHERE id = '$instance_id'");
303 print json_encode(array("feeds" => $feeds));
305 print json_encode(array("error" => array("code" => 6)));
310 $uuid = db_escape_string($_REQUEST["key"]);
312 $result = db_query($this->link
, "SELECT ref_id, owner_uid FROM ttrss_user_entries WHERE
315 if (db_num_rows($result) != 0) {
316 header("Content-Type: text/html");
318 $id = db_fetch_result($result, 0, "ref_id");
319 $owner_uid = db_fetch_result($result, 0, "owner_uid");
321 $article = format_article($this->link
, $id, false, true, $owner_uid);
323 print_r($article['content']);
326 print "Article not found.";
332 $feed = db_escape_string($_REQUEST["id"]);
333 $key = db_escape_string($_REQUEST["key"]);
334 $is_cat = $_REQUEST["is_cat"] != false;
335 $limit = (int)db_escape_string($_REQUEST["limit"]);
337 $search = db_escape_string($_REQUEST["q"]);
338 $match_on = db_escape_string($_REQUEST["m"]);
339 $search_mode = db_escape_string($_REQUEST["smode"]);
340 $view_mode = db_escape_string($_REQUEST["view-mode"]);
342 $format = db_escape_string($_REQUEST['format']);
344 if (!$format) $format = 'atom';
346 if (SINGLE_USER_MODE
) {
347 authenticate_user($this->link
, "admin", null);
353 $result = db_query($this->link
, "SELECT owner_uid FROM
354 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
356 if (db_num_rows($result) == 1)
357 $owner_id = db_fetch_result($result, 0, "owner_uid");
361 $this->generate_syndicated_feed($owner_id, $feed, $is_cat, $limit,
362 $search, $search_mode, $match_on, $view_mode, $format);
364 header('HTTP/1.1 403 Forbidden');
368 function globalUpdateFeeds() {
369 include "rssfuncs.php";
370 // Update all feeds needing a update.
371 update_daemon_common($this->link
, 0, true, false);
374 function sharepopup() {
375 header('Content-Type: text/html; charset=utf-8');
378 <title>Tiny Tiny RSS</title>
379 <link rel=\"stylesheet\" type=\"text/css\" href=\"utility.css\">
380 <script type=\"text/javascript\" src=\"lib/prototype.js\"></script>
381 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
383 <body id='sharepopup'>";
385 $action = $_REQUEST["action"];
387 if ($_SESSION["uid"]) {
391 print "<table height='100%' width='100%'><tr><td colspan='2'>";
392 print "<h1>".__("Share with Tiny Tiny RSS")."</h1>";
395 print "<form id='share_form' name='share_form'>";
397 print "<input type=\"hidden\" name=\"op\" value=\"sharepopup\">";
398 print "<input type=\"hidden\" name=\"action\" value=\"share\">";
400 $title = htmlspecialchars($_REQUEST["title"]);
401 $url = htmlspecialchars($_REQUEST["url"]);
403 print "<tr><td>".__("Title:")."</td><td width='80%'><input name='title' value=\"$title\"></td></tr>";
404 print "<tr><td>".__("URL:")."</td><td><input name='url' value=\"$url\"></td></tr>";
405 print "<tr><td>".__("Content:")."</td><td><input name='content' value=\"\"></td></tr>";
407 print "<script type='text/javascript'>";
408 print "document.forms[0].title.focus();";
411 print "<tr><td colspan='2'>
412 <div style='float : right' class='insensitive-small'>".
413 __("Shared article will appear in the Published feed.").
414 "</div><button type=\"submit\">".
415 __('Share')."</button>
416 <button onclick=\"return window.close()\">".
417 __('Cancel')."</button>
421 print "</td></tr></table>";
423 print "</body></html>";
427 $title = db_escape_string(strip_tags($_REQUEST["title"]));
428 $url = db_escape_string(strip_tags($_REQUEST["url"]));
429 $content = db_escape_string(strip_tags($_REQUEST["content"]));
431 create_published_article($this->link
, $title, $url, $content, $_SESSION["uid"]);
433 print "<script type='text/javascript'>";
434 print "window.close();";
440 print "<table><tr><td>" . __("Not logged in.") . "</td></tr></table>";
449 $_SESSION["prefs_cache"] = array();
451 if (!SINGLE_USER_MODE
) {
453 $login = db_escape_string($_POST["login"]);
454 $password = $_POST["password"];
455 $remember_me = $_POST["remember_me"];
457 if (authenticate_user($this->link
, $login, $password)) {
458 $_POST["password"] = "";
460 $_SESSION["language"] = $_POST["language"];
461 $_SESSION["ref_schema_version"] = get_schema_version($this->link
, true);
462 $_SESSION["bw_limit"] = !!$_POST["bw_limit"];
464 if ($_POST["profile"]) {
466 $profile = db_escape_string($_POST["profile"]);
468 $result = db_query($this->link
, "SELECT id FROM ttrss_settings_profiles
469 WHERE id = '$profile' AND owner_uid = " . $_SESSION["uid"]);
471 if (db_num_rows($result) != 0) {
472 $_SESSION["profile"] = $profile;
473 $_SESSION["prefs_cache"] = array();
477 $_SESSION["login_error_msg"] = __("Incorrect username or password");
480 if ($_REQUEST['return']) {
481 header("Location: " . $_REQUEST['return']);
483 header("Location: " . SELF_URL_PATH
);
488 function subscribe() {
489 if ($_SESSION["uid"]) {
491 $feed_url = db_escape_string(trim($_REQUEST["feed_url"]));
493 header('Content-Type: text/html; charset=utf-8');
496 <title>Tiny Tiny RSS</title>
497 <link rel=\"stylesheet\" type=\"text/css\" href=\"utility.css\">
498 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
501 <img class=\"floatingLogo\" src=\"images/logo_wide.png\"
502 alt=\"Tiny Tiny RSS\"/>
503 <h1>".__("Subscribe to feed...")."</h1>";
505 $rc = subscribe_to_feed($this->link
, $feed_url);
507 switch ($rc['code']) {
509 print_warning(T_sprintf("Already subscribed to <b>%s</b>.", $feed_url));
512 print_notice(T_sprintf("Subscribed to <b>%s</b>.", $feed_url));
515 print_error(T_sprintf("Could not subscribe to <b>%s</b>.", $feed_url));
518 print_error(T_sprintf("No feeds found in <b>%s</b>.", $feed_url));
521 print_notice(__("Multiple feed URLs found."));
522 $feed_urls = get_feeds_from_html($feed_url);
525 print_error(T_sprintf("Could not subscribe to <b>%s</b>.<br>Can't download the Feed URL.", $feed_url));
531 print "<form action=\"public.php\">";
532 print "<input type=\"hidden\" name=\"op\" value=\"subscribe\">";
534 print "<select name=\"feed_url\">";
536 foreach ($feed_urls as $url => $name) {
537 $url = htmlspecialchars($url);
538 $name = htmlspecialchars($name);
540 print "<option value=\"$url\">$name</option>";
543 print "<input type=\"submit\" value=\"".__("Subscribe to selected feed").
549 $tp_uri = get_self_url_prefix() . "/prefs.php";
550 $tt_uri = get_self_url_prefix();
552 if ($rc['code'] <= 2){
553 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
554 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
556 $feed_id = db_fetch_result($result, 0, "id");
563 print "<form method=\"GET\" style='display: inline'
565 <input type=\"hidden\" name=\"tab\" value=\"feedConfig\">
566 <input type=\"hidden\" name=\"method\" value=\"editFeed\">
567 <input type=\"hidden\" name=\"methodparam\" value=\"$feed_id\">
568 <input type=\"submit\" value=\"".__("Edit subscription options")."\">
572 print "<form style='display: inline' method=\"GET\" action=\"$tt_uri\">
573 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
576 print "</body></html>";
579 render_login_form($this->link
);
583 function subscribe2() {
584 $feed_url = db_escape_string(trim($_REQUEST["feed_url"]));
585 $cat_id = db_escape_string($_REQUEST["cat_id"]);
586 $from = db_escape_string($_REQUEST["from"]);
588 /* only read authentication information from POST */
590 $auth_login = db_escape_string(trim($_POST["auth_login"]));
591 $auth_pass = db_escape_string(trim($_POST["auth_pass"]));
593 $rc = subscribe_to_feed($this->link
, $feed_url, $cat_id, $auth_login, $auth_pass);
597 print_notice(T_sprintf("Subscribed to <b>%s</b>.", $feed_url));
600 print_error(T_sprintf("Could not subscribe to <b>%s</b>.", $feed_url));
603 print_error(T_sprintf("No feeds found in <b>%s</b>.", $feed_url));
606 print_warning(T_sprintf("Already subscribed to <b>%s</b>.", $feed_url));
609 print_notice(__("Multiple feed URLs found."));
611 $feed_urls = get_feeds_from_html($feed_url);
614 print_error(T_sprintf("Could not subscribe to <b>%s</b>.<br>Can't download the Feed URL.", $feed_url));
619 print "<form action=\"backend.php\">";
620 print "<input type=\"hidden\" name=\"op\" value=\"pref-feeds\">";
621 print "<input type=\"hidden\" name=\"quiet\" value=\"1\">";
622 print "<input type=\"hidden\" name=\"method\" value=\"add\">";
624 print "<select name=\"feed_url\">";
626 foreach ($feed_urls as $url => $name) {
627 $url = htmlspecialchars($url);
628 $name = htmlspecialchars($name);
629 print "<option value=\"$url\">$name</option>";
632 print "<input type=\"submit\" value=\"".__("Subscribe to selected feed")."\">";
636 $tp_uri = get_self_url_prefix() . "/prefs.php";
637 $tt_uri = get_self_url_prefix();
640 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
641 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
643 $feed_id = db_fetch_result($result, 0, "id");
651 print "<form method=\"GET\" style='display: inline'
653 <input type=\"hidden\" name=\"tab\" value=\"feedConfig\">
654 <input type=\"hidden\" name=\"method\" value=\"editFeed\">
655 <input type=\"hidden\" name=\"methodparam\" value=\"$feed_id\">
656 <input type=\"submit\" value=\"".__("Edit subscription options")."\">
660 print "<form style='display: inline' method=\"GET\" action=\"$tt_uri\">
661 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
664 print "</body></html>";
668 header("Content-Type: text/plain");
669 print json_encode(array("error" => array("code" => 7)));