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