]> git.wh0rd.org - tt-rss.git/blame - classes/article.php
move add_feed_url() to pref_feeds
[tt-rss.git] / classes / article.php
CommitLineData
6afcbcd1
AD
1<?php
2class Article extends Handler_Protected {
3
4 function csrf_ignore($method) {
1c9bda91 5 $csrf_ignored = array("redirect", "editarticletags");
6afcbcd1
AD
6
7 return array_search($method, $csrf_ignored) !== false;
8 }
9
10 function redirect() {
d9c85e0f 11 $id = $this->dbh->escape_string($_REQUEST['id']);
6afcbcd1 12
d9c85e0f 13 $result = $this->dbh->query("SELECT link FROM ttrss_entries, ttrss_user_entries
6afcbcd1
AD
14 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'
15 LIMIT 1");
16
d9c85e0f
AD
17 if ($this->dbh->num_rows($result) == 1) {
18 $article_url = $this->dbh->fetch_result($result, 0, 'link');
6afcbcd1
AD
19 $article_url = str_replace("\n", "", $article_url);
20
21 header("Location: $article_url");
22 return;
23
24 } else {
25 print_error(__("Article not found."));
26 }
27 }
28
29 function view() {
d9c85e0f
AD
30 $id = $this->dbh->escape_string($_REQUEST["id"]);
31 $cids = explode(",", $this->dbh->escape_string($_REQUEST["cids"]));
32 $mode = $this->dbh->escape_string($_REQUEST["mode"]);
6afcbcd1
AD
33
34 // in prefetch mode we only output requested cids, main article
35 // just gets marked as read (it already exists in client cache)
36
37 $articles = array();
38
39 if ($mode == "") {
7e5f8d9f 40 array_push($articles, $this->format_article($id, false));
6afcbcd1 41 } else if ($mode == "zoom") {
7e5f8d9f 42 array_push($articles, $this->format_article($id, true, true));
6afcbcd1 43 } else if ($mode == "raw") {
f48f292d 44 if (isset($_REQUEST['html'])) {
6afcbcd1 45 header("Content-Type: text/html");
5bbc4bb4 46 print '<link rel="stylesheet" type="text/css" href="css/tt-rss.css"/>';
6afcbcd1
AD
47 }
48
7e5f8d9f 49 $article = $this->format_article($id, false, isset($_REQUEST["zoom"]));
6afcbcd1
AD
50 print $article['content'];
51 return;
52 }
53
a42c55f0 54 $this->catchupArticleById($id, 0);
6afcbcd1
AD
55
56 if (!$_SESSION["bw_limit"]) {
57 foreach ($cids as $cid) {
58 if ($cid) {
7e5f8d9f 59 array_push($articles, $this->format_article($cid, false, false));
6afcbcd1
AD
60 }
61 }
62 }
63
64 print json_encode($articles);
65 }
66
a42c55f0 67 private function catchupArticleById($id, $cmode) {
6afcbcd1
AD
68
69 if ($cmode == 0) {
d9c85e0f 70 $this->dbh->query("UPDATE ttrss_user_entries SET
6afcbcd1
AD
71 unread = false,last_read = NOW()
72 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
73 } else if ($cmode == 1) {
d9c85e0f 74 $this->dbh->query("UPDATE ttrss_user_entries SET
6afcbcd1
AD
75 unread = true
76 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
77 } else {
d9c85e0f 78 $this->dbh->query("UPDATE ttrss_user_entries SET
6afcbcd1
AD
79 unread = NOT unread,last_read = NOW()
80 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
81 }
82
4122da02 83 $feed_id = $this->getArticleFeed($id);
2ed0d6c4 84 CCache::update($feed_id, $_SESSION["uid"]);
6afcbcd1
AD
85 }
86
a42c55f0 87 static function create_published_article($title, $url, $content, $labels_str,
6afcbcd1
AD
88 $owner_uid) {
89
5e3d5480 90 $guid = 'SHA1:' . sha1("ttshared:" . $url . $owner_uid); // include owner_uid to prevent global GUID clash
666cd333
AD
91
92 if (!$content) {
93 $pluginhost = new PluginHost();
94 $pluginhost->load_all(PluginHost::KIND_ALL, $owner_uid);
7af2e795 95 $pluginhost->load_data();
666cd333
AD
96
97 $af_readability = $pluginhost->get_plugin("Af_Readability");
98
99 if ($af_readability) {
7af2e795 100 $enable_share_anything = $pluginhost->get($af_readability, "enable_share_anything");
666cd333 101
7af2e795
AD
102 if ($enable_share_anything) {
103 $extracted_content = $af_readability->extract_content($url);
104
105 if ($extracted_content) $content = db_escape_string($extracted_content);
106 }
666cd333
AD
107 }
108 }
109
6afcbcd1
AD
110 $content_hash = sha1($content);
111
112 if ($labels_str != "") {
113 $labels = explode(",", $labels_str);
114 } else {
115 $labels = array();
116 }
117
118 $rc = false;
119
120 if (!$title) $title = $url;
121 if (!$title && !$url) return false;
122
123 if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) return false;
124
a42c55f0 125 db_query("BEGIN");
6afcbcd1
AD
126
127 // only check for our user data here, others might have shared this with different content etc
a42c55f0 128 $result = db_query("SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
74163057 129 guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1");
6afcbcd1
AD
130
131 if (db_num_rows($result) != 0) {
132 $ref_id = db_fetch_result($result, 0, "id");
133
a42c55f0 134 $result = db_query("SELECT int_id FROM ttrss_user_entries WHERE
6afcbcd1
AD
135 ref_id = '$ref_id' AND owner_uid = '$owner_uid' LIMIT 1");
136
137 if (db_num_rows($result) != 0) {
138 $int_id = db_fetch_result($result, 0, "int_id");
139
a42c55f0 140 db_query("UPDATE ttrss_entries SET
6afcbcd1
AD
141 content = '$content', content_hash = '$content_hash' WHERE id = '$ref_id'");
142
a42c55f0 143 db_query("UPDATE ttrss_user_entries SET published = true,
d2888e88 144 last_published = NOW() WHERE
6afcbcd1
AD
145 int_id = '$int_id' AND owner_uid = '$owner_uid'");
146 } else {
147
a42c55f0 148 db_query("INSERT INTO ttrss_user_entries
d2888e88
AD
149 (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache,
150 last_read, note, unread, last_published)
6afcbcd1 151 VALUES
d2888e88 152 ('$ref_id', '', NULL, NULL, $owner_uid, true, '', '', NOW(), '', false, NOW())");
6afcbcd1
AD
153 }
154
155 if (count($labels) != 0) {
156 foreach ($labels as $label) {
7c9b5a3f 157 Labels::add_article($ref_id, trim($label), $owner_uid);
6afcbcd1
AD
158 }
159 }
160
161 $rc = true;
162
163 } else {
a42c55f0 164 $result = db_query("INSERT INTO ttrss_entries
6afcbcd1
AD
165 (title, guid, link, updated, content, content_hash, date_entered, date_updated)
166 VALUES
167 ('$title', '$guid', '$url', NOW(), '$content', '$content_hash', NOW(), NOW())");
168
a42c55f0 169 $result = db_query("SELECT id FROM ttrss_entries WHERE guid = '$guid'");
6afcbcd1
AD
170
171 if (db_num_rows($result) != 0) {
172 $ref_id = db_fetch_result($result, 0, "id");
173
a42c55f0 174 db_query("INSERT INTO ttrss_user_entries
d2888e88
AD
175 (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache,
176 last_read, note, unread, last_published)
6afcbcd1 177 VALUES
d2888e88 178 ('$ref_id', '', NULL, NULL, $owner_uid, true, '', '', NOW(), '', false, NOW())");
6afcbcd1
AD
179
180 if (count($labels) != 0) {
181 foreach ($labels as $label) {
7c9b5a3f 182 Labels::add_article($ref_id, trim($label), $owner_uid);
6afcbcd1
AD
183 }
184 }
185
186 $rc = true;
187 }
188 }
189
a42c55f0 190 db_query("COMMIT");
6afcbcd1
AD
191
192 return $rc;
193 }
194
1c9bda91
AD
195 function editArticleTags() {
196
197 print __("Tags for this article (separated by commas):")."<br>";
198
d9c85e0f 199 $param = $this->dbh->escape_string($_REQUEST['param']);
1c9bda91 200
7e5f8d9f 201 $tags = Article::get_article_tags($this->dbh->escape_string($param));
1c9bda91
AD
202
203 $tags_str = join(", ", $tags);
204
328118d1
AD
205 print_hidden("id", "$param");
206 print_hidden("op", "article");
207 print_hidden("method", "setArticleTags");
1c9bda91
AD
208
209 print "<table width='100%'><tr><td>";
210
211 print "<textarea dojoType=\"dijit.form.SimpleTextarea\" rows='4'
8b8568e9 212 style='height : 100px; font-size : 12px; width : 98%' id=\"tags_str\"
1c9bda91
AD
213 name='tags_str'>$tags_str</textarea>
214 <div class=\"autocomplete\" id=\"tags_choices\"
215 style=\"display:none\"></div>";
216
217 print "</td></tr></table>";
218
219 print "<div class='dlgButtons'>";
220
221 print "<button dojoType=\"dijit.form.Button\"
222 onclick=\"dijit.byId('editTagsDlg').execute()\">".__('Save')."</button> ";
223 print "<button dojoType=\"dijit.form.Button\"
224 onclick=\"dijit.byId('editTagsDlg').hide()\">".__('Cancel')."</button>";
225 print "</div>";
226
227 }
6afcbcd1 228
d719b062 229 function setScore() {
d9c85e0f
AD
230 $ids = $this->dbh->escape_string($_REQUEST['id']);
231 $score = (int)$this->dbh->escape_string($_REQUEST['score']);
d719b062 232
d9c85e0f 233 $this->dbh->query("UPDATE ttrss_user_entries SET
d719b062
AD
234 score = '$score' WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
235
6f7798b6 236 print json_encode(array("id" => $ids,
a72cd54c
AD
237 "score" => (int)$score,
238 "score_pic" => get_score_pic($score)));
239 }
240
241 function getScore() {
242 $id = $this->dbh->escape_string($_REQUEST['id']);
243
244 $result = $this->dbh->query("SELECT score FROM ttrss_user_entries WHERE ref_id = $id AND owner_uid = " . $_SESSION["uid"]);
245 $score = $this->dbh->fetch_result($result, 0, "score");
246
247 print json_encode(array("id" => $id,
248 "score" => (int)$score,
d719b062
AD
249 "score_pic" => get_score_pic($score)));
250 }
251
6afcbcd1 252
5df8be5c
AD
253 function setArticleTags() {
254
d9c85e0f 255 $id = $this->dbh->escape_string($_REQUEST["id"]);
5df8be5c 256
d9c85e0f 257 $tags_str = $this->dbh->escape_string($_REQUEST["tags_str"]);
5df8be5c
AD
258 $tags = array_unique(trim_array(explode(",", $tags_str)));
259
d9c85e0f 260 $this->dbh->query("BEGIN");
5df8be5c 261
d9c85e0f 262 $result = $this->dbh->query("SELECT int_id FROM ttrss_user_entries WHERE
5df8be5c
AD
263 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
264
d9c85e0f 265 if ($this->dbh->num_rows($result) == 1) {
5df8be5c
AD
266
267 $tags_to_cache = array();
268
d9c85e0f 269 $int_id = $this->dbh->fetch_result($result, 0, "int_id");
5df8be5c 270
d9c85e0f 271 $this->dbh->query("DELETE FROM ttrss_tags WHERE
5df8be5c
AD
272 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
273
274 foreach ($tags as $tag) {
275 $tag = sanitize_tag($tag);
276
277 if (!tag_is_valid($tag)) {
278 continue;
279 }
280
281 if (preg_match("/^[0-9]*$/", $tag)) {
282 continue;
283 }
284
285 // print "<!-- $id : $int_id : $tag -->";
286
287 if ($tag != '') {
d9c85e0f 288 $this->dbh->query("INSERT INTO ttrss_tags
5df8be5c
AD
289 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
290 }
291
292 array_push($tags_to_cache, $tag);
293 }
294
295 /* update tag cache */
296
297 sort($tags_to_cache);
298 $tags_str = join(",", $tags_to_cache);
299
d9c85e0f 300 $this->dbh->query("UPDATE ttrss_user_entries
5df8be5c
AD
301 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
302 AND owner_uid = " . $_SESSION["uid"]);
303 }
304
d9c85e0f 305 $this->dbh->query("COMMIT");
5df8be5c 306
7e5f8d9f
AD
307 $tags = Article::get_article_tags($id);
308 $tags_str = $this->format_tags_string($tags, $id);
5df8be5c
AD
309 $tags_str_full = join(", ", $tags);
310
311 if (!$tags_str_full) $tags_str_full = __("no tags");
312
313 print json_encode(array("id" => (int)$id,
314 "content" => $tags_str, "content_full" => $tags_str_full));
315 }
316
c83554bd
AD
317
318 function completeTags() {
d9c85e0f 319 $search = $this->dbh->escape_string($_REQUEST["search"]);
c83554bd 320
d9c85e0f 321 $result = $this->dbh->query("SELECT DISTINCT tag_name FROM ttrss_tags
c83554bd
AD
322 WHERE owner_uid = '".$_SESSION["uid"]."' AND
323 tag_name LIKE '$search%' ORDER BY tag_name
324 LIMIT 10");
325
326 print "<ul>";
d9c85e0f 327 while ($line = $this->dbh->fetch_assoc($result)) {
c83554bd
AD
328 print "<li>" . $line["tag_name"] . "</li>";
329 }
330 print "</ul>";
331 }
332
4b7726f0
AD
333 function assigntolabel() {
334 return $this->labelops(true);
335 }
336
337 function removefromlabel() {
338 return $this->labelops(false);
339 }
340
341 private function labelops($assign) {
342 $reply = array();
343
d9c85e0f
AD
344 $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
345 $label_id = $this->dbh->escape_string($_REQUEST["lid"]);
4b7726f0 346
7c9b5a3f 347 $label = $this->dbh->escape_string(Labels::find_caption($label_id,
4b7726f0
AD
348 $_SESSION["uid"]));
349
350 $reply["info-for-headlines"] = array();
351
352 if ($label) {
353
354 foreach ($ids as $id) {
355
356 if ($assign)
7c9b5a3f 357 Labels::add_article($id, $label, $_SESSION["uid"]);
4b7726f0 358 else
7c9b5a3f 359 Labels::remove_article($id, $label, $_SESSION["uid"]);
4b7726f0 360
4a0da0e5 361 $labels = $this->get_article_labels($id, $_SESSION["uid"]);
4b7726f0
AD
362
363 array_push($reply["info-for-headlines"],
7e5f8d9f 364 array("id" => $id, "labels" => $this->format_article_labels($labels)));
4b7726f0
AD
365
366 }
367 }
368
369 $reply["message"] = "UPDATE_COUNTERS";
370
371 print json_encode($reply);
372 }
373
4122da02
AD
374 function getArticleFeed($id) {
375 $result = db_query("SELECT feed_id FROM ttrss_user_entries
376 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
377
378 if (db_num_rows($result) != 0) {
379 return db_fetch_result($result, 0, "feed_id");
380 } else {
381 return 0;
382 }
383 }
4b7726f0 384
7e5f8d9f
AD
385 static function format_article_enclosures($id, $always_display_enclosures,
386 $article_content, $hide_images = false) {
387
388 $result = Article::get_article_enclosures($id);
389 $rv = '';
390
391 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FORMAT_ENCLOSURES) as $plugin) {
392 $retval = $plugin->hook_format_enclosures($rv, $result, $id, $always_display_enclosures, $article_content, $hide_images);
393 if (is_array($retval)) {
394 $rv = $retval[0];
395 $result = $retval[1];
396 } else {
397 $rv = $retval;
398 }
399 }
400 unset($retval); // Unset to prevent breaking render if there are no HOOK_RENDER_ENCLOSURE hooks below.
401
402 if ($rv === '' && !empty($result)) {
403 $entries_html = array();
404 $entries = array();
405 $entries_inline = array();
406
407 foreach ($result as $line) {
408
409 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ENCLOSURE_ENTRY) as $plugin) {
410 $line = $plugin->hook_enclosure_entry($line);
411 }
412
413 $url = $line["content_url"];
414 $ctype = $line["content_type"];
415 $title = $line["title"];
416 $width = $line["width"];
417 $height = $line["height"];
418
419 if (!$ctype) $ctype = __("unknown type");
420
421 //$filename = substr($url, strrpos($url, "/")+1);
422 $filename = basename($url);
423
424 $player = format_inline_player($url, $ctype);
425
426 if ($player) array_push($entries_inline, $player);
427
428# $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\" rel=\"noopener noreferrer\">" .
429# $filename . " (" . $ctype . ")" . "</a>";
430
431 $entry = "<div onclick=\"openUrlPopup('".htmlspecialchars($url)."')\"
432 dojoType=\"dijit.MenuItem\">$filename ($ctype)</div>";
433
434 array_push($entries_html, $entry);
435
436 $entry = array();
437
438 $entry["type"] = $ctype;
439 $entry["filename"] = $filename;
440 $entry["url"] = $url;
441 $entry["title"] = $title;
442 $entry["width"] = $width;
443 $entry["height"] = $height;
444
445 array_push($entries, $entry);
446 }
447
448 if ($_SESSION['uid'] && !get_pref("STRIP_IMAGES") && !$_SESSION["bw_limit"]) {
449 if ($always_display_enclosures ||
450 !preg_match("/<img/i", $article_content)) {
451
452 foreach ($entries as $entry) {
453
454 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ENCLOSURE) as $plugin)
455 $retval = $plugin->hook_render_enclosure($entry, $hide_images);
456
457
458 if ($retval) {
459 $rv .= $retval;
460 } else {
461
462 if (preg_match("/image/", $entry["type"])) {
463
464 if (!$hide_images) {
465 $encsize = '';
466 if ($entry['height'] > 0)
467 $encsize .= ' height="' . intval($entry['height']) . '"';
468 if ($entry['width'] > 0)
469 $encsize .= ' width="' . intval($entry['width']) . '"';
470 $rv .= "<p><img
471 alt=\"".htmlspecialchars($entry["filename"])."\"
472 src=\"" .htmlspecialchars($entry["url"]) . "\"
473 " . $encsize . " /></p>";
474 } else {
475 $rv .= "<p><a target=\"_blank\" rel=\"noopener noreferrer\"
476 href=\"".htmlspecialchars($entry["url"])."\"
477 >" .htmlspecialchars($entry["url"]) . "</a></p>";
478 }
479
480 if ($entry['title']) {
481 $rv.= "<div class=\"enclosure_title\">${entry['title']}</div>";
482 }
483 }
484 }
485 }
486 }
487 }
488
489 if (count($entries_inline) > 0) {
490 $rv .= "<hr clear='both'/>";
491 foreach ($entries_inline as $entry) { $rv .= $entry; };
492 $rv .= "<hr clear='both'/>";
493 }
494
495 $rv .= "<div class=\"attachments\" dojoType=\"dijit.form.DropDownButton\">".
496 "<span>" . __('Attachments')."</span>";
497
498 $rv .= "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
499
500 foreach ($entries as $entry) {
501 if ($entry["title"])
502 $title = " &mdash; " . truncate_string($entry["title"], 30);
503 else
504 $title = "";
505
506 if ($entry["filename"])
507 $filename = truncate_middle(htmlspecialchars($entry["filename"]), 60);
508 else
509 $filename = "";
510
511 $rv .= "<div onclick='openUrlPopup(\"".htmlspecialchars($entry["url"])."\")'
512 dojoType=\"dijit.MenuItem\">".$filename . $title."</div>";
513
514 };
515
516 $rv .= "</div>";
517 $rv .= "</div>";
518 }
519
520 return $rv;
521 }
522
523 static function format_article($id, $mark_as_read = true, $zoom_mode = false, $owner_uid = false) {
524 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
525
526 $rv = array();
527
528 $rv['id'] = $id;
529
530 /* we can figure out feed_id from article id anyway, why do we
531 * pass feed_id here? let's ignore the argument :(*/
532
533 $result = db_query("SELECT feed_id FROM ttrss_user_entries
534 WHERE ref_id = '$id'");
535
536 $feed_id = (int) db_fetch_result($result, 0, "feed_id");
537
538 $rv['feed_id'] = $feed_id;
539
540 //if (!$zoom_mode) { print "<article id='$id'><![CDATA["; };
541
542 if ($mark_as_read) {
543 $result = db_query("UPDATE ttrss_user_entries
544 SET unread = false,last_read = NOW()
545 WHERE ref_id = '$id' AND owner_uid = $owner_uid");
546
2ed0d6c4 547 CCache::update($feed_id, $owner_uid);
7e5f8d9f
AD
548 }
549
550 $result = db_query("SELECT id,title,link,content,feed_id,comments,int_id,lang,
551 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
552 (SELECT site_url FROM ttrss_feeds WHERE id = feed_id) as site_url,
553 (SELECT title FROM ttrss_feeds WHERE id = feed_id) as feed_title,
554 (SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) as hide_images,
555 (SELECT always_display_enclosures FROM ttrss_feeds WHERE id = feed_id) as always_display_enclosures,
556 num_comments,
557 tag_cache,
558 author,
559 guid,
560 orig_feed_id,
561 note
562 FROM ttrss_entries,ttrss_user_entries
563 WHERE id = '$id' AND ref_id = id AND owner_uid = $owner_uid");
564
565 if ($result) {
566
567 $line = db_fetch_assoc($result);
568
569 $line["tags"] = Article::get_article_tags($id, $owner_uid, $line["tag_cache"]);
570 unset($line["tag_cache"]);
571
572 $line["content"] = sanitize($line["content"],
573 sql_bool_to_bool($line['hide_images']),
574 $owner_uid, $line["site_url"], false, $line["id"]);
575
576 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE) as $p) {
577 $line = $p->hook_render_article($line);
578 }
579
580 $num_comments = (int) $line["num_comments"];
581 $entry_comments = "";
582
583 if ($num_comments > 0) {
584 if ($line["comments"]) {
585 $comments_url = htmlspecialchars($line["comments"]);
586 } else {
587 $comments_url = htmlspecialchars($line["link"]);
588 }
589 $entry_comments = "<a class=\"postComments\"
590 target='_blank' rel=\"noopener noreferrer\" href=\"$comments_url\">$num_comments ".
591 _ngettext("comment", "comments", $num_comments)."</a>";
592
593 } else {
594 if ($line["comments"] && $line["link"] != $line["comments"]) {
595 $entry_comments = "<a class=\"postComments\" target='_blank' rel=\"noopener noreferrer\" href=\"".htmlspecialchars($line["comments"])."\">".__("comments")."</a>";
596 }
597 }
598
599 if ($zoom_mode) {
600 header("Content-Type: text/html");
601 $rv['content'] .= "<html><head>
602 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
603 <title>Tiny Tiny RSS - ".$line["title"]."</title>".
604 stylesheet_tag("css/tt-rss.css").
605 stylesheet_tag("css/zoom.css").
606 stylesheet_tag("css/dijit.css")."
607
608 <link rel=\"shortcut icon\" type=\"image/png\" href=\"images/favicon.png\">
609 <link rel=\"icon\" type=\"image/png\" sizes=\"72x72\" href=\"images/favicon-72px.png\">
610
611 </head><body id=\"ttrssZoom\">";
612 }
613
614 $rv['content'] .= "<div class=\"postReply\" id=\"POST-$id\">";
615
616 $rv['content'] .= "<div class=\"postHeader\" id=\"POSTHDR-$id\">";
617
618 $entry_author = $line["author"];
619
620 if ($entry_author) {
621 $entry_author = __(" - ") . $entry_author;
622 }
623
624 $parsed_updated = make_local_datetime($line["updated"], true,
625 $owner_uid, true);
626
627 if (!$zoom_mode)
628 $rv['content'] .= "<div class=\"postDate\">$parsed_updated</div>";
629
630 if ($line["link"]) {
631 $rv['content'] .= "<div class='postTitle'><a target='_blank' rel='noopener noreferrer'
632 title=\"".htmlspecialchars($line['title'])."\"
633 href=\"" .
634 htmlspecialchars($line["link"]) . "\">" .
635 $line["title"] . "</a>" .
636 "<span class='author'>$entry_author</span></div>";
637 } else {
638 $rv['content'] .= "<div class='postTitle'>" . $line["title"] . "$entry_author</div>";
639 }
640
641 if ($zoom_mode) {
642 $feed_title = htmlspecialchars($line["feed_title"]);
643
644 $rv['content'] .= "<div class=\"postFeedTitle\">$feed_title</div>";
645
646 $rv['content'] .= "<div class=\"postDate\">$parsed_updated</div>";
647 }
648
649 $tags_str = Article::format_tags_string($line["tags"], $id);
650 $tags_str_full = join(", ", $line["tags"]);
651
652 if (!$tags_str_full) $tags_str_full = __("no tags");
653
654 if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
655
656 $rv['content'] .= "<div class='postTags' style='float : right'>
657 <img src='images/tag.png'
658 class='tagsPic' alt='Tags' title='Tags'>&nbsp;";
659
660 if (!$zoom_mode) {
661 $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>
662 <a title=\"".__('Edit tags for this article')."\"
663 href=\"#\" onclick=\"editArticleTags($id, $feed_id)\">(+)</a>";
664
665 $rv['content'] .= "<div dojoType=\"dijit.Tooltip\"
666 id=\"ATSTRTIP-$id\" connectId=\"ATSTR-$id\"
667 position=\"below\">$tags_str_full</div>";
668
669 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_BUTTON) as $p) {
670 $rv['content'] .= $p->hook_article_button($line);
671 }
672
673 } else {
674 $tags_str = strip_tags($tags_str);
675 $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>";
676 }
677 $rv['content'] .= "</div>";
678 $rv['content'] .= "<div clear='both'>";
679
680 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_LEFT_BUTTON) as $p) {
681 $rv['content'] .= $p->hook_article_left_button($line);
682 }
683
684 $rv['content'] .= "$entry_comments</div>";
685
686 if ($line["orig_feed_id"]) {
687
688 $tmp_result = db_query("SELECT * FROM ttrss_archived_feeds
689 WHERE id = ".$line["orig_feed_id"] . " AND owner_uid = " . $_SESSION["uid"]);
690
691 if (db_num_rows($tmp_result) != 0) {
692
693 $rv['content'] .= "<div clear='both'>";
694 $rv['content'] .= __("Originally from:");
695
696 $rv['content'] .= "&nbsp;";
697
698 $tmp_line = db_fetch_assoc($tmp_result);
699
700 $rv['content'] .= "<a target='_blank' rel='noopener noreferrer'
701 href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
702 $tmp_line['title'] . "</a>";
703
704 $rv['content'] .= "&nbsp;";
705
706 $rv['content'] .= "<a target='_blank' rel='noopener noreferrer' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
707 $rv['content'] .= "<img title='".__('Feed URL')."' class='tinyFeedIcon' src='images/pub_set.png'></a>";
708
709 $rv['content'] .= "</div>";
710 }
711 }
712
713 $rv['content'] .= "</div>";
714
715 $rv['content'] .= "<div id=\"POSTNOTE-$id\">";
716 if ($line['note']) {
717 $rv['content'] .= Article::format_article_note($id, $line['note'], !$zoom_mode);
718 }
719 $rv['content'] .= "</div>";
720
721 if (!$line['lang']) $line['lang'] = 'en';
722
723 $rv['content'] .= "<div class=\"postContent\" lang=\"".$line['lang']."\">";
724
725 $rv['content'] .= $line["content"];
726
727 if (!$zoom_mode) {
728 $rv['content'] .= Article::format_article_enclosures($id,
729 sql_bool_to_bool($line["always_display_enclosures"]),
730 $line["content"],
731 sql_bool_to_bool($line["hide_images"]));
732 }
733
734 $rv['content'] .= "</div>";
735
736 $rv['content'] .= "</div>";
737
738 }
739
740 if ($zoom_mode) {
741 $rv['content'] .= "
742 <div class='footer'>
743 <button onclick=\"return window.close()\">".
744 __("Close this window")."</button></div>";
745 $rv['content'] .= "</body></html>";
746 }
747
748 return $rv;
749
750 }
751
752 static function get_article_tags($id, $owner_uid = 0, $tag_cache = false) {
753
754 $a_id = db_escape_string($id);
755
756 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
757
758 $query = "SELECT DISTINCT tag_name,
759 owner_uid as owner FROM
760 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
761 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name";
762
763 $tags = array();
764
765 /* check cache first */
766
767 if ($tag_cache === false) {
768 $result = db_query("SELECT tag_cache FROM ttrss_user_entries
769 WHERE ref_id = '$id' AND owner_uid = $owner_uid");
770
771 if (db_num_rows($result) != 0)
772 $tag_cache = db_fetch_result($result, 0, "tag_cache");
773 }
774
775 if ($tag_cache) {
776 $tags = explode(",", $tag_cache);
777 } else {
778
779 /* do it the hard way */
780
781 $tmp_result = db_query($query);
782
783 while ($tmp_line = db_fetch_assoc($tmp_result)) {
784 array_push($tags, $tmp_line["tag_name"]);
785 }
786
787 /* update the cache */
788
789 $tags_str = db_escape_string(join(",", $tags));
790
791 db_query("UPDATE ttrss_user_entries
792 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
793 AND owner_uid = $owner_uid");
794 }
795
796 return $tags;
797 }
798
799 static function format_tags_string($tags) {
800 if (!is_array($tags) || count($tags) == 0) {
801 return __("no tags");
802 } else {
803 $maxtags = min(5, count($tags));
804 $tags_str = "";
805
806 for ($i = 0; $i < $maxtags; $i++) {
807 $tags_str .= "<a class=\"tag\" href=\"#\" onclick=\"viewfeed({feed:'".$tags[$i]."'})\">" . $tags[$i] . "</a>, ";
808 }
809
810 $tags_str = mb_substr($tags_str, 0, mb_strlen($tags_str)-2);
811
812 if (count($tags) > $maxtags)
813 $tags_str .= ", &hellip;";
814
815 return $tags_str;
816 }
817 }
818
819 static function format_article_labels($labels) {
820
821 if (!is_array($labels)) return '';
822
823 $labels_str = "";
824
825 foreach ($labels as $l) {
826 $labels_str .= sprintf("<span class='hlLabelRef'
827 style='color : %s; background-color : %s'>%s</span>",
828 $l[2], $l[3], $l[1]);
829 }
830
831 return $labels_str;
832
833 }
834
835 static function format_article_note($id, $note, $allow_edit = true) {
836
837 $str = "<div class='articleNote' onclick=\"editArticleNote($id)\">
838 <div class='noteEdit' onclick=\"editArticleNote($id)\">".
839 ($allow_edit ? __('(edit note)') : "")."</div>$note</div>";
840
841 return $str;
842 }
843
844 static function get_article_enclosures($id) {
845
846 $query = "SELECT * FROM ttrss_enclosures
847 WHERE post_id = '$id' AND content_url != ''";
848
849 $rv = array();
850
851 $result = db_query($query);
852
853 if (db_num_rows($result) > 0) {
854 while ($line = db_fetch_assoc($result)) {
855
856 if (file_exists(CACHE_DIR . '/images/' . sha1($line["content_url"]))) {
857 $line["content_url"] = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($line["content_url"]);
858 }
859
860 array_push($rv, $line);
861 }
862 }
863
864 return $rv;
865 }
c83554bd 866
a230bf88
AD
867 static function purge_orphans($do_output = false) {
868
869 // purge orphaned posts in main content table
870 $result = db_query("DELETE FROM ttrss_entries WHERE
871 NOT EXISTS (SELECT ref_id FROM ttrss_user_entries WHERE ref_id = id)");
872
873 if ($do_output) {
874 $rows = db_affected_rows($result);
875 _debug("Purged $rows orphaned posts.");
876 }
877 }
878
aeb1abed
AD
879 static function catchupArticlesById($ids, $cmode, $owner_uid = false) {
880
881 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
882 if (count($ids) == 0) return;
883
884 $tmp_ids = array();
885
886 foreach ($ids as $id) {
887 array_push($tmp_ids, "ref_id = '$id'");
888 }
889
890 $ids_qpart = join(" OR ", $tmp_ids);
891
892 if ($cmode == 0) {
893 db_query("UPDATE ttrss_user_entries SET
894 unread = false,last_read = NOW()
895 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
896 } else if ($cmode == 1) {
897 db_query("UPDATE ttrss_user_entries SET
898 unread = true
899 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
900 } else {
901 db_query("UPDATE ttrss_user_entries SET
902 unread = NOT unread,last_read = NOW()
903 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
904 }
905
906 /* update ccache */
907
908 $result = db_query("SELECT DISTINCT feed_id FROM ttrss_user_entries
909 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
910
911 while ($line = db_fetch_assoc($result)) {
2ed0d6c4 912 CCache::update($line["feed_id"], $owner_uid);
aeb1abed
AD
913 }
914 }
915
916 static function getLastArticleId() {
917 $result = db_query("SELECT ref_id AS id FROM ttrss_user_entries
918 WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY ref_id DESC LIMIT 1");
919
920 if (db_num_rows($result) == 1) {
921 return db_fetch_result($result, 0, "id");
922 } else {
923 return -1;
924 }
925 }
a230bf88 926
4a0da0e5
AD
927 static function get_article_labels($id, $owner_uid = false) {
928 $rv = array();
929
930 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
931
932 $result = db_query("SELECT label_cache FROM
933 ttrss_user_entries WHERE ref_id = '$id' AND owner_uid = " .
934 $owner_uid);
935
936 if (db_num_rows($result) > 0) {
937 $label_cache = db_fetch_result($result, 0, "label_cache");
938
939 if ($label_cache) {
940 $label_cache = json_decode($label_cache, true);
941
942 if ($label_cache["no-labels"] == 1)
943 return $rv;
944 else
945 return $label_cache;
946 }
947 }
948
949 $result = db_query(
950 "SELECT DISTINCT label_id,caption,fg_color,bg_color
951 FROM ttrss_labels2, ttrss_user_labels2
952 WHERE id = label_id
953 AND article_id = '$id'
954 AND owner_uid = ". $owner_uid . "
955 ORDER BY caption");
956
957 while ($line = db_fetch_assoc($result)) {
7c9b5a3f 958 $rk = array(Labels::label_to_feed_id($line["label_id"]),
4a0da0e5
AD
959 $line["caption"], $line["fg_color"],
960 $line["bg_color"]);
961 array_push($rv, $rk);
962 }
963
964 if (count($rv) > 0)
7c9b5a3f 965 Labels::update_cache($owner_uid, $id, $rv);
4a0da0e5 966 else
7c9b5a3f 967 Labels::update_cache($owner_uid, $id, array("no-labels" => 1));
4a0da0e5
AD
968
969 return $rv;
970 }
971
6afcbcd1 972}