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