]>
Commit | Line | Data |
---|---|---|
de8260cb | 1 | <?php |
de8260cb AD |
2 | class API extends Handler { |
3 | ||
f1b3b3f3 | 4 | const API_LEVEL = 14; |
de8260cb AD |
5 | |
6 | const STATUS_OK = 0; | |
7 | const STATUS_ERR = 1; | |
8 | ||
9 | private $seq; | |
10 | ||
11 | function before($method) { | |
12 | if (parent::before($method)) { | |
12f31782 | 13 | header("Content-Type: text/json"); |
de8260cb AD |
14 | |
15 | if (!$_SESSION["uid"] && $method != "login" && $method != "isloggedin") { | |
6f7798b6 | 16 | $this->wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN')); |
de8260cb AD |
17 | return false; |
18 | } | |
19 | ||
a42c55f0 | 20 | if ($_SESSION["uid"] && $method != "logout" && !get_pref('ENABLE_API_ACCESS')) { |
6f7798b6 | 21 | $this->wrap(self::STATUS_ERR, array("error" => 'API_DISABLED')); |
de8260cb AD |
22 | return false; |
23 | } | |
24 | ||
25 | $this->seq = (int) $_REQUEST['seq']; | |
26 | ||
de8260cb AD |
27 | return true; |
28 | } | |
29 | return false; | |
30 | } | |
31 | ||
32 | function wrap($status, $reply) { | |
33 | print json_encode(array("seq" => $this->seq, | |
34 | "status" => $status, | |
35 | "content" => $reply)); | |
36 | } | |
37 | ||
38 | function getVersion() { | |
39 | $rv = array("version" => VERSION); | |
6f7798b6 | 40 | $this->wrap(self::STATUS_OK, $rv); |
de8260cb AD |
41 | } |
42 | ||
43 | function getApiLevel() { | |
a3b5394a | 44 | $rv = array("level" => self::API_LEVEL); |
6f7798b6 | 45 | $this->wrap(self::STATUS_OK, $rv); |
de8260cb AD |
46 | } |
47 | ||
48 | function login() { | |
79bb5589 | 49 | @session_destroy(); |
5160620c AD |
50 | @session_start(); |
51 | ||
3467e1fd | 52 | $login = $_REQUEST["user"]; |
de8260cb AD |
53 | $password = $_REQUEST["password"]; |
54 | $password_base64 = base64_decode($_REQUEST["password"]); | |
55 | ||
56 | if (SINGLE_USER_MODE) $login = "admin"; | |
57 | ||
3467e1fd AD |
58 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE login = ?"); |
59 | $sth->execute([$login]); | |
de8260cb | 60 | |
3467e1fd AD |
61 | if ($row = $sth->fetch()) { |
62 | $uid = $row["id"]; | |
de8260cb AD |
63 | } else { |
64 | $uid = 0; | |
65 | } | |
66 | ||
67 | if (!$uid) { | |
6f7798b6 | 68 | $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR")); |
de8260cb AD |
69 | return; |
70 | } | |
71 | ||
a42c55f0 AD |
72 | if (get_pref("ENABLE_API_ACCESS", $uid)) { |
73 | if (authenticate_user($login, $password)) { // try login with normal password | |
6f7798b6 | 74 | $this->wrap(self::STATUS_OK, array("session_id" => session_id(), |
5ba4ebc6 | 75 | "api_level" => self::API_LEVEL)); |
a42c55f0 | 76 | } else if (authenticate_user($login, $password_base64)) { // else try with base64_decoded password |
6f7798b6 | 77 | $this->wrap(self::STATUS_OK, array("session_id" => session_id(), |
5ba4ebc6 | 78 | "api_level" => self::API_LEVEL)); |
de8260cb | 79 | } else { // else we are not logged in |
a2108ee9 | 80 | user_error("Failed login attempt for $login from {$_SERVER['REMOTE_ADDR']}", E_USER_WARNING); |
6f7798b6 | 81 | $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR")); |
de8260cb AD |
82 | } |
83 | } else { | |
6f7798b6 | 84 | $this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED")); |
de8260cb AD |
85 | } |
86 | ||
87 | } | |
88 | ||
89 | function logout() { | |
90 | logout_user(); | |
6f7798b6 | 91 | $this->wrap(self::STATUS_OK, array("status" => "OK")); |
de8260cb AD |
92 | } |
93 | ||
94 | function isLoggedIn() { | |
6f7798b6 | 95 | $this->wrap(self::STATUS_OK, array("status" => $_SESSION["uid"] != '')); |
de8260cb AD |
96 | } |
97 | ||
98 | function getUnread() { | |
3467e1fd AD |
99 | $feed_id = $_REQUEST["feed_id"]; |
100 | $is_cat = $_REQUEST["is_cat"]; | |
de8260cb AD |
101 | |
102 | if ($feed_id) { | |
6f7798b6 | 103 | $this->wrap(self::STATUS_OK, array("unread" => getFeedUnread($feed_id, $is_cat))); |
de8260cb | 104 | } else { |
a230bf88 | 105 | $this->wrap(self::STATUS_OK, array("unread" => Feeds::getGlobalUnread())); |
de8260cb AD |
106 | } |
107 | } | |
108 | ||
109 | /* Method added for ttrss-reader for Android */ | |
110 | function getCounters() { | |
65af3b2c | 111 | $this->wrap(self::STATUS_OK, Counters::getAllCounters()); |
de8260cb AD |
112 | } |
113 | ||
114 | function getFeeds() { | |
3467e1fd | 115 | $cat_id = $_REQUEST["cat_id"]; |
9955a134 | 116 | $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]); |
3467e1fd AD |
117 | $limit = (int) $_REQUEST["limit"]; |
118 | $offset = (int) $_REQUEST["offset"]; | |
9955a134 | 119 | $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]); |
de8260cb | 120 | |
a42c55f0 | 121 | $feeds = $this->api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested); |
de8260cb | 122 | |
6f7798b6 | 123 | $this->wrap(self::STATUS_OK, $feeds); |
de8260cb AD |
124 | } |
125 | ||
126 | function getCategories() { | |
9955a134 AD |
127 | $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]); |
128 | $enable_nested = sql_bool_to_bool($_REQUEST["enable_nested"]); | |
b3575bd8 | 129 | $include_empty = sql_bool_to_bool($_REQUEST['include_empty']); |
de8260cb AD |
130 | |
131 | // TODO do not return empty categories, return Uncategorized and standard virtual cats | |
132 | ||
48646336 AD |
133 | if ($enable_nested) |
134 | $nested_qpart = "parent_cat IS NULL"; | |
135 | else | |
136 | $nested_qpart = "true"; | |
137 | ||
3467e1fd | 138 | $sth = $this->pdo->prepare("SELECT |
d49dfa38 AD |
139 | id, title, order_id, (SELECT COUNT(id) FROM |
140 | ttrss_feeds WHERE | |
7be3fcd5 AD |
141 | ttrss_feed_categories.id IS NOT NULL AND cat_id = ttrss_feed_categories.id) AS num_feeds, |
142 | (SELECT COUNT(id) FROM | |
143 | ttrss_feed_categories AS c2 WHERE | |
144 | c2.parent_cat = ttrss_feed_categories.id) AS num_cats | |
d49dfa38 | 145 | FROM ttrss_feed_categories |
3467e1fd AD |
146 | WHERE $nested_qpart AND owner_uid = ?"); |
147 | $sth->execute([$_SESSION['uid']]); | |
de8260cb AD |
148 | |
149 | $cats = array(); | |
150 | ||
3467e1fd | 151 | while ($line = $sth->fetch()) { |
66ca7c30 | 152 | if ($include_empty || $line["num_feeds"] > 0 || $line["num_cats"] > 0) { |
a42c55f0 | 153 | $unread = getFeedUnread($line["id"], true); |
d49dfa38 AD |
154 | |
155 | if ($enable_nested) | |
86a8351c | 156 | $unread += Feeds::getCategoryChildrenUnread($line["id"]); |
d49dfa38 AD |
157 | |
158 | if ($unread || !$unread_only) { | |
159 | array_push($cats, array("id" => $line["id"], | |
160 | "title" => $line["title"], | |
161 | "unread" => $unread, | |
162 | "order_id" => (int) $line["order_id"], | |
163 | )); | |
164 | } | |
de8260cb AD |
165 | } |
166 | } | |
167 | ||
168 | foreach (array(-2,-1,0) as $cat_id) { | |
c0a08063 | 169 | if ($include_empty || !$this->isCategoryEmpty($cat_id)) { |
a42c55f0 | 170 | $unread = getFeedUnread($cat_id, true); |
de8260cb | 171 | |
c0a08063 AD |
172 | if ($unread || !$unread_only) { |
173 | array_push($cats, array("id" => $cat_id, | |
a230bf88 | 174 | "title" => Feeds::getCategoryTitle($cat_id), |
c0a08063 AD |
175 | "unread" => $unread)); |
176 | } | |
de8260cb AD |
177 | } |
178 | } | |
179 | ||
6f7798b6 | 180 | $this->wrap(self::STATUS_OK, $cats); |
de8260cb AD |
181 | } |
182 | ||
183 | function getHeadlines() { | |
3467e1fd | 184 | $feed_id = $_REQUEST["feed_id"]; |
8aa3becc | 185 | if ($feed_id != "") { |
de8260cb | 186 | |
96ac72bc AD |
187 | if (is_numeric($feed_id)) $feed_id = (int) $feed_id; |
188 | ||
3467e1fd | 189 | $limit = (int)$_REQUEST["limit"]; |
1a740cf6 | 190 | |
6421b429 | 191 | if (!$limit || $limit >= 200) $limit = 200; |
1a740cf6 | 192 | |
3467e1fd AD |
193 | $offset = (int)$_REQUEST["skip"]; |
194 | $filter = $_REQUEST["filter"]; | |
9955a134 AD |
195 | $is_cat = sql_bool_to_bool($_REQUEST["is_cat"]); |
196 | $show_excerpt = sql_bool_to_bool($_REQUEST["show_excerpt"]); | |
197 | $show_content = sql_bool_to_bool($_REQUEST["show_content"]); | |
de8260cb | 198 | /* all_articles, unread, adaptive, marked, updated */ |
3467e1fd | 199 | $view_mode = $_REQUEST["view_mode"]; |
9955a134 | 200 | $include_attachments = sql_bool_to_bool($_REQUEST["include_attachments"]); |
3467e1fd | 201 | $since_id = (int)$_REQUEST["since_id"]; |
9955a134 | 202 | $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]); |
bd3c6723 AD |
203 | $sanitize_content = !isset($_REQUEST["sanitize"]) || |
204 | sql_bool_to_bool($_REQUEST["sanitize"]); | |
4baa1afa | 205 | $force_update = sql_bool_to_bool($_REQUEST["force_update"]); |
9997b38e | 206 | $has_sandbox = sql_bool_to_bool($_REQUEST["has_sandbox"]); |
3467e1fd AD |
207 | $excerpt_length = (int)$_REQUEST["excerpt_length"]; |
208 | $check_first_id = (int)$_REQUEST["check_first_id"]; | |
5c784e70 | 209 | $include_header = sql_bool_to_bool($_REQUEST["include_header"]); |
9997b38e AD |
210 | |
211 | $_SESSION['hasSandbox'] = $has_sandbox; | |
ffd07864 | 212 | |
19e47ad6 AD |
213 | $skip_first_id_check = false; |
214 | ||
0bbd1414 | 215 | $override_order = false; |
216 | switch ($_REQUEST["order_by"]) { | |
273c33e5 | 217 | case "title": |
81d96c0d | 218 | $override_order = "ttrss_entries.title, date_entered, updated"; |
273c33e5 | 219 | break; |
0bbd1414 | 220 | case "date_reverse": |
e9687f67 | 221 | $override_order = "score DESC, date_entered, updated"; |
19e47ad6 | 222 | $skip_first_id_check = true; |
0bbd1414 | 223 | break; |
224 | case "feed_dates": | |
225 | $override_order = "updated DESC"; | |
226 | break; | |
227 | } | |
ffd07864 | 228 | |
3e4af5b0 AD |
229 | /* do not rely on params below */ |
230 | ||
3467e1fd | 231 | $search = $_REQUEST["search"]; |
3e4af5b0 | 232 | |
5c784e70 | 233 | list($headlines, $headlines_header) = $this->api_get_headlines($feed_id, $limit, $offset, |
0bbd1414 | 234 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $override_order, |
9090b874 | 235 | $include_attachments, $since_id, $search, |
19e47ad6 | 236 | $include_nested, $sanitize_content, $force_update, $excerpt_length, $check_first_id, $skip_first_id_check); |
de8260cb | 237 | |
5c784e70 AD |
238 | if ($include_header) { |
239 | $this->wrap(self::STATUS_OK, array($headlines_header, $headlines)); | |
240 | } else { | |
241 | $this->wrap(self::STATUS_OK, $headlines); | |
242 | } | |
de8260cb | 243 | } else { |
6f7798b6 | 244 | $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); |
de8260cb AD |
245 | } |
246 | } | |
247 | ||
248 | function updateArticle() { | |
3467e1fd AD |
249 | $article_ids = explode(",", $_REQUEST["article_ids"]); |
250 | $mode = (int) $_REQUEST["mode"]; | |
251 | $data = $_REQUEST["data"]; | |
252 | $field_raw = (int)$_REQUEST["field"]; | |
de8260cb AD |
253 | |
254 | $field = ""; | |
255 | $set_to = ""; | |
256 | ||
257 | switch ($field_raw) { | |
258 | case 0: | |
259 | $field = "marked"; | |
7873d588 | 260 | $additional_fields = ",last_marked = NOW()"; |
de8260cb AD |
261 | break; |
262 | case 1: | |
263 | $field = "published"; | |
7873d588 | 264 | $additional_fields = ",last_published = NOW()"; |
de8260cb AD |
265 | break; |
266 | case 2: | |
267 | $field = "unread"; | |
7873d588 | 268 | $additional_fields = ",last_read = NOW()"; |
de8260cb AD |
269 | break; |
270 | case 3: | |
271 | $field = "note"; | |
272 | }; | |
273 | ||
274 | switch ($mode) { | |
275 | case 1: | |
276 | $set_to = "true"; | |
277 | break; | |
278 | case 0: | |
279 | $set_to = "false"; | |
280 | break; | |
281 | case 2: | |
282 | $set_to = "NOT $field"; | |
283 | break; | |
284 | } | |
285 | ||
3467e1fd | 286 | if ($field == "note") $set_to = $this->pdo->quote($data); |
de8260cb AD |
287 | |
288 | if ($field && $set_to && count($article_ids) > 0) { | |
289 | ||
3467e1fd | 290 | $article_qmarks = arr_qmarks($article_ids); |
de8260cb | 291 | |
3467e1fd AD |
292 | $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET |
293 | $field = $set_to $additional_fields | |
294 | WHERE ref_id IN ($article_qmarks) AND owner_uid = ?"); | |
295 | $sth->execute(array_merge($article_ids, [$_SESSION['uid']])); | |
de8260cb | 296 | |
3467e1fd | 297 | $num_updated = $sth->rowCount(); |
de8260cb AD |
298 | |
299 | if ($num_updated > 0 && $field == "unread") { | |
3467e1fd | 300 | $sth = $this->pdo->query("SELECT DISTINCT feed_id FROM ttrss_user_entries |
de8260cb AD |
301 | WHERE ref_id IN ($article_ids)"); |
302 | ||
3467e1fd | 303 | while ($line = $sth->fetch()) { |
2ed0d6c4 | 304 | CCache::update($line["feed_id"], $_SESSION["uid"]); |
de8260cb AD |
305 | } |
306 | } | |
307 | ||
6f7798b6 | 308 | $this->wrap(self::STATUS_OK, array("status" => "OK", |
de8260cb AD |
309 | "updated" => $num_updated)); |
310 | ||
311 | } else { | |
6f7798b6 | 312 | $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); |
de8260cb AD |
313 | } |
314 | ||
315 | } | |
316 | ||
317 | function getArticle() { | |
318 | ||
3467e1fd | 319 | $article_ids = explode(",", $_REQUEST["article_id"]); |
73c77ab0 J |
320 | $sanitize_content = !isset($_REQUEST["sanitize"]) || |
321 | sql_bool_to_bool($_REQUEST["sanitize"]); | |
de8260cb | 322 | |
3467e1fd | 323 | if ($article_ids) { |
de8260cb | 324 | |
3467e1fd AD |
325 | $article_qmarks = arr_qmarks($article_ids); |
326 | ||
327 | $sth = $this->pdo->prepare("SELECT id,guid,title,link,content,feed_id,comments,int_id, | |
429ff9af | 328 | marked,unread,published,score,note,lang, |
6f81395d | 329 | ".SUBSTRING_FOR_DATE."(updated,1,16) as updated, |
73c77ab0 J |
330 | author,(SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title, |
331 | (SELECT site_url FROM ttrss_feeds WHERE id = feed_id) AS site_url, | |
332 | (SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) AS hide_images | |
6f81395d | 333 | FROM ttrss_entries,ttrss_user_entries |
3467e1fd | 334 | WHERE id IN ($article_qmarks) AND ref_id = id AND owner_uid = ?"); |
de8260cb | 335 | |
3467e1fd | 336 | $sth->execute(array_merge($article_ids, [$_SESSION['uid']])); |
de8260cb | 337 | |
6f81395d | 338 | $articles = array(); |
de8260cb | 339 | |
3467e1fd AD |
340 | while ($line = $sth->fetch()) { |
341 | ||
342 | $attachments = Article::get_article_enclosures($line['id']); | |
343 | ||
344 | $article = array( | |
345 | "id" => $line["id"], | |
346 | "guid" => $line["guid"], | |
347 | "title" => $line["title"], | |
348 | "link" => $line["link"], | |
349 | "labels" => Article::get_article_labels($line['id']), | |
350 | "unread" => sql_bool_to_bool($line["unread"]), | |
351 | "marked" => sql_bool_to_bool($line["marked"]), | |
352 | "published" => sql_bool_to_bool($line["published"]), | |
353 | "comments" => $line["comments"], | |
354 | "author" => $line["author"], | |
355 | "updated" => (int) strtotime($line["updated"]), | |
356 | "feed_id" => $line["feed_id"], | |
357 | "attachments" => $attachments, | |
358 | "score" => (int)$line["score"], | |
359 | "feed_title" => $line["feed_title"], | |
360 | "note" => $line["note"], | |
361 | "lang" => $line["lang"] | |
362 | ); | |
363 | ||
364 | if ($sanitize_content) { | |
365 | $article["content"] = sanitize( | |
366 | $line["content"], | |
367 | sql_bool_to_bool($line['hide_images']), | |
368 | false, $line["site_url"], false, $line["id"]); | |
369 | } else { | |
370 | $article["content"] = $line["content"]; | |
371 | } | |
6f81395d | 372 | |
3467e1fd AD |
373 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) { |
374 | $article = $p->hook_render_article_api(array("article" => $article)); | |
375 | } | |
6f81395d | 376 | |
3467e1fd | 377 | array_push($articles, $article); |
de8260cb | 378 | |
6f81395d | 379 | } |
de8260cb | 380 | |
6f81395d AD |
381 | $this->wrap(self::STATUS_OK, $articles); |
382 | } else { | |
383 | $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); | |
384 | } | |
de8260cb AD |
385 | } |
386 | ||
387 | function getConfig() { | |
388 | $config = array( | |
389 | "icons_dir" => ICONS_DIR, | |
390 | "icons_url" => ICONS_URL); | |
391 | ||
392 | $config["daemon_is_running"] = file_is_locked("update_daemon.lock"); | |
393 | ||
3467e1fd AD |
394 | $sth = $this->pdo->prepare("SELECT COUNT(*) AS cf FROM |
395 | ttrss_feeds WHERE owner_uid = ?"); | |
396 | $sth->execute([$_SESSION['uid']]); | |
397 | $row = $sth->fetch(); | |
de8260cb | 398 | |
3467e1fd | 399 | $config["num_feeds"] = $row["cf"]; |
de8260cb | 400 | |
6f7798b6 | 401 | $this->wrap(self::STATUS_OK, $config); |
de8260cb AD |
402 | } |
403 | ||
404 | function updateFeed() { | |
3467e1fd | 405 | $feed_id = (int) $_REQUEST["feed_id"]; |
de8260cb | 406 | |
29c92d7b | 407 | if (!ini_get("open_basedir")) { |
e6c886bf | 408 | RSSUtils::update_rss_feed($feed_id); |
29c92d7b | 409 | } |
de8260cb | 410 | |
6f7798b6 | 411 | $this->wrap(self::STATUS_OK, array("status" => "OK")); |
de8260cb AD |
412 | } |
413 | ||
414 | function catchupFeed() { | |
3467e1fd AD |
415 | $feed_id = $_REQUEST["feed_id"]; |
416 | $is_cat = $_REQUEST["is_cat"]; | |
de8260cb | 417 | |
86a8351c | 418 | Feeds::catchup_feed($feed_id, $is_cat); |
de8260cb | 419 | |
6f7798b6 | 420 | $this->wrap(self::STATUS_OK, array("status" => "OK")); |
de8260cb AD |
421 | } |
422 | ||
423 | function getPref() { | |
3467e1fd | 424 | $pref_name = $_REQUEST["pref_name"]; |
de8260cb | 425 | |
6f7798b6 | 426 | $this->wrap(self::STATUS_OK, array("value" => get_pref($pref_name))); |
de8260cb AD |
427 | } |
428 | ||
ea1c2903 | 429 | function getLabels() { |
ea1c2903 AD |
430 | $article_id = (int)$_REQUEST['article_id']; |
431 | ||
432 | $rv = array(); | |
433 | ||
3467e1fd | 434 | $sth = $this->pdo->prepare("SELECT id, caption, fg_color, bg_color |
ea1c2903 | 435 | FROM ttrss_labels2 |
3467e1fd AD |
436 | WHERE owner_uid = ? ORDER BY caption"); |
437 | $sth->execute([$_SESSION['uid']]); | |
ea1c2903 AD |
438 | |
439 | if ($article_id) | |
4a0da0e5 | 440 | $article_labels = Article::get_article_labels($article_id); |
ea1c2903 AD |
441 | else |
442 | $article_labels = array(); | |
443 | ||
3467e1fd | 444 | while ($line = $sth->fetch()) { |
ea1c2903 AD |
445 | |
446 | $checked = false; | |
447 | foreach ($article_labels as $al) { | |
7c9b5a3f | 448 | if (Labels::feed_to_label_id($al[0]) == $line['id']) { |
ea1c2903 AD |
449 | $checked = true; |
450 | break; | |
451 | } | |
452 | } | |
453 | ||
454 | array_push($rv, array( | |
7c9b5a3f | 455 | "id" => (int)Labels::label_to_feed_id($line['id']), |
ea1c2903 AD |
456 | "caption" => $line['caption'], |
457 | "fg_color" => $line['fg_color'], | |
458 | "bg_color" => $line['bg_color'], | |
459 | "checked" => $checked)); | |
460 | } | |
461 | ||
6f7798b6 | 462 | $this->wrap(self::STATUS_OK, $rv); |
ea1c2903 AD |
463 | } |
464 | ||
396bfdf9 AD |
465 | function setArticleLabel() { |
466 | ||
3467e1fd AD |
467 | $article_ids = explode(",", $_REQUEST["article_ids"]); |
468 | $label_id = (int) $_REQUEST['label_id']; | |
91f49ba1 | 469 | $assign = sql_bool_to_bool($_REQUEST['assign']); |
396bfdf9 | 470 | |
3467e1fd | 471 | $label = Labels::find_caption(Labels::feed_to_label_id($label_id), $_SESSION["uid"]); |
396bfdf9 AD |
472 | |
473 | $num_updated = 0; | |
474 | ||
475 | if ($label) { | |
476 | ||
477 | foreach ($article_ids as $id) { | |
478 | ||
479 | if ($assign) | |
7c9b5a3f | 480 | Labels::add_article($id, $label, $_SESSION["uid"]); |
396bfdf9 | 481 | else |
7c9b5a3f | 482 | Labels::remove_article($id, $label, $_SESSION["uid"]); |
396bfdf9 AD |
483 | |
484 | ++$num_updated; | |
485 | ||
486 | } | |
487 | } | |
488 | ||
6f7798b6 | 489 | $this->wrap(self::STATUS_OK, array("status" => "OK", |
396bfdf9 AD |
490 | "updated" => $num_updated)); |
491 | ||
492 | } | |
493 | ||
79f9bef7 | 494 | function index($method) { |
1ffe3391 | 495 | $plugin = PluginHost::getInstance()->get_api_method(strtolower($method)); |
79f9bef7 AD |
496 | |
497 | if ($plugin && method_exists($plugin, $method)) { | |
498 | $reply = $plugin->$method(); | |
499 | ||
6f7798b6 | 500 | $this->wrap($reply[0], $reply[1]); |
79f9bef7 AD |
501 | |
502 | } else { | |
6f7798b6 | 503 | $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD', "method" => $method)); |
79f9bef7 | 504 | } |
de8260cb AD |
505 | } |
506 | ||
8361e724 | 507 | function shareToPublished() { |
3467e1fd AD |
508 | $title = strip_tags($_REQUEST["title"]); |
509 | $url = strip_tags($_REQUEST["url"]); | |
510 | $content = strip_tags($_REQUEST["content"]); | |
8361e724 | 511 | |
a42c55f0 | 512 | if (Article::create_published_article($title, $url, $content, "", $_SESSION["uid"])) { |
6f7798b6 | 513 | $this->wrap(self::STATUS_OK, array("status" => 'OK')); |
8361e724 | 514 | } else { |
6f7798b6 | 515 | $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed')); |
8361e724 AD |
516 | } |
517 | } | |
04f60eb7 | 518 | |
a42c55f0 | 519 | static function api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested = false) { |
04f60eb7 AD |
520 | |
521 | $feeds = array(); | |
522 | ||
3467e1fd AD |
523 | $pdo = Db::pdo(); |
524 | ||
525 | $limit = (int) $limit; | |
526 | $offset = (int) $offset; | |
527 | $cat_id = (int) $cat_id; | |
528 | ||
04f60eb7 AD |
529 | /* Labels */ |
530 | ||
531 | if ($cat_id == -4 || $cat_id == -2) { | |
65af3b2c | 532 | $counters = Counters::getLabelCounters(true); |
04f60eb7 AD |
533 | |
534 | foreach (array_values($counters) as $cv) { | |
535 | ||
536 | $unread = $cv["counter"]; | |
537 | ||
538 | if ($unread || !$unread_only) { | |
539 | ||
540 | $row = array( | |
1d3cbe31 | 541 | "id" => (int) $cv["id"], |
04f60eb7 AD |
542 | "title" => $cv["description"], |
543 | "unread" => $cv["counter"], | |
544 | "cat_id" => -2, | |
545 | ); | |
546 | ||
547 | array_push($feeds, $row); | |
548 | } | |
549 | } | |
550 | } | |
551 | ||
552 | /* Virtual feeds */ | |
553 | ||
554 | if ($cat_id == -4 || $cat_id == -1) { | |
555 | foreach (array(-1, -2, -3, -4, -6, 0) as $i) { | |
a42c55f0 | 556 | $unread = getFeedUnread($i); |
04f60eb7 AD |
557 | |
558 | if ($unread || !$unread_only) { | |
86a8351c | 559 | $title = Feeds::getFeedTitle($i); |
04f60eb7 AD |
560 | |
561 | $row = array( | |
562 | "id" => $i, | |
563 | "title" => $title, | |
564 | "unread" => $unread, | |
565 | "cat_id" => -1, | |
566 | ); | |
567 | array_push($feeds, $row); | |
568 | } | |
569 | ||
570 | } | |
571 | } | |
572 | ||
573 | /* Child cats */ | |
574 | ||
575 | if ($include_nested && $cat_id) { | |
3467e1fd | 576 | $sth = $pdo->prepare("SELECT |
c053b976 | 577 | id, title, order_id FROM ttrss_feed_categories |
3467e1fd AD |
578 | WHERE parent_cat = ? AND owner_uid = ? ORDER BY id, title"); |
579 | ||
580 | $sth->execute([$cat_id, $_SESSION['uid']]); | |
04f60eb7 | 581 | |
3467e1fd | 582 | while ($line = $sth->fetch()) { |
a42c55f0 | 583 | $unread = getFeedUnread($line["id"], true) + |
86a8351c | 584 | Feeds::getCategoryChildrenUnread($line["id"]); |
04f60eb7 AD |
585 | |
586 | if ($unread || !$unread_only) { | |
587 | $row = array( | |
1d3cbe31 | 588 | "id" => (int) $line["id"], |
04f60eb7 AD |
589 | "title" => $line["title"], |
590 | "unread" => $unread, | |
591 | "is_cat" => true, | |
c053b976 | 592 | "order_id" => (int) $line["order_id"] |
04f60eb7 AD |
593 | ); |
594 | array_push($feeds, $row); | |
595 | } | |
596 | } | |
597 | } | |
598 | ||
599 | /* Real feeds */ | |
600 | ||
601 | if ($limit) { | |
602 | $limit_qpart = "LIMIT $limit OFFSET $offset"; | |
603 | } else { | |
604 | $limit_qpart = ""; | |
605 | } | |
606 | ||
607 | if ($cat_id == -4 || $cat_id == -3) { | |
3467e1fd | 608 | $sth = $pdo->prepare("SELECT |
04f60eb7 AD |
609 | id, feed_url, cat_id, title, order_id, ". |
610 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
3467e1fd AD |
611 | FROM ttrss_feeds WHERE owner_uid = ? |
612 | ORDER BY cat_id, title " . $limit_qpart); | |
613 | $sth->execute([$_SESSION['uid']]); | |
04f60eb7 | 614 | |
3467e1fd | 615 | } else { |
04f60eb7 | 616 | |
3467e1fd | 617 | $sth = $pdo->prepare("SELECT |
04f60eb7 AD |
618 | id, feed_url, cat_id, title, order_id, ". |
619 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
620 | FROM ttrss_feeds WHERE | |
3467e1fd AD |
621 | (cat_id = :cat OR (:cat = 0 AND cat_id IS NULL)) |
622 | AND owner_uid = :uid | |
623 | ORDER BY cat_id, title " . $limit_qpart); | |
624 | $sth->execute([":uid" => $_SESSION['uid'], ":cat" => $cat_id]); | |
04f60eb7 AD |
625 | } |
626 | ||
3467e1fd | 627 | while ($line = $sth->fetch()) { |
04f60eb7 | 628 | |
a42c55f0 | 629 | $unread = getFeedUnread($line["id"]); |
04f60eb7 AD |
630 | |
631 | $has_icon = feed_has_icon($line['id']); | |
632 | ||
633 | if ($unread || !$unread_only) { | |
634 | ||
635 | $row = array( | |
636 | "feed_url" => $line["feed_url"], | |
637 | "title" => $line["title"], | |
638 | "id" => (int)$line["id"], | |
639 | "unread" => (int)$unread, | |
640 | "has_icon" => $has_icon, | |
641 | "cat_id" => (int)$line["cat_id"], | |
ef3da31c | 642 | "last_updated" => (int) strtotime($line["last_updated"]), |
04f60eb7 AD |
643 | "order_id" => (int) $line["order_id"], |
644 | ); | |
645 | ||
646 | array_push($feeds, $row); | |
647 | } | |
648 | } | |
649 | ||
650 | return $feeds; | |
651 | } | |
652 | ||
7b55001e AD |
653 | /** |
654 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
655 | */ | |
a42c55f0 | 656 | static function api_get_headlines($feed_id, $limit, $offset, |
04f60eb7 AD |
657 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order, |
658 | $include_attachments, $since_id, | |
5c784e70 | 659 | $search = "", $include_nested = false, $sanitize_content = true, |
19e47ad6 | 660 | $force_update = false, $excerpt_length = 100, $check_first_id = false, $skip_first_id_check = false) { |
4baa1afa | 661 | |
3467e1fd AD |
662 | $pdo = Db::pdo(); |
663 | ||
4baa1afa AD |
664 | if ($force_update && $feed_id > 0 && is_numeric($feed_id)) { |
665 | // Update the feed if required with some basic flood control | |
666 | ||
3467e1fd | 667 | $sth = $pdo->prepare( |
4baa1afa | 668 | "SELECT cache_images,".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated |
3467e1fd AD |
669 | FROM ttrss_feeds WHERE id = ?"); |
670 | $sth->execute([$feed_id]); | |
4baa1afa | 671 | |
3467e1fd AD |
672 | if ($row = $sth->fetch()) { |
673 | $last_updated = strtotime($row["last_updated"]); | |
674 | $cache_images = sql_bool_to_bool($row["cache_images"]); | |
4baa1afa AD |
675 | |
676 | if (!$cache_images && time() - $last_updated > 120) { | |
e6c886bf | 677 | RSSUtils::update_rss_feed($feed_id, true); |
4baa1afa | 678 | } else { |
3467e1fd AD |
679 | $sth = $pdo->prepare("UPDATE ttrss_feeds SET last_updated = '1970-01-01', last_update_started = '1970-01-01' |
680 | WHERE id = ?"); | |
681 | $sth->execute([$feed_id]); | |
4baa1afa AD |
682 | } |
683 | } | |
684 | } | |
04f60eb7 | 685 | |
f5a0fb8b AD |
686 | $params = array( |
687 | "feed" => $feed_id, | |
688 | "limit" => $limit, | |
689 | "view_mode" => $view_mode, | |
690 | "cat_view" => $is_cat, | |
691 | "search" => $search, | |
692 | "override_order" => $order, | |
693 | "offset" => $offset, | |
694 | "since_id" => $since_id, | |
695 | "include_children" => $include_nested, | |
19e47ad6 AD |
696 | "check_first_id" => $check_first_id, |
697 | "skip_first_id_check" => $skip_first_id_check | |
f5a0fb8b AD |
698 | ); |
699 | ||
aeb1abed | 700 | $qfh_ret = Feeds::queryFeedHeadlines($params); |
f5a0fb8b | 701 | |
34440201 | 702 | $result = $qfh_ret[0]; |
04f60eb7 | 703 | $feed_title = $qfh_ret[1]; |
48fefe2f | 704 | $first_id = $qfh_ret[6]; |
04f60eb7 AD |
705 | |
706 | $headlines = array(); | |
b0ce3d33 AD |
707 | |
708 | $headlines_header = array( | |
709 | 'id' => $feed_id, | |
48fefe2f | 710 | 'first_id' => $first_id, |
b0ce3d33 | 711 | 'is_cat' => $is_cat); |
4f2a2ca9 | 712 | |
ec57104d | 713 | if (!is_numeric($result)) { |
3467e1fd | 714 | while ($line = $result->fetch()) { |
34440201 AD |
715 | $line["content_preview"] = truncate_string(strip_tags($line["content"]), $excerpt_length); |
716 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) { | |
717 | $line = $p->hook_query_headlines($line, $excerpt_length, true); | |
718 | } | |
04f60eb7 | 719 | |
34440201 AD |
720 | $is_updated = ($line["last_read"] == "" && |
721 | ($line["unread"] != "t" && $line["unread"] != "1")); | |
f71a669b | 722 | |
34440201 | 723 | $tags = explode(",", $line["tag_cache"]); |
f71a669b | 724 | |
34440201 AD |
725 | $label_cache = $line["label_cache"]; |
726 | $labels = false; | |
f71a669b AD |
727 | |
728 | if ($label_cache) { | |
34440201 AD |
729 | $label_cache = json_decode($label_cache, true); |
730 | ||
731 | if ($label_cache) { | |
732 | if ($label_cache["no-labels"] == 1) | |
733 | $labels = array(); | |
734 | else | |
735 | $labels = $label_cache; | |
736 | } | |
f71a669b | 737 | } |
f71a669b | 738 | |
4a0da0e5 | 739 | if (!is_array($labels)) $labels = Article::get_article_labels($line["id"]); |
04f60eb7 | 740 | |
34440201 | 741 | $headline_row = array( |
04f60eb7 | 742 | "id" => (int)$line["id"], |
18186149 | 743 | "guid" => $line["guid"], |
04f60eb7 AD |
744 | "unread" => sql_bool_to_bool($line["unread"]), |
745 | "marked" => sql_bool_to_bool($line["marked"]), | |
746 | "published" => sql_bool_to_bool($line["published"]), | |
34440201 | 747 | "updated" => (int)strtotime($line["updated"]), |
04f60eb7 AD |
748 | "is_updated" => $is_updated, |
749 | "title" => $line["title"], | |
750 | "link" => $line["link"], | |
751 | "feed_id" => $line["feed_id"], | |
752 | "tags" => $tags, | |
753 | ); | |
754 | ||
34440201 | 755 | if ($include_attachments) |
7e5f8d9f | 756 | $headline_row['attachments'] = Article::get_article_enclosures( |
34440201 | 757 | $line['id']); |
04f60eb7 | 758 | |
34440201 AD |
759 | if ($show_excerpt) |
760 | $headline_row["excerpt"] = $line["content_preview"]; | |
04f60eb7 | 761 | |
34440201 | 762 | if ($show_content) { |
04f60eb7 | 763 | |
34440201 AD |
764 | if ($sanitize_content) { |
765 | $headline_row["content"] = sanitize( | |
766 | $line["content"], | |
767 | sql_bool_to_bool($line['hide_images']), | |
768 | false, $line["site_url"], false, $line["id"]); | |
769 | } else { | |
770 | $headline_row["content"] = $line["content"]; | |
771 | } | |
04f60eb7 | 772 | } |
04f60eb7 | 773 | |
34440201 AD |
774 | // unify label output to ease parsing |
775 | if ($labels["no-labels"] == 1) $labels = array(); | |
04f60eb7 | 776 | |
34440201 | 777 | $headline_row["labels"] = $labels; |
04f60eb7 | 778 | |
34440201 AD |
779 | $headline_row["feed_title"] = $line["feed_title"] ? $line["feed_title"] : |
780 | $feed_title; | |
04f60eb7 | 781 | |
34440201 AD |
782 | $headline_row["comments_count"] = (int)$line["num_comments"]; |
783 | $headline_row["comments_link"] = $line["comments"]; | |
04f60eb7 | 784 | |
34440201 | 785 | $headline_row["always_display_attachments"] = sql_bool_to_bool($line["always_display_enclosures"]); |
04f60eb7 | 786 | |
34440201 | 787 | $headline_row["author"] = $line["author"]; |
4f2a2ca9 | 788 | |
34440201 AD |
789 | $headline_row["score"] = (int)$line["score"]; |
790 | $headline_row["note"] = $line["note"]; | |
791 | $headline_row["lang"] = $line["lang"]; | |
583dbc56 | 792 | |
34440201 AD |
793 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) { |
794 | $headline_row = $p->hook_render_article_api(array("headline" => $headline_row)); | |
795 | } | |
b6604c96 | 796 | |
34440201 AD |
797 | array_push($headlines, $headline_row); |
798 | } | |
5c784e70 | 799 | } else if (is_numeric($result) && $result == -1) { |
48fefe2f | 800 | $headlines_header['first_id_changed'] = true; |
04f60eb7 AD |
801 | } |
802 | ||
5c784e70 | 803 | return array($headlines, $headlines_header); |
04f60eb7 AD |
804 | } |
805 | ||
efc6553d | 806 | function unsubscribeFeed() { |
3467e1fd | 807 | $feed_id = (int) $_REQUEST["feed_id"]; |
efc6553d | 808 | |
3467e1fd AD |
809 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE |
810 | id = ? AND owner_uid = ?"); | |
811 | $sth->execute([$feed_id, $_SESSION['uid']]); | |
efc6553d | 812 | |
3467e1fd | 813 | if ($row = $sth->fetch()) { |
a42c55f0 | 814 | Pref_Feeds::remove_feed($feed_id, $_SESSION["uid"]); |
6f7798b6 | 815 | $this->wrap(self::STATUS_OK, array("status" => "OK")); |
efc6553d | 816 | } else { |
6f7798b6 | 817 | $this->wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND")); |
efc6553d AD |
818 | } |
819 | } | |
820 | ||
821 | function subscribeToFeed() { | |
3467e1fd AD |
822 | $feed_url = $_REQUEST["feed_url"]; |
823 | $category_id = (int) $_REQUEST["category_id"]; | |
824 | $login = $_REQUEST["login"]; | |
825 | $password = $_REQUEST["password"]; | |
efc6553d AD |
826 | |
827 | if ($feed_url) { | |
86a8351c | 828 | $rc = Feeds::subscribe_to_feed($feed_url, $category_id, $login, $password); |
efc6553d | 829 | |
6f7798b6 | 830 | $this->wrap(self::STATUS_OK, array("status" => $rc)); |
efc6553d | 831 | } else { |
6f7798b6 | 832 | $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); |
efc6553d AD |
833 | } |
834 | } | |
835 | ||
0bb5833b | 836 | function getFeedTree() { |
b3575bd8 | 837 | $include_empty = sql_bool_to_bool($_REQUEST['include_empty']); |
66ca7c30 | 838 | |
a42c55f0 | 839 | $pf = new Pref_Feeds($_REQUEST); |
0bb5833b AD |
840 | |
841 | $_REQUEST['mode'] = 2; | |
66ca7c30 | 842 | $_REQUEST['force_show_empty'] = $include_empty; |
0bb5833b AD |
843 | |
844 | if ($pf){ | |
845 | $data = $pf->makefeedtree(); | |
6f7798b6 | 846 | $this->wrap(self::STATUS_OK, array("categories" => $data)); |
0bb5833b | 847 | } else { |
6f7798b6 | 848 | $this->wrap(self::STATUS_ERR, array("error" => |
0bb5833b AD |
849 | 'UNABLE_TO_INSTANTIATE_OBJECT')); |
850 | } | |
851 | ||
852 | } | |
c0a08063 | 853 | |
dc5a8a21 | 854 | // only works for labels or uncategorized for the time being |
c0a08063 AD |
855 | private function isCategoryEmpty($id) { |
856 | ||
857 | if ($id == -2) { | |
3467e1fd AD |
858 | $sth = $this->pdo->prepare("SELECT COUNT(id) AS count FROM ttrss_labels2 |
859 | WHERE owner_uid = ?"); | |
860 | $sth->execute([$_SESSION['uid']]); | |
861 | $row = $sth->fetch(); | |
c0a08063 | 862 | |
3467e1fd | 863 | return $row["count"] == 0; |
c0a08063 | 864 | |
dc5a8a21 | 865 | } else if ($id == 0) { |
3467e1fd AD |
866 | $sth = $this->pdo->prepare("SELECT COUNT(id) AS count FROM ttrss_feeds |
867 | WHERE cat_id IS NULL AND owner_uid = ?"); | |
868 | $sth->execute([$_SESSION['uid']]); | |
869 | $row = $sth->fetch(); | |
dc5a8a21 | 870 | |
3467e1fd | 871 | return $row["count"] == 0; |
dc5a8a21 | 872 | |
c0a08063 AD |
873 | } |
874 | ||
875 | return false; | |
876 | } | |
877 | ||
878 | ||
891df346 | 879 | } |