]>
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() { | |
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"); |
9dd336a2 | 46 | print '<link rel="stylesheet" type="text/css" href="css/default.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 = " — " . 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\"/> | |
8f0a59f3 | 603 | <title>".$line["title"]."</title>". |
9dd336a2 | 604 | stylesheet_tag("css/default.css")." |
7e5f8d9f AD |
605 | |
606 | <link rel=\"shortcut icon\" type=\"image/png\" href=\"images/favicon.png\"> | |
607 | <link rel=\"icon\" type=\"image/png\" sizes=\"72x72\" href=\"images/favicon-72px.png\"> | |
608 | ||
609 | </head><body id=\"ttrssZoom\">"; | |
610 | } | |
611 | ||
612 | $rv['content'] .= "<div class=\"postReply\" id=\"POST-$id\">"; | |
613 | ||
614 | $rv['content'] .= "<div class=\"postHeader\" id=\"POSTHDR-$id\">"; | |
615 | ||
616 | $entry_author = $line["author"]; | |
617 | ||
618 | if ($entry_author) { | |
619 | $entry_author = __(" - ") . $entry_author; | |
620 | } | |
621 | ||
622 | $parsed_updated = make_local_datetime($line["updated"], true, | |
623 | $owner_uid, true); | |
624 | ||
625 | if (!$zoom_mode) | |
626 | $rv['content'] .= "<div class=\"postDate\">$parsed_updated</div>"; | |
627 | ||
628 | if ($line["link"]) { | |
629 | $rv['content'] .= "<div class='postTitle'><a target='_blank' rel='noopener noreferrer' | |
630 | title=\"".htmlspecialchars($line['title'])."\" | |
631 | href=\"" . | |
632 | htmlspecialchars($line["link"]) . "\">" . | |
633 | $line["title"] . "</a>" . | |
634 | "<span class='author'>$entry_author</span></div>"; | |
635 | } else { | |
636 | $rv['content'] .= "<div class='postTitle'>" . $line["title"] . "$entry_author</div>"; | |
637 | } | |
638 | ||
639 | if ($zoom_mode) { | |
640 | $feed_title = htmlspecialchars($line["feed_title"]); | |
641 | ||
642 | $rv['content'] .= "<div class=\"postFeedTitle\">$feed_title</div>"; | |
643 | ||
644 | $rv['content'] .= "<div class=\"postDate\">$parsed_updated</div>"; | |
645 | } | |
646 | ||
647 | $tags_str = Article::format_tags_string($line["tags"], $id); | |
648 | $tags_str_full = join(", ", $line["tags"]); | |
649 | ||
650 | if (!$tags_str_full) $tags_str_full = __("no tags"); | |
651 | ||
652 | if (!$entry_comments) $entry_comments = " "; # placeholder | |
653 | ||
654 | $rv['content'] .= "<div class='postTags' style='float : right'> | |
655 | <img src='images/tag.png' | |
656 | class='tagsPic' alt='Tags' title='Tags'> "; | |
657 | ||
658 | if (!$zoom_mode) { | |
659 | $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span> | |
660 | <a title=\"".__('Edit tags for this article')."\" | |
661 | href=\"#\" onclick=\"editArticleTags($id, $feed_id)\">(+)</a>"; | |
662 | ||
663 | $rv['content'] .= "<div dojoType=\"dijit.Tooltip\" | |
664 | id=\"ATSTRTIP-$id\" connectId=\"ATSTR-$id\" | |
665 | position=\"below\">$tags_str_full</div>"; | |
666 | ||
667 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_BUTTON) as $p) { | |
668 | $rv['content'] .= $p->hook_article_button($line); | |
669 | } | |
670 | ||
671 | } else { | |
672 | $tags_str = strip_tags($tags_str); | |
673 | $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>"; | |
674 | } | |
675 | $rv['content'] .= "</div>"; | |
676 | $rv['content'] .= "<div clear='both'>"; | |
677 | ||
678 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_LEFT_BUTTON) as $p) { | |
679 | $rv['content'] .= $p->hook_article_left_button($line); | |
680 | } | |
681 | ||
682 | $rv['content'] .= "$entry_comments</div>"; | |
683 | ||
684 | if ($line["orig_feed_id"]) { | |
685 | ||
686 | $tmp_result = db_query("SELECT * FROM ttrss_archived_feeds | |
687 | WHERE id = ".$line["orig_feed_id"] . " AND owner_uid = " . $_SESSION["uid"]); | |
688 | ||
689 | if (db_num_rows($tmp_result) != 0) { | |
690 | ||
691 | $rv['content'] .= "<div clear='both'>"; | |
692 | $rv['content'] .= __("Originally from:"); | |
693 | ||
694 | $rv['content'] .= " "; | |
695 | ||
696 | $tmp_line = db_fetch_assoc($tmp_result); | |
697 | ||
698 | $rv['content'] .= "<a target='_blank' rel='noopener noreferrer' | |
699 | href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" . | |
700 | $tmp_line['title'] . "</a>"; | |
701 | ||
702 | $rv['content'] .= " "; | |
703 | ||
704 | $rv['content'] .= "<a target='_blank' rel='noopener noreferrer' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>"; | |
705 | $rv['content'] .= "<img title='".__('Feed URL')."' class='tinyFeedIcon' src='images/pub_set.png'></a>"; | |
706 | ||
707 | $rv['content'] .= "</div>"; | |
708 | } | |
709 | } | |
710 | ||
711 | $rv['content'] .= "</div>"; | |
712 | ||
713 | $rv['content'] .= "<div id=\"POSTNOTE-$id\">"; | |
714 | if ($line['note']) { | |
715 | $rv['content'] .= Article::format_article_note($id, $line['note'], !$zoom_mode); | |
716 | } | |
717 | $rv['content'] .= "</div>"; | |
718 | ||
719 | if (!$line['lang']) $line['lang'] = 'en'; | |
720 | ||
721 | $rv['content'] .= "<div class=\"postContent\" lang=\"".$line['lang']."\">"; | |
722 | ||
723 | $rv['content'] .= $line["content"]; | |
724 | ||
725 | if (!$zoom_mode) { | |
726 | $rv['content'] .= Article::format_article_enclosures($id, | |
727 | sql_bool_to_bool($line["always_display_enclosures"]), | |
728 | $line["content"], | |
729 | sql_bool_to_bool($line["hide_images"])); | |
730 | } | |
731 | ||
732 | $rv['content'] .= "</div>"; | |
733 | ||
734 | $rv['content'] .= "</div>"; | |
735 | ||
736 | } | |
737 | ||
738 | if ($zoom_mode) { | |
739 | $rv['content'] .= " | |
740 | <div class='footer'> | |
741 | <button onclick=\"return window.close()\">". | |
742 | __("Close this window")."</button></div>"; | |
743 | $rv['content'] .= "</body></html>"; | |
744 | } | |
745 | ||
e50a6479 AD |
746 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FORMAT_ARTICLE) as $p) { |
747 | $rv['content'] = $p->hook_format_article($rv['content'], $line, $zoom_mode); | |
748 | } | |
749 | ||
7e5f8d9f AD |
750 | return $rv; |
751 | ||
752 | } | |
753 | ||
754 | static function get_article_tags($id, $owner_uid = 0, $tag_cache = false) { | |
755 | ||
756 | $a_id = db_escape_string($id); | |
757 | ||
758 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
759 | ||
760 | $query = "SELECT DISTINCT tag_name, | |
761 | owner_uid as owner FROM | |
762 | ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE | |
763 | ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name"; | |
764 | ||
765 | $tags = array(); | |
766 | ||
767 | /* check cache first */ | |
768 | ||
769 | if ($tag_cache === false) { | |
770 | $result = db_query("SELECT tag_cache FROM ttrss_user_entries | |
771 | WHERE ref_id = '$id' AND owner_uid = $owner_uid"); | |
772 | ||
773 | if (db_num_rows($result) != 0) | |
774 | $tag_cache = db_fetch_result($result, 0, "tag_cache"); | |
775 | } | |
776 | ||
777 | if ($tag_cache) { | |
778 | $tags = explode(",", $tag_cache); | |
779 | } else { | |
780 | ||
781 | /* do it the hard way */ | |
782 | ||
783 | $tmp_result = db_query($query); | |
784 | ||
785 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
786 | array_push($tags, $tmp_line["tag_name"]); | |
787 | } | |
788 | ||
789 | /* update the cache */ | |
790 | ||
791 | $tags_str = db_escape_string(join(",", $tags)); | |
792 | ||
793 | db_query("UPDATE ttrss_user_entries | |
794 | SET tag_cache = '$tags_str' WHERE ref_id = '$id' | |
795 | AND owner_uid = $owner_uid"); | |
796 | } | |
797 | ||
798 | return $tags; | |
799 | } | |
800 | ||
801 | static function format_tags_string($tags) { | |
802 | if (!is_array($tags) || count($tags) == 0) { | |
803 | return __("no tags"); | |
804 | } else { | |
805 | $maxtags = min(5, count($tags)); | |
806 | $tags_str = ""; | |
807 | ||
808 | for ($i = 0; $i < $maxtags; $i++) { | |
809 | $tags_str .= "<a class=\"tag\" href=\"#\" onclick=\"viewfeed({feed:'".$tags[$i]."'})\">" . $tags[$i] . "</a>, "; | |
810 | } | |
811 | ||
812 | $tags_str = mb_substr($tags_str, 0, mb_strlen($tags_str)-2); | |
813 | ||
814 | if (count($tags) > $maxtags) | |
815 | $tags_str .= ", …"; | |
816 | ||
817 | return $tags_str; | |
818 | } | |
819 | } | |
820 | ||
821 | static function format_article_labels($labels) { | |
822 | ||
823 | if (!is_array($labels)) return ''; | |
824 | ||
825 | $labels_str = ""; | |
826 | ||
827 | foreach ($labels as $l) { | |
828 | $labels_str .= sprintf("<span class='hlLabelRef' | |
829 | style='color : %s; background-color : %s'>%s</span>", | |
830 | $l[2], $l[3], $l[1]); | |
831 | } | |
832 | ||
833 | return $labels_str; | |
834 | ||
835 | } | |
836 | ||
837 | static function format_article_note($id, $note, $allow_edit = true) { | |
838 | ||
839 | $str = "<div class='articleNote' onclick=\"editArticleNote($id)\"> | |
840 | <div class='noteEdit' onclick=\"editArticleNote($id)\">". | |
841 | ($allow_edit ? __('(edit note)') : "")."</div>$note</div>"; | |
842 | ||
843 | return $str; | |
844 | } | |
845 | ||
846 | static function get_article_enclosures($id) { | |
847 | ||
848 | $query = "SELECT * FROM ttrss_enclosures | |
849 | WHERE post_id = '$id' AND content_url != ''"; | |
850 | ||
851 | $rv = array(); | |
852 | ||
853 | $result = db_query($query); | |
854 | ||
855 | if (db_num_rows($result) > 0) { | |
856 | while ($line = db_fetch_assoc($result)) { | |
857 | ||
858 | if (file_exists(CACHE_DIR . '/images/' . sha1($line["content_url"]))) { | |
859 | $line["content_url"] = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($line["content_url"]); | |
860 | } | |
861 | ||
862 | array_push($rv, $line); | |
863 | } | |
864 | } | |
865 | ||
866 | return $rv; | |
867 | } | |
c83554bd | 868 | |
a230bf88 AD |
869 | static function purge_orphans($do_output = false) { |
870 | ||
871 | // purge orphaned posts in main content table | |
872 | $result = db_query("DELETE FROM ttrss_entries WHERE | |
873 | NOT EXISTS (SELECT ref_id FROM ttrss_user_entries WHERE ref_id = id)"); | |
874 | ||
875 | if ($do_output) { | |
876 | $rows = db_affected_rows($result); | |
877 | _debug("Purged $rows orphaned posts."); | |
878 | } | |
879 | } | |
880 | ||
aeb1abed AD |
881 | static function catchupArticlesById($ids, $cmode, $owner_uid = false) { |
882 | ||
883 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
884 | if (count($ids) == 0) return; | |
885 | ||
886 | $tmp_ids = array(); | |
887 | ||
888 | foreach ($ids as $id) { | |
889 | array_push($tmp_ids, "ref_id = '$id'"); | |
890 | } | |
891 | ||
892 | $ids_qpart = join(" OR ", $tmp_ids); | |
893 | ||
894 | if ($cmode == 0) { | |
895 | db_query("UPDATE ttrss_user_entries SET | |
896 | unread = false,last_read = NOW() | |
897 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); | |
898 | } else if ($cmode == 1) { | |
899 | db_query("UPDATE ttrss_user_entries SET | |
900 | unread = true | |
901 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); | |
902 | } else { | |
903 | db_query("UPDATE ttrss_user_entries SET | |
904 | unread = NOT unread,last_read = NOW() | |
905 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); | |
906 | } | |
907 | ||
908 | /* update ccache */ | |
909 | ||
910 | $result = db_query("SELECT DISTINCT feed_id FROM ttrss_user_entries | |
911 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); | |
912 | ||
913 | while ($line = db_fetch_assoc($result)) { | |
2ed0d6c4 | 914 | CCache::update($line["feed_id"], $owner_uid); |
aeb1abed AD |
915 | } |
916 | } | |
917 | ||
918 | static function getLastArticleId() { | |
919 | $result = db_query("SELECT ref_id AS id FROM ttrss_user_entries | |
920 | WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY ref_id DESC LIMIT 1"); | |
921 | ||
922 | if (db_num_rows($result) == 1) { | |
923 | return db_fetch_result($result, 0, "id"); | |
924 | } else { | |
925 | return -1; | |
926 | } | |
927 | } | |
a230bf88 | 928 | |
4a0da0e5 AD |
929 | static function get_article_labels($id, $owner_uid = false) { |
930 | $rv = array(); | |
931 | ||
932 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
933 | ||
934 | $result = db_query("SELECT label_cache FROM | |
935 | ttrss_user_entries WHERE ref_id = '$id' AND owner_uid = " . | |
936 | $owner_uid); | |
937 | ||
938 | if (db_num_rows($result) > 0) { | |
939 | $label_cache = db_fetch_result($result, 0, "label_cache"); | |
940 | ||
941 | if ($label_cache) { | |
942 | $label_cache = json_decode($label_cache, true); | |
943 | ||
944 | if ($label_cache["no-labels"] == 1) | |
945 | return $rv; | |
946 | else | |
947 | return $label_cache; | |
948 | } | |
949 | } | |
950 | ||
951 | $result = db_query( | |
952 | "SELECT DISTINCT label_id,caption,fg_color,bg_color | |
953 | FROM ttrss_labels2, ttrss_user_labels2 | |
954 | WHERE id = label_id | |
955 | AND article_id = '$id' | |
956 | AND owner_uid = ". $owner_uid . " | |
957 | ORDER BY caption"); | |
958 | ||
959 | while ($line = db_fetch_assoc($result)) { | |
7c9b5a3f | 960 | $rk = array(Labels::label_to_feed_id($line["label_id"]), |
4a0da0e5 AD |
961 | $line["caption"], $line["fg_color"], |
962 | $line["bg_color"]); | |
963 | array_push($rv, $rk); | |
964 | } | |
965 | ||
966 | if (count($rv) > 0) | |
7c9b5a3f | 967 | Labels::update_cache($owner_uid, $id, $rv); |
4a0da0e5 | 968 | else |
7c9b5a3f | 969 | Labels::update_cache($owner_uid, $id, array("no-labels" => 1)); |
4a0da0e5 AD |
970 | |
971 | return $rv; | |
972 | } | |
973 | ||
6afcbcd1 | 974 | } |