article.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. class Article extends Handler_Protected {
  3. function csrf_ignore($method) {
  4. $csrf_ignored = array("redirect");
  5. return array_search($method, $csrf_ignored) !== false;
  6. }
  7. function redirect() {
  8. $id = db_escape_string($this->link, $_REQUEST['id']);
  9. $result = db_query($this->link, "SELECT link FROM ttrss_entries, ttrss_user_entries
  10. WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'
  11. LIMIT 1");
  12. if (db_num_rows($result) == 1) {
  13. $article_url = db_fetch_result($result, 0, 'link');
  14. $article_url = str_replace("\n", "", $article_url);
  15. header("Location: $article_url");
  16. return;
  17. } else {
  18. print_error(__("Article not found."));
  19. }
  20. }
  21. function view() {
  22. $id = db_escape_string($this->link, $_REQUEST["id"]);
  23. $cids = explode(",", db_escape_string($this->link, $_REQUEST["cids"]));
  24. $mode = db_escape_string($this->link, $_REQUEST["mode"]);
  25. $omode = db_escape_string($this->link, $_REQUEST["omode"]);
  26. // in prefetch mode we only output requested cids, main article
  27. // just gets marked as read (it already exists in client cache)
  28. $articles = array();
  29. if ($mode == "") {
  30. array_push($articles, format_article($this->link, $id, false));
  31. } else if ($mode == "zoom") {
  32. array_push($articles, format_article($this->link, $id, true, true));
  33. } else if ($mode == "raw") {
  34. if ($_REQUEST['html']) {
  35. header("Content-Type: text/html");
  36. print '<link rel="stylesheet" type="text/css" href="tt-rss.css"/>';
  37. }
  38. $article = format_article($this->link, $id, false);
  39. print $article['content'];
  40. return;
  41. }
  42. $this->catchupArticleById($this->link, $id, 0);
  43. if (!$_SESSION["bw_limit"]) {
  44. foreach ($cids as $cid) {
  45. if ($cid) {
  46. array_push($articles, format_article($this->link, $cid, false, false));
  47. }
  48. }
  49. }
  50. print json_encode($articles);
  51. }
  52. private function catchupArticleById($link, $id, $cmode) {
  53. if ($cmode == 0) {
  54. db_query($link, "UPDATE ttrss_user_entries SET
  55. unread = false,last_read = NOW()
  56. WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
  57. } else if ($cmode == 1) {
  58. db_query($link, "UPDATE ttrss_user_entries SET
  59. unread = true
  60. WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
  61. } else {
  62. db_query($link, "UPDATE ttrss_user_entries SET
  63. unread = NOT unread,last_read = NOW()
  64. WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
  65. }
  66. $feed_id = getArticleFeed($link, $id);
  67. ccache_update($link, $feed_id, $_SESSION["uid"]);
  68. }
  69. static function create_published_article($link, $title, $url, $content, $labels_str,
  70. $owner_uid) {
  71. $guid = sha1($url . $owner_uid); // include owner_uid to prevent global GUID clash
  72. $content_hash = sha1($content);
  73. if ($labels_str != "") {
  74. $labels = explode(",", $labels_str);
  75. } else {
  76. $labels = array();
  77. }
  78. $rc = false;
  79. if (!$title) $title = $url;
  80. if (!$title && !$url) return false;
  81. if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) return false;
  82. db_query($link, "BEGIN");
  83. // only check for our user data here, others might have shared this with different content etc
  84. $result = db_query($link, "SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
  85. link = '$url' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1");
  86. if (db_num_rows($result) != 0) {
  87. $ref_id = db_fetch_result($result, 0, "id");
  88. $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
  89. ref_id = '$ref_id' AND owner_uid = '$owner_uid' LIMIT 1");
  90. if (db_num_rows($result) != 0) {
  91. $int_id = db_fetch_result($result, 0, "int_id");
  92. db_query($link, "UPDATE ttrss_entries SET
  93. content = '$content', content_hash = '$content_hash' WHERE id = '$ref_id'");
  94. db_query($link, "UPDATE ttrss_user_entries SET published = true WHERE
  95. int_id = '$int_id' AND owner_uid = '$owner_uid'");
  96. } else {
  97. db_query($link, "INSERT INTO ttrss_user_entries
  98. (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache, last_read, note, unread)
  99. VALUES
  100. ('$ref_id', '', NULL, NULL, $owner_uid, true, '', '', NOW(), '', false)");
  101. }
  102. if (count($labels) != 0) {
  103. foreach ($labels as $label) {
  104. label_add_article($link, $ref_id, trim($label), $owner_uid);
  105. }
  106. }
  107. $rc = true;
  108. } else {
  109. $result = db_query($link, "INSERT INTO ttrss_entries
  110. (title, guid, link, updated, content, content_hash, date_entered, date_updated)
  111. VALUES
  112. ('$title', '$guid', '$url', NOW(), '$content', '$content_hash', NOW(), NOW())");
  113. $result = db_query($link, "SELECT id FROM ttrss_entries WHERE guid = '$guid'");
  114. if (db_num_rows($result) != 0) {
  115. $ref_id = db_fetch_result($result, 0, "id");
  116. db_query($link, "INSERT INTO ttrss_user_entries
  117. (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache, last_read, note, unread)
  118. VALUES
  119. ('$ref_id', '', NULL, NULL, $owner_uid, true, '', '', NOW(), '', false)");
  120. if (count($labels) != 0) {
  121. foreach ($labels as $label) {
  122. label_add_article($link, $ref_id, trim($label), $owner_uid);
  123. }
  124. }
  125. $rc = true;
  126. }
  127. }
  128. db_query($link, "COMMIT");
  129. return $rc;
  130. }
  131. }