]> git.wh0rd.org Git - tt-rss.git/blob - classes/article.php
enable support for readability (if af_readability is enabled) in shareanything bookma...
[tt-rss.git] / classes / article.php
1 <?php
2 class Article extends Handler_Protected {
3
4         function csrf_ignore($method) {
5                 $csrf_ignored = array("redirect", "editarticletags");
6
7                 return array_search($method, $csrf_ignored) !== false;
8         }
9
10         function redirect() {
11                 $id = $this->dbh->escape_string($_REQUEST['id']);
12
13                 $result = $this->dbh->query("SELECT link FROM ttrss_entries, ttrss_user_entries
14                                                 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'
15                                                 LIMIT 1");
16
17                 if ($this->dbh->num_rows($result) == 1) {
18                         $article_url = $this->dbh->fetch_result($result, 0, 'link');
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() {
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"]);
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 == "") {
40                         array_push($articles, format_article($id, false));
41                 } else if ($mode == "zoom") {
42                         array_push($articles, format_article($id, true, true));
43                 } else if ($mode == "raw") {
44                         if (isset($_REQUEST['html'])) {
45                                 header("Content-Type: text/html");
46                                 print '<link rel="stylesheet" type="text/css" href="css/tt-rss.css"/>';
47                         }
48
49                         $article = format_article($id, false, isset($_REQUEST["zoom"]));
50                         print $article['content'];
51                         return;
52                 }
53
54                 $this->catchupArticleById($id, 0);
55
56                 if (!$_SESSION["bw_limit"]) {
57                         foreach ($cids as $cid) {
58                                 if ($cid) {
59                                         array_push($articles, format_article($cid, false, false));
60                                 }
61                         }
62                 }
63
64                 print json_encode($articles);
65         }
66
67         private function catchupArticleById($id, $cmode) {
68
69                 if ($cmode == 0) {
70                         $this->dbh->query("UPDATE ttrss_user_entries SET
71                         unread = false,last_read = NOW()
72                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
73                 } else if ($cmode == 1) {
74                         $this->dbh->query("UPDATE ttrss_user_entries SET
75                         unread = true
76                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
77                 } else {
78                         $this->dbh->query("UPDATE ttrss_user_entries SET
79                         unread = NOT unread,last_read = NOW()
80                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
81                 }
82
83                 $feed_id = getArticleFeed($id);
84                 ccache_update($feed_id, $_SESSION["uid"]);
85         }
86
87         static function create_published_article($title, $url, $content, $labels_str,
88                         $owner_uid) {
89
90                 $guid = 'SHA1:' . sha1("ttshared:" . $url . $owner_uid); // include owner_uid to prevent global GUID clash
91
92                 if (!$content) {
93                         $pluginhost = new PluginHost();
94                         $pluginhost->load_all(PluginHost::KIND_ALL, $owner_uid);
95
96                         $af_readability = $pluginhost->get_plugin("Af_Readability");
97
98                         if ($af_readability) {
99                                 $extracted_content = $af_readability->extract_content($url);
100
101                                 if ($extracted_content) $content = db_escape_string($extracted_content);
102                         }
103                 }
104
105                 $content_hash = sha1($content);
106
107                 if ($labels_str != "") {
108                         $labels = explode(",", $labels_str);
109                 } else {
110                         $labels = array();
111                 }
112
113                 $rc = false;
114
115                 if (!$title) $title = $url;
116                 if (!$title && !$url) return false;
117
118                 if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) return false;
119
120                 db_query("BEGIN");
121
122                 // only check for our user data here, others might have shared this with different content etc
123                 $result = db_query("SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
124                         guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1");
125
126                 if (db_num_rows($result) != 0) {
127                         $ref_id = db_fetch_result($result, 0, "id");
128
129                         $result = db_query("SELECT int_id FROM ttrss_user_entries WHERE
130                                 ref_id = '$ref_id' AND owner_uid = '$owner_uid' LIMIT 1");
131
132                         if (db_num_rows($result) != 0) {
133                                 $int_id = db_fetch_result($result, 0, "int_id");
134
135                                 db_query("UPDATE ttrss_entries SET
136                                         content = '$content', content_hash = '$content_hash' WHERE id = '$ref_id'");
137
138                                 db_query("UPDATE ttrss_user_entries SET published = true,
139                                                 last_published = NOW() WHERE
140                                                 int_id = '$int_id' AND owner_uid = '$owner_uid'");
141                         } else {
142
143                                 db_query("INSERT INTO ttrss_user_entries
144                                         (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache,
145                                                 last_read, note, unread, last_published)
146                                         VALUES
147                                         ('$ref_id', '', NULL, NULL, $owner_uid, true, '', '', NOW(), '', false, NOW())");
148                         }
149
150                         if (count($labels) != 0) {
151                                 foreach ($labels as $label) {
152                                         label_add_article($ref_id, trim($label), $owner_uid);
153                                 }
154                         }
155
156                         $rc = true;
157
158                 } else {
159                         $result = db_query("INSERT INTO ttrss_entries
160                                 (title, guid, link, updated, content, content_hash, date_entered, date_updated)
161                                 VALUES
162                                 ('$title', '$guid', '$url', NOW(), '$content', '$content_hash', NOW(), NOW())");
163
164                         $result = db_query("SELECT id FROM ttrss_entries WHERE guid = '$guid'");
165
166                         if (db_num_rows($result) != 0) {
167                                 $ref_id = db_fetch_result($result, 0, "id");
168
169                                 db_query("INSERT INTO ttrss_user_entries
170                                         (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache,
171                                                 last_read, note, unread, last_published)
172                                         VALUES
173                                         ('$ref_id', '', NULL, NULL, $owner_uid, true, '', '', NOW(), '', false, NOW())");
174
175                                 if (count($labels) != 0) {
176                                         foreach ($labels as $label) {
177                                                 label_add_article($ref_id, trim($label), $owner_uid);
178                                         }
179                                 }
180
181                                 $rc = true;
182                         }
183                 }
184
185                 db_query("COMMIT");
186
187                 return $rc;
188         }
189
190         function editArticleTags() {
191
192                 print __("Tags for this article (separated by commas):")."<br>";
193
194                 $param = $this->dbh->escape_string($_REQUEST['param']);
195
196                 $tags = get_article_tags($this->dbh->escape_string($param));
197
198                 $tags_str = join(", ", $tags);
199
200                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$param\">";
201                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"article\">";
202                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"setArticleTags\">";
203
204                 print "<table width='100%'><tr><td>";
205
206                 print "<textarea dojoType=\"dijit.form.SimpleTextarea\" rows='4'
207                         style='font-size : 12px; width : 98%' id=\"tags_str\"
208                         name='tags_str'>$tags_str</textarea>
209                 <div class=\"autocomplete\" id=\"tags_choices\"
210                                 style=\"display:none\"></div>";
211
212                 print "</td></tr></table>";
213
214                 print "<div class='dlgButtons'>";
215
216                 print "<button dojoType=\"dijit.form.Button\"
217                         onclick=\"dijit.byId('editTagsDlg').execute()\">".__('Save')."</button> ";
218                 print "<button dojoType=\"dijit.form.Button\"
219                         onclick=\"dijit.byId('editTagsDlg').hide()\">".__('Cancel')."</button>";
220                 print "</div>";
221
222         }
223
224         function setScore() {
225                 $ids = $this->dbh->escape_string($_REQUEST['id']);
226                 $score = (int)$this->dbh->escape_string($_REQUEST['score']);
227
228                 $this->dbh->query("UPDATE ttrss_user_entries SET
229                         score = '$score' WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
230
231                 print json_encode(array("id" => $ids,
232                         "score" => (int)$score,
233                         "score_pic" => get_score_pic($score)));
234         }
235
236         function getScore() {
237                 $id = $this->dbh->escape_string($_REQUEST['id']);
238
239                 $result = $this->dbh->query("SELECT score FROM ttrss_user_entries WHERE ref_id = $id AND owner_uid = " . $_SESSION["uid"]);
240                 $score = $this->dbh->fetch_result($result, 0, "score");
241
242                 print json_encode(array("id" => $id,
243                         "score" => (int)$score,
244                         "score_pic" => get_score_pic($score)));
245         }
246
247
248         function setArticleTags() {
249
250                 $id = $this->dbh->escape_string($_REQUEST["id"]);
251
252                 $tags_str = $this->dbh->escape_string($_REQUEST["tags_str"]);
253                 $tags = array_unique(trim_array(explode(",", $tags_str)));
254
255                 $this->dbh->query("BEGIN");
256
257                 $result = $this->dbh->query("SELECT int_id FROM ttrss_user_entries WHERE
258                                 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
259
260                 if ($this->dbh->num_rows($result) == 1) {
261
262                         $tags_to_cache = array();
263
264                         $int_id = $this->dbh->fetch_result($result, 0, "int_id");
265
266                         $this->dbh->query("DELETE FROM ttrss_tags WHERE
267                                 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
268
269                         foreach ($tags as $tag) {
270                                 $tag = sanitize_tag($tag);
271
272                                 if (!tag_is_valid($tag)) {
273                                         continue;
274                                 }
275
276                                 if (preg_match("/^[0-9]*$/", $tag)) {
277                                         continue;
278                                 }
279
280                                 //                                      print "<!-- $id : $int_id : $tag -->";
281
282                                 if ($tag != '') {
283                                         $this->dbh->query("INSERT INTO ttrss_tags
284                                                                 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
285                                 }
286
287                                 array_push($tags_to_cache, $tag);
288                         }
289
290                         /* update tag cache */
291
292                         sort($tags_to_cache);
293                         $tags_str = join(",", $tags_to_cache);
294
295                         $this->dbh->query("UPDATE ttrss_user_entries
296                                 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
297                                                 AND owner_uid = " . $_SESSION["uid"]);
298                 }
299
300                 $this->dbh->query("COMMIT");
301
302                 $tags = get_article_tags($id);
303                 $tags_str = format_tags_string($tags, $id);
304                 $tags_str_full = join(", ", $tags);
305
306                 if (!$tags_str_full) $tags_str_full = __("no tags");
307
308                 print json_encode(array("id" => (int)$id,
309                                 "content" => $tags_str, "content_full" => $tags_str_full));
310         }
311
312
313         function completeTags() {
314                 $search = $this->dbh->escape_string($_REQUEST["search"]);
315
316                 $result = $this->dbh->query("SELECT DISTINCT tag_name FROM ttrss_tags
317                                 WHERE owner_uid = '".$_SESSION["uid"]."' AND
318                                 tag_name LIKE '$search%' ORDER BY tag_name
319                                 LIMIT 10");
320
321                 print "<ul>";
322                 while ($line = $this->dbh->fetch_assoc($result)) {
323                         print "<li>" . $line["tag_name"] . "</li>";
324                 }
325                 print "</ul>";
326         }
327
328         function assigntolabel() {
329                 return $this->labelops(true);
330         }
331
332         function removefromlabel() {
333                 return $this->labelops(false);
334         }
335
336         private function labelops($assign) {
337                 $reply = array();
338
339                 $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
340                 $label_id = $this->dbh->escape_string($_REQUEST["lid"]);
341
342                 $label = $this->dbh->escape_string(label_find_caption($label_id,
343                 $_SESSION["uid"]));
344
345                 $reply["info-for-headlines"] = array();
346
347                 if ($label) {
348
349                         foreach ($ids as $id) {
350
351                                 if ($assign)
352                                         label_add_article($id, $label, $_SESSION["uid"]);
353                                 else
354                                         label_remove_article($id, $label, $_SESSION["uid"]);
355
356                                 $labels = get_article_labels($id, $_SESSION["uid"]);
357
358                                 array_push($reply["info-for-headlines"],
359                                 array("id" => $id, "labels" => format_article_labels($labels, $id)));
360
361                         }
362                 }
363
364                 $reply["message"] = "UPDATE_COUNTERS";
365
366                 print json_encode($reply);
367         }
368
369
370
371 }