public.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. class Handler_Public extends Handler {
  3. private function generate_syndicated_feed($owner_uid, $feed, $is_cat,
  4. $limit, $search, $search_mode, $match_on, $view_mode = false) {
  5. require_once "lib/MiniTemplator.class.php";
  6. $note_style = "background-color : #fff7d5;
  7. border-width : 1px; ".
  8. "padding : 5px; border-style : dashed; border-color : #e7d796;".
  9. "margin-bottom : 1em; color : #9a8c59;";
  10. if (!$limit) $limit = 30;
  11. if (get_pref($this->link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
  12. $date_sort_field = "updated";
  13. } else {
  14. $date_sort_field = "date_entered";
  15. }
  16. $qfh_ret = queryFeedHeadlines($this->link, $feed,
  17. $limit, $view_mode, $is_cat, $search, $search_mode,
  18. $match_on, "$date_sort_field DESC", 0, $owner_uid);
  19. $result = $qfh_ret[0];
  20. $feed_title = htmlspecialchars($qfh_ret[1]);
  21. $feed_site_url = $qfh_ret[2];
  22. $last_error = $qfh_ret[3];
  23. $feed_self_url = get_self_url_prefix() .
  24. "/public.php?op=rss&id=-2&key=" .
  25. get_feed_access_key($this->link, -2, false, $owner_uid);
  26. if (!$feed_site_url) $feed_site_url = get_self_url_prefix();
  27. $tpl = new MiniTemplator;
  28. $tpl->readTemplateFromFile("templates/generated_feed.txt");
  29. $tpl->setVariable('FEED_TITLE', $feed_title, true);
  30. $tpl->setVariable('VERSION', VERSION, true);
  31. $tpl->setVariable('FEED_URL', htmlspecialchars($feed_self_url), true);
  32. if (PUBSUBHUBBUB_HUB && $feed == -2) {
  33. $tpl->setVariable('HUB_URL', htmlspecialchars(PUBSUBHUBBUB_HUB), true);
  34. $tpl->addBlock('feed_hub');
  35. }
  36. $tpl->setVariable('SELF_URL', htmlspecialchars(get_self_url_prefix()), true);
  37. while ($line = db_fetch_assoc($result)) {
  38. $tpl->setVariable('ARTICLE_ID', htmlspecialchars($line['link']), true);
  39. $tpl->setVariable('ARTICLE_LINK', htmlspecialchars($line['link']), true);
  40. $tpl->setVariable('ARTICLE_TITLE', htmlspecialchars($line['title']), true);
  41. $tpl->setVariable('ARTICLE_EXCERPT',
  42. truncate_string(strip_tags($line["content_preview"]), 100, '...'), true);
  43. $content = sanitize($this->link, $line["content_preview"], false, $owner_uid);
  44. if ($line['note']) {
  45. $content = "<div style=\"$note_style\">Article note: " . $line['note'] . "</div>" .
  46. $content;
  47. }
  48. $tpl->setVariable('ARTICLE_CONTENT', $content, true);
  49. $tpl->setVariable('ARTICLE_UPDATED_ATOM',
  50. date('c', strtotime($line["updated"])), true);
  51. $tpl->setVariable('ARTICLE_UPDATED_RFC822',
  52. date(DATE_RFC822, strtotime($line["updated"])), true);
  53. $tpl->setVariable('ARTICLE_AUTHOR', htmlspecialchars($line['author']), true);
  54. $tags = get_article_tags($this->link, $line["id"], $owner_uid);
  55. foreach ($tags as $tag) {
  56. $tpl->setVariable('ARTICLE_CATEGORY', htmlspecialchars($tag), true);
  57. $tpl->addBlock('category');
  58. }
  59. $enclosures = get_article_enclosures($this->link, $line["id"]);
  60. foreach ($enclosures as $e) {
  61. $type = htmlspecialchars($e['content_type']);
  62. $url = htmlspecialchars($e['content_url']);
  63. $length = $e['duration'];
  64. $tpl->setVariable('ARTICLE_ENCLOSURE_URL', $url, true);
  65. $tpl->setVariable('ARTICLE_ENCLOSURE_TYPE', $type, true);
  66. $tpl->setVariable('ARTICLE_ENCLOSURE_LENGTH', $length, true);
  67. $tpl->addBlock('enclosure');
  68. }
  69. $tpl->addBlock('entry');
  70. }
  71. $tmp = "";
  72. $tpl->addBlock('feed');
  73. $tpl->generateOutputToString($tmp);
  74. print $tmp;
  75. }
  76. function getUnread() {
  77. $login = db_escape_string($_REQUEST["login"]);
  78. $fresh = $_REQUEST["fresh"] == "1";
  79. $result = db_query($this->link, "SELECT id FROM ttrss_users WHERE login = '$login'");
  80. if (db_num_rows($result) == 1) {
  81. $uid = db_fetch_result($result, 0, "id");
  82. print getGlobalUnread($this->link, $uid);
  83. if ($fresh) {
  84. print ";";
  85. print getFeedArticles($this->link, -3, false, true, $uid);
  86. }
  87. } else {
  88. print "-1;User not found";
  89. }
  90. }
  91. function getProfiles() {
  92. $login = db_escape_string($_REQUEST["login"]);
  93. $password = db_escape_string($_REQUEST["password"]);
  94. if (authenticate_user($this->link, $login, $password)) {
  95. $result = db_query($this->link, "SELECT * FROM ttrss_settings_profiles
  96. WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
  97. print "<select style='width: 100%' name='profile'>";
  98. print "<option value='0'>" . __("Default profile") . "</option>";
  99. while ($line = db_fetch_assoc($result)) {
  100. $id = $line["id"];
  101. $title = $line["title"];
  102. print "<option value='$id'>$title</option>";
  103. }
  104. print "</select>";
  105. $_SESSION = array();
  106. }
  107. }
  108. function pubsub() {
  109. $mode = db_escape_string($_REQUEST['hub_mode']);
  110. $feed_id = (int) db_escape_string($_REQUEST['id']);
  111. $feed_url = db_escape_string($_REQUEST['hub_topic']);
  112. if (!PUBSUBHUBBUB_ENABLED) {
  113. header('HTTP/1.0 404 Not Found');
  114. echo "404 Not found";
  115. return;
  116. }
  117. // TODO: implement hub_verifytoken checking
  118. $result = db_query($this->link, "SELECT feed_url FROM ttrss_feeds
  119. WHERE id = '$feed_id'");
  120. if (db_num_rows($result) != 0) {
  121. $check_feed_url = db_fetch_result($result, 0, "feed_url");
  122. if ($check_feed_url && ($check_feed_url == $feed_url || !$feed_url)) {
  123. if ($mode == "subscribe") {
  124. db_query($this->link, "UPDATE ttrss_feeds SET pubsub_state = 2
  125. WHERE id = '$feed_id'");
  126. print $_REQUEST['hub_challenge'];
  127. return;
  128. } else if ($mode == "unsubscribe") {
  129. db_query($this->link, "UPDATE ttrss_feeds SET pubsub_state = 0
  130. WHERE id = '$feed_id'");
  131. print $_REQUEST['hub_challenge'];
  132. return;
  133. } else if (!$mode) {
  134. // Received update ping, schedule feed update.
  135. //update_rss_feed($this->link, $feed_id, true, true);
  136. db_query($this->link, "UPDATE ttrss_feeds SET
  137. last_update_started = '1970-01-01',
  138. last_updated = '1970-01-01' WHERE id = '$feed_id'");
  139. }
  140. } else {
  141. header('HTTP/1.0 404 Not Found');
  142. echo "404 Not found";
  143. }
  144. } else {
  145. header('HTTP/1.0 404 Not Found');
  146. echo "404 Not found";
  147. }
  148. }
  149. function logout() {
  150. logout_user();
  151. header("Location: index.php");
  152. }
  153. function fbexport() {
  154. $access_key = db_escape_string($_POST["key"]);
  155. // TODO: rate limit checking using last_connected
  156. $result = db_query($this->link, "SELECT id FROM ttrss_linked_instances
  157. WHERE access_key = '$access_key'");
  158. if (db_num_rows($result) == 1) {
  159. $instance_id = db_fetch_result($result, 0, "id");
  160. $result = db_query($this->link, "SELECT feed_url, site_url, title, subscribers
  161. FROM ttrss_feedbrowser_cache ORDER BY subscribers DESC LIMIT 100");
  162. $feeds = array();
  163. while ($line = db_fetch_assoc($result)) {
  164. array_push($feeds, $line);
  165. }
  166. db_query($this->link, "UPDATE ttrss_linked_instances SET
  167. last_status_in = 1 WHERE id = '$instance_id'");
  168. print json_encode(array("feeds" => $feeds));
  169. } else {
  170. print json_encode(array("error" => array("code" => 6)));
  171. }
  172. }
  173. function share() {
  174. $uuid = db_escape_string($_REQUEST["key"]);
  175. $result = db_query($this->link, "SELECT ref_id, owner_uid FROM ttrss_user_entries WHERE
  176. uuid = '$uuid'");
  177. if (db_num_rows($result) != 0) {
  178. header("Content-Type: text/html");
  179. $id = db_fetch_result($result, 0, "ref_id");
  180. $owner_uid = db_fetch_result($result, 0, "owner_uid");
  181. $article = format_article($this->link, $id, false, true, $owner_uid);
  182. print_r($article['content']);
  183. } else {
  184. print "Article not found.";
  185. }
  186. }
  187. function rss() {
  188. header("Content-Type: text/xml; charset=utf-8");
  189. $feed = db_escape_string($_REQUEST["id"]);
  190. $key = db_escape_string($_REQUEST["key"]);
  191. $is_cat = $_REQUEST["is_cat"] != false;
  192. $limit = (int)db_escape_string($_REQUEST["limit"]);
  193. $search = db_escape_string($_REQUEST["q"]);
  194. $match_on = db_escape_string($_REQUEST["m"]);
  195. $search_mode = db_escape_string($_REQUEST["smode"]);
  196. $view_mode = db_escape_string($_REQUEST["view-mode"]);
  197. if (SINGLE_USER_MODE) {
  198. authenticate_user($this->link, "admin", null);
  199. }
  200. $owner_id = false;
  201. if ($key) {
  202. $result = db_query($this->link, "SELECT owner_uid FROM
  203. ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
  204. if (db_num_rows($result) == 1)
  205. $owner_id = db_fetch_result($result, 0, "owner_uid");
  206. }
  207. if ($owner_id) {
  208. $this->generate_syndicated_feed($owner_id, $feed, $is_cat, $limit,
  209. $search, $search_mode, $match_on, $view_mode);
  210. } else {
  211. header('HTTP/1.1 403 Forbidden');
  212. }
  213. }
  214. function globalUpdateFeeds() {
  215. include "rssfuncs.php";
  216. // Update all feeds needing a update.
  217. update_daemon_common($this->link, 0, true, false);
  218. }
  219. }
  220. ?>